[代码] mydata.h
10 |
#ifndef MYDATA_H |
11 |
#define MYDATA_H |
12 |
#include <QObject> |
13 |
#include <QString> |
14 |
#include <QtSql/QSqlTableModel> |
15 |
#include <QtSql/QSqlQuery> |
16 |
#include <QStringList> |
17 |
#include <QtSql/QSqlDatabase> |
18 |
19 |
class mydata : public QObject
|
20 |
{ |
21 |
Q_OBJECT
|
22 |
public :
|
23 |
explicit mydata(QObject *parent = 0);
|
24 |
~mydata();
|
25 |
void SetTableName(QString table);
|
26 |
bool opendatabase();
|
27 |
28 |
bool insert(QString &table, QStringList &names,QStringList &values);
|
29 |
30 |
bool Updata(QString &table, QStringList &names,QStringList &values, QString &expression);
|
31 |
32 |
bool del(QString &table, QString &expression);
|
33 |
34 |
void GetValues(QString &table, QStringList &values);
|
35 |
private :
|
36 |
QSqlTableModel *model;
|
37 |
QSqlDatabase _db;
|
38 |
QString _tableName;
|
39 |
40 |
}; |
41 |
42 |
#endif // MYDATA_H |
mydata.cpp
001 |
/**************************************************************************** |
002 |
** |
003 |
** Copyright (C) 2013 C,g |
004 |
** All rights reserved. |
005 |
** Contact: 1213125967@qq.com |
006 |
** Please keep the author contact information. |
007 |
** 2013-10-31 |
008 |
** |
009 |
****************************************************************************/ |
010 |
#include <QtSql/QSqlQuery> |
011 |
#include <QObject> |
012 |
#include "mydata.h" |
013 |
014 |
mydata::mydata(QObject *parent) : |
015 |
QObject(parent)
|
016 |
{ |
017 |
018 |
} |
019 |
020 |
mydata::~mydata() |
021 |
{ |
022 |
_db.close();
|
023 |
} |
024 |
025 |
void mydata::SetTableName(QString table)
|
026 |
{ |
027 |
_tableName = table + ".db" ;
|
028 |
} |
029 |
030 |
/* |
031 |
打开数据库.
|
032 |
*/ |
033 |
bool mydata::opendatabase()
|
034 |
{ |
035 |
_db = QSqlDatabase::addDatabase( "QSQLITE" );
|
036 |
_db.setDatabaseName(_tableName);
|
037 |
return _db.open();
|
038 |
} |
039 |
040 |
/* |
041 |
插入函数.
|
042 |
构造SQL插入语句.
|
043 |
*/ |
044 |
bool mydata::insert(QString &table, QStringList &names, QStringList &values)
|
045 |
{ |
046 |
if (names.size() != values.size())
|
047 |
{
|
048 |
return false ;
|
049 |
}
|
050 |
051 |
QSqlQuery query(QSqlDatabase::database());
|
052 |
053 |
QString sql = QString( "insert into " ) + table + QString( "(" );
|
054 |
055 |
int i;
|
056 |
057 |
for (i=0; i < names.size(); i++)
|
058 |
{
|
059 |
sql = sql + names.value(i);
|
060 |
if (i != names.size() - 1)
|
061 |
{
|
062 |
sql+=QString( "," );
|
063 |
}
|
064 |
else
|
065 |
{
|
066 |
067 |
sql = sql + QString( ")" );
|
068 |
}
|
069 |
}
|
070 |
071 |
sql = sql + QString( "values (" );
|
072 |
073 |
for (i = 0; i < values.size(); i++)
|
074 |
{
|
075 |
sql = sql + QString( "'" ) + values.value(i) + QString( "'" );
|
076 |
if (i != values.size()-1)
|
077 |
{
|
078 |
sql = sql + QString( "," );
|
079 |
}
|
080 |
}
|
081 |
082 |
sql = sql + QString( ")" );
|
083 |
084 |
if (query.exec(sql))
|
085 |
{
|
086 |
return true ;
|
087 |
}
|
088 |
else
|
089 |
{
|
090 |
return false ;
|
091 |
}
|
092 |
} |
093 |
094 |
/* |
095 |
修改函数.
|
096 |
构造SQL修改语句.
|
097 |
*/ |
098 |
bool mydata::Updata(QString &table, QStringList &names, QStringList &values, QString &expression)
|
099 |
{ |
100 |
if (names.size() != values.size())
|
101 |
{
|
102 |
return false ;
|
103 |
}
|
104 |
105 |
//UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值
|
106 |
QSqlQuery query(QSqlDatabase::database());
|
107 |
QString sql = QString( "update " )+table+QString( " set " );
|
108 |
for ( int i = 0; i < names.size(); i++)
|
109 |
{
|
110 |
sql = sql + names.value(i);
|
111 |
sql = sql + QString( " = '" );
|
112 |
sql = sql + values.value(i);
|
113 |
sql = sql + QString( "'" );
|
114 |
if (i != names.size()-1)
|
115 |
{
|
116 |
sql = sql + QString( " ," );
|
117 |
}
|
118 |
}
|
119 |
120 |
sql = sql + QString( " where " ) + expression;
|
121 |
if (query.exec(sql))
|
122 |
{
|
123 |
return true ;
|
124 |
}
|
125 |
else
|
126 |
{
|
127 |
return false ;
|
128 |
}
|
129 |
} |
130 |
131 |
/* |
132 |
删除函数.
|
133 |
构造SQL删除语句.
|
134 |
*/ |
135 |
bool mydata::del(QString &table, QString &expression)
|
136 |
{ |
137 |
//DELETE FROM 表名称 WHERE 列名称 = 值
|
138 |
QSqlQuery query(QSqlDatabase::database());
|
139 |
QString sql = QString( "delete from " ) + table + QString( " where " ) + expression;
|
140 |
141 |
if (query.exec(sql))
|
142 |
{
|
143 |
return true ;
|
144 |
}
|
145 |
else
|
146 |
{
|
147 |
return false ;
|
148 |
}
|
149 |
} |
150 |
151 |
void mydata::GetValues(QString &table, QStringList &values)
|
152 |
{ |
153 |
QSqlQuery query(QSqlDatabase::database());
|
154 |
QString sql = QString( "select * from " ) + table;
|
155 |
query.exec(sql);
|
156 |
while (query.next())
|
157 |
{
|
158 |
values << query.value(0).toString();
|
159 |
}
|
160 |
} |