封装Qt的SQLite接口类

还没测试完善。。

 1 #ifndef SQLITE_H
 2 #define SQLITE_H
 3 
 4 #include <QSqlDatabase>
 5 #include <QSqlQuery>
 6 #include <QSqlRecord>
 7 #include <QSqlError>
 8 #include <QMap>
 9 #include <QInternal>
10 #include <QString>
11 #include <QDebug>
12 
13 
14 class Sqlite
15 {
16     QSqlDatabase db;
17     QString host_name;  //主机名
18     QString user_name;  //用户名
19     QString pwd;    //密码
20     QString db_file_name;   //数据库文件名
21     QSqlError error;
22     QSqlQuery *query;
23     QSqlRecord rec;
24 public:
25     Sqlite(QString m_file_name,QString m_host_name,QString m_user_name,QString m_pwd);
26     ~Sqlite();
27     bool create_table(QString table_name,QMap<QString,QString> table_data);   //新建表
28     int db_query(QString m_query_sql); //发送数据库语句
29     int add(QString table_name,QMap<QString,QString> data); //增加数据
30     int del(QString table_name,QMap<QString,QString> where); //删除一条记录
31     int updata(QString table_name,QMap<QString,QString> where,QMap<QString,QString> data);  //更新数据
32     int find(QString table_name,QList<QString> key,QMap<QString,QString> where,QList<QList<QString>> *row);    //查找
33     int find(QString table_name,QList<QString> key,QList<QList<QString>> *row);    //查找所有
34     QString getError();
35 };
36 
37 #endif // SQLITE_H

sqlite.cpp

  1 #include "sqlite.h"
  2 
  3 Sqlite::Sqlite(QString m_file_name,QString m_host_name,QString m_user_name,QString m_pwd)
  4 {
  5     host_name=m_host_name;
  6     user_name=m_user_name;
  7     pwd=m_pwd;
  8     db_file_name=m_file_name;
  9     db=QSqlDatabase::addDatabase("QSQLITE");
 10     query=new QSqlQuery(db);
 11     db.setHostName(host_name);
 12     db.setUserName(user_name);
 13     db.setPassword(pwd);
 14     db.setDatabaseName(db_file_name);
 15     if(!db.open())
 16     {
 17         qDebug()<<"database open error.";
 18         error = db.lastError();
 19     }
 20 }
 21 
 22 
 23 /*
 24  *
 25  * create_table函数:创建数据库表
 26  * table_name:表名
 27  * table_data:表项 名->属性
 28  *
 29  */
 30 
 31 bool Sqlite::create_table(QString table_name,QMap<QString,QString> table_data)
 32 {
 33     QSqlQuery query;
 34     QString sql="create table "+table_name+" (";
 35     for(QMap<QString,QString>::const_iterator i=table_data.constBegin();i!=table_data.constEnd();i++)
 36     {
 37         sql+=i.key()+' '+i.value();
 38         if(i!=table_data.constEnd())
 39             sql+=',';
 40     }
 41     sql.chop(1);
 42     sql+=")";
 43     qDebug()<<sql;
 44     query.prepare(sql);
 45     return query.exec();
 46 }
 47 
 48 /*
 49  * 向数据库中增加数据
 50  */
 51 
 52 int Sqlite::add(QString table_name, QMap<QString, QString> data)
 53 {
 54     QString sql="insert into "+table_name+  "(";
 55     QString values=" values(";
 56     for(QMap<QString,QString>::const_iterator i=data.constBegin();i!=data.constEnd();i++)
 57     {
 58         sql+=i.key()+", ";
 59         values+=i.value()+", ";
 60     }
 61     sql.chop(2);
 62     values.chop(2);
 63     sql+=")";
 64     values+=")";
 65     sql+=values;
 66     qDebug()<<sql;
 67     query->prepare(sql);
 68     return query->exec();
 69 }
 70 
 71 /*
 72  * 向数据库发送一条语句
 73  */
 74 
 75 int Sqlite::db_query(QString m_query_sql)
 76 {
 77     query->prepare(m_query_sql);
 78     return query->exec();
 79 }
 80 
 81 /*
 82  * 删除一条记录
 83  */
 84 
 85 int Sqlite::del(QString table_name, QMap<QString, QString> where)
 86 {
 87     QString sql="delete ";
 88     sql+=table_name;
 89     sql+=" where ";
 90     for(QMap<QString,QString>::const_iterator i=where.constBegin();i!=where.constEnd();i++)
 91     {
 92         sql+=i.key()+"=";
 93         sql+="'"+i.value()+"' ";
 94     }
 95     sql.chop(2);
 96     query->prepare(sql);
 97     return query->exec();
 98 }
 99 
100 /*
101  * 修改数据库记录
102  */
103 int Sqlite::updata(QString table_name, QMap<QString, QString> where, QMap<QString, QString> data)
104 {
105     QString sql="updata "+table_name+" set";
106     for(QMap<QString,QString>::const_iterator i=where.constBegin();i!=where.constEnd();i++)
107     {
108         sql+=i.key()+"=";
109         sql+=i.value()+" ";
110     }
111     sql+="where ";
112     for(QMap<QString,QString>::const_iterator i=where.constBegin();i!=where.constEnd();i++)
113     {
114         sql+=i.key()+"=";
115         sql+=i.value()+" ";
116     }
117     return query->exec();
118 }
119 
120 /*
121  * 查找所有记录
122  */
123 
124 int Sqlite::find(QString table_name,QList<QString> key,QList<QList<QString>> *row)
125 {
126     QString sql="select ";
127     int len=key.size();
128     for(int i=0;i<len;i++)
129     {
130         sql+=key.at(i);
131         sql+=",";
132     }
133     sql.chop(1);
134     sql+=" from "+table_name;
135     //qDebug()<<sql;
136     query->prepare(sql);
137     if(query->exec())
138     {
139         while (query->next()) {
140             QList<QString> j;
141             for(int i=0;i<len;i++)
142             {
143                 j.append(query->value(i).toString());
144             }
145             row->append(j);
146         }
147         return 1;
148     }
149     else return 0;
150 }
151 
152 int Sqlite::find(QString table_name, QList<QString> key, QMap<QString, QString> where, QList<QList<QString> > *row)
153 {
154     QString sql="select ";
155     int len=key.size();
156     for(int i=0;i<len;i++)
157     {
158         sql+=key.at(i);
159         sql+=",";
160     }
161     sql.chop(1);
162     sql+=" from "+table_name;
163     sql+=" where ";
164     for(QMap<QString,QString>::const_iterator i=where.constBegin();i!=where.constEnd();i++)
165     {
166         sql+=i.key()+"="+i.value()+",";
167     }
168     sql.chop(1);
169     //qDebug()<<sql;
170     query->prepare(sql);
171     if(query->exec())
172     {
173         while (query->next()) {
174             QList<QString> j;
175             for(int i=0;i<len;i++)
176             {
177                 j.append(query->value(i).toString());
178             }
179             row->append(j);
180         }
181         return 1;
182     }
183     else return 0;
184 }
185 
186 /*
187  * 获取错误信息
188  */
189 
190 QString Sqlite::getError()
191 {
192     return error.text();
193 }
194 
195 
196 
197 Sqlite::~Sqlite()
198 {
199     db.close();
200 }
原文地址:https://www.cnblogs.com/HadesBlog/p/9159770.html