-----------------
-----------------
sqlite,是一款轻型的
数据库,是遵守ACID的关联式
数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。它能够
支持Windows/Linux/Unix等主流的操作系统,同时能够跟很多程序语言相结合,比如 Tcl、C#、
PHP、Java等,还有ODBC接口,同样比起
MysqL、Postgre
sql这两款开源世界著名的
数据库管理系统来讲,它的处理速度比他们都快。
sqlite第
一个Alpha版本诞生于2000年5月。 至今已经有13个年头,
sqlite也迎来了
一个版本
sqlite 3已经发布。
不像常见的客户-服务器范例,
sqlite引擎不是个程序与之通信的独立进程,而是连接到程序中成为它的
一个主要部分。所以主要的通信协议是在编程语言内的直接API
调用。这在消耗总量、延迟时间和整体简单性上有积极的作用。整个
数据库(定义、表、索引和数据本身)都在宿主主机上存储在
一个单一的
文件中(eg. test.db)。它的简单的设计是通过在开始
一个事务的时候锁定整个数据
文件而完成的。
----------------
-----------------
从
sqlite 官网下载最新的源码包 http://www.
sqlite.org/download.html 现在最新版本为3080100版本
下载完成后解压: tar -zxvf
sqlite-autoconf-3080100.tar.gz
2.交叉编译
a.解压完成之后,进入解压后的目录:
cd
sqlite-autoconf-3080100
mkdir build
./con
figure --prefix=/
文件夹的
绝对路径/s
sqlite-autoconf-3080100/build
--Prefix:可以指定编译之后目标存放的路径,可自行设定
还可以通过 --host=arm-linux 指定使用 arm交叉编译器进行交叉编译 以供嵌入式系统使用
d.然后运行指令:
make
make install
3.编译和安装完成之后,在我们指定的路径/
文件夹的
绝对路径/s
sqlite-autoconf-3080100/build 下会
生成四个
文件夹:
bin、lib、include、share,将bin中的
文件拷贝至开发板的/usr/bin中,将lib
文件夹中的所有
内容拷贝至项目lib
文件夹中
然后在开发板的终端中输入指令:
执行的结果为:
sqlite version 3.8.1 2013-10-17 12:57:35
Enter ".help" for instructions
Enter
sql statements terminated with a ";"
----------------------------
----------------------------
create table qttest([id] integer PRIMARY KEY autoincrement,[userName] varchar(32),[userPwd] varchar(32));
insert into qttest(id,userName,userPwd) values(NULL,"apple","123456");
insert into qttest(id,"orange","123456");
2.创建qt项目,将 test.db 放在qt的项目
文件夹,并在qt工程
文件(xxx.pro)里
添加:
01 |
#include <QApplication> |
02 |
#include <QMessageBox> |
06 |
bool dbConnect( char *dbname) |
08 |
QsqlDatabase db = QsqlDatabase::addDatabase( "QsqlITE" ); |
10 |
//db.setHostName(host); |
11 |
db.setDatabaseName(dbname); |
12 |
//db.setUserName(user); |
13 |
//db.setPassword(pwd); |
17 |
qDebug()<<"Database Error!" <<db.lastError().text(); |
19 |
}else 20 |
"Database Success!"21 |
true; |
24 |
25 |
26 |
intmain( argc, *argv[]) |
27 |
28 |
QApplication app(argc,argv); |
31 |
32 |
iRet=dbConnect((*) "test.db" 33 |
(iRet ==) |
37 |
query.exec("select id,userPwd from qttest" ); |
41 |
42 |
"Query Error!"<<query.lastError().text(); |
49 |
iRet=query.numRowsAffected(); |
52 |
qDebug()<<"NO Record!"; |
55 |
qDebug()<<"Find "<<iRet<<" Records!"; |
60 |
id = query.value(0).toInt(); |
61 |
QString name = query.value(1).toString(); |
62 |
QString pwd = query.value(2).toString(); |
63 |
"user info: id= "<<id<< ",name= " <<name<< display:inline; margin:0px!important; padding:0px!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-family:Consolas,pwd= " <<pwd; |
67 |
"insert into qttest(id,userPwd) values(null,\"ox\",\"123456\")"68 |
//更新数据 |
69 |
"update qttest set userPwd= \"1111\" where id=\"1\""70 |
71 |
/* |
74 |
query.prepare("insert into qttest(id,userPwd) values(?,?,?)"); |
75 |
query.addBindValue("null"); |
76 |
query.addBindValue("dog"); |
77 |
query.addBindValue("hello"); |
原因不祥,如果是
代码或是
数据库配置等其他原因,望能得到指点。(平台环境:Ubuntu + Qt Creator 4.8)
运行结果:
--------------------------------
--------------------------------
1. 核心对象:
在
sqlite中最主要的两个对象是,database_connection和prepared_statement。 database_connection对象是由
sqlite3_
open()接口
函数创建并返回的,在应用程序使用任何其他
sqlite接口
函数之前,必须先
调用该
函数以便获得database_connnection对象,在随后的其他APIs
调用中,都需要该对象作为输入参数以完成相应的工作。至于 prepare_statement,我们可以简单的将它视为编译后的
sql语句,因此,所有和
sql语句执行相关的
函数也都需要该对象作为输入参数以完成指定的
sql操作。
2. 核心接口:
上面已经提到过这个
函数了,它是操作
sqlite
数据库的入口
函数。该
函数返回的database_connection对象是很多其他
sqlite APIs的句柄参数。注意,我们通过该
函数既可以打开已经存在的
数据库文件,也可以创建新的
数据库文件。对于该
函数返回的 database_connection对象,我们可以在多个线程之间共享该对象的指针,以便完成和
数据库相关的任意操作。然而在多线程情况下,我们更为推荐的使用方式是,为每个线程创建独立的database_connection对象。对于该
函数还有一点也需要额外说明,我们没有必要为了访问多个
数据库而创建多个
数据库连接对象,因为通过
sqlite
自带的ATTACH命令可以在
一个连接中方便的访问多个
数据库。
该
函数将
sql文本转换为prepared_statement对象,并在
函数执行后返回该对象的指针。事实上,该
函数并不会评估参数指定
sql语句,它仅仅是将
sql文本初始化为待执行的状态。最后需要指出的,对于新的应用程序我们可以使用
sqlite3_prepare_v2接口
函数来替代该
函数以完成相同的工作。
该
函数用于评估
sqlite3_prepare
函数返回的prepared_statement对象,在执行完该
函数之后,prepared_statement对象的内部指针将指向其返回的结果集的第一行。如果打算进一步迭代其后的数据行,就需要不断的
调用该
函数,直到所有的数据行都遍历完毕。然而对于INSERT、UPDATE和DELETE等DML语句,该
函数执行一次即可完成。
该
函数用于
获取当前行指定列的数据,然而严格意义上讲,此
函数在
sqlite的接口
函数中并不存在,而是由一组相关的接口
函数来完成该
功能,其中每个
函数都返回不同类型的数据,如:
该
函数用于销毁prepared statement对象,否则将会造成内存泄露。
该
函数用于
关闭之前打开的database_connection对象,其中所有和该对象相关的prepared_statements对象都必须在此之前先被销毁。
14 |
15 |
printf ( "Can't open database: %s\n" display:inline; margin:0px!important; padding:0px!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-family:Consolas,sqlite3_errmsg(db)); |
18 |
19 |
"open database!\n"); |
21 |
22 |
/****************************************************************************** |
25 |
const char *sql,// sql to be evaluated |
26 |
int (*callback)(void*,int,char**,char**),// Callback function |
27 |
void *,// 1st argument to callback |
28 |
char **errmsg // Error msg written here |
30 |
********************************************************************************/ |
32 |
iRet = sqlite3_exec(db,display:inline; margin:0px!important; padding:0px!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-family:Consolas,\"cat\",NULL,&ErrMsg); |
34 |
35 |
"sql error: %s\n"display:inline; margin:0px!important; padding:0px!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-family:Consolas,ErrMsg); |
40 |
41 |
/****************************************************************************** |
44 |
const char *zsql,// sql statement,UTF-8 encoded |
45 |
int nByte,// Maximum length of zsql in bytes |
46 |
sqlite3_stmt **ppStmt,// OUT: Statement handle |
47 |
const char **pzTail // OUT: Pointer to unused portion of zsql |
49 |
********************************************************************************/ |
51 |
iRet = sqlite3_prepare(db,-1,&ppStmt,NULL); |
53 |
54 |
"Select error\n"55 |
//释放ppStmt |
58 |
59 |
"user info:\n"60 |
(sqlite3_step(ppStmt) == sqlITE_ROW) |
61 |
62 |
"Id: %s\t"display:inline; margin:0px!important; padding:0px!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-family:Consolas,sqlite3_column_text(ppStmt,0)); |
63 |
"Name: %s\t"display:inline; margin:0px!important; padding:0px!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-family:Consolas,1)); |
64 |
"Pwd: %s\n"display:inline; margin:0px!important; padding:0px!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-family:Consolas,2)); |
转载:http://www.ichanging.org/qt-
sqlite.html
原文地址:https://www.jb51.cc/sqlite/199010.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。