基于sqlite的Qt 数据库封装
[代码] 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 |
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 |
} |
基于sqlite的Qt 数据库封装的更多相关文章
- Qt基于sqlite数据库的管理小软件
闲来无事,写了一个基于sqlite的数据库管理小软件. 先上图 中心思想就是: 创建一个数据库 然后每一个分组对应一个数据表 然后遍历该数据表.将名字以treewidgetItem显示出来.添加删除实 ...
- 适用于app.config与web.config的ConfigUtil读写工具类 基于MongoDb官方C#驱动封装MongoDbCsharpHelper类(CRUD类) 基于ASP.NET WEB API实现分布式数据访问中间层(提供对数据库的CRUD) C# 实现AOP 的几种常见方式
适用于app.config与web.config的ConfigUtil读写工具类 之前文章:<两种读写配置文件的方案(app.config与web.config通用)>,现在重新整理一 ...
- 基于sqlitecpp的sqlite3 c++封装
Github: 人富水也甜 感谢GitHub大佬: sqlitecpp github: https://github.com/SRombauts/SQLiteCpp sqlite: https:// ...
- Asp.Net Core 2.0 项目实战(4)ADO.NET操作数据库封装、 EF Core操作及实例
Asp.Net Core 2.0 项目实战(1) NCMVC开源下载了 Asp.Net Core 2.0 项目实战(2)NCMVC一个基于Net Core2.0搭建的角色权限管理开发框架 Asp.Ne ...
- 基于C#的MongoDB数据库开发应用(2)--MongoDB数据库的C#开发
在上篇博客<基于C#的MongoDB数据库开发应用(1)--MongoDB数据库的基础知识和使用>里面,我总结了MongoDB数据库的一些基础信息,并在最后面部分简单介绍了数据库C#驱动的 ...
- qt数据库多线程问题的解决(QSqlDatabase只能在创建它的线程中使用)
Qt数据库由QSqlDatabase::addDatabase()生成的QSqlDatabase只能在创建它的线程中使用, 在多线程中共用连接或者在另外一个线程中创建query都是不支持的几乎国内没有 ...
- Qt添加驱动——Qt数据库之添加MySQL驱动插件
Qt数据库之添加MySQL驱动插件(1) 现在可用的数据库驱动只有3种,在Qt中,我们需要自己编译其他数据库驱动的代码,让它们以插件的形式来使用.下面我们就以现在比较流行的MySQL数据库为例,说明一 ...
- Qt数据库_资料
1. QT笔记_数据库总结(一)-rojian-ChinaUnix博客.html http://blog.chinaunix.net/uid-28194872-id-3631462.html (里面有 ...
- Qt——数据库编程
一.概述 Qt提供了一个类似JDBC的数据库接口,需要为每个可以连接的特定数据库提供驱动程序,可以通过 QStringList QSqlDatabase::drivers() 知道当前版本的Qt哪些驱 ...
随机推荐
- git url ssh和https相互切换
Changing a remote's URL The git remote set-url command changes an existing remote repository URL. Ti ...
- Unity3D Persistent Storage
[Unity3D Persistent Storage] 1.PlayerPrefs类以键值对的形式来提供PersistentStorage能力.提供小额存储能力.(做成sst可以提供大规模数据存储) ...
- [原创]Devexpress XtraReports 系列 2 创建表格报表
昨天发表了Devexpress XtraReports系列开篇,今天我们继续. 今天的主题是创建表格报表. 首先我们来看看最后实现的效果.Demo最后附上. 接下来开始讲解如何一步一步做出这个报表: ...
- 微软IOC容器Unity简单代码示例2-配置文件方式
@(编程) 1. 通过Nuget下载Unity 这个就不介绍了 2. 接口代码 namespace UnityDemo { interface ILogIn { void Login(); } } n ...
- poj 1719 Shooting Contest
http://poj.org/problem?id=1719 Shooting Contest Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- hdu 2819 Swap
Swap http://acm.hdu.edu.cn/showproblem.php?pid=2819 Special Judge Problem Description Given an N*N m ...
- android 自定义用相机拍照后的照片存储位置
1.imageUri = Uri.fromFile(new File(Environment .getExternalStorageDirectory()+ File.separator + getP ...
- Codeforces 682 D. Alyona and Strings (dp)
题目链接:http://codeforces.com/contest/682/problem/D 给你两个字符串,求两个字符串中顺序k个的相同子串 长度之和.(注意是子串) dp[i][j][k][0 ...
- CodeForces 709C Letters Cyclic Shift (水题)
题意:给定一个字符串,让你把它的一个子串字符都减1,使得总字符串字典序最小. 析:由于这个题是必须要有一个字串,所以你就要注意这个只有一个字符a的情况,其他的就从开始减 1,如果碰到a了就不减了,如果 ...
- 传统的Ado.net 参数设置:params SqlParameter[] commandParameters
C#代码 ExecuteReader(string connectionString, CommandType commandType, string commandText, params Sql ...