Sqlite3入门,安装和使用。
Win 7 + MSVC 2012 试用版
之所以用Sqlite,主要是因为,朋友不能在公司电脑上面安装东西,基于Mysql这种需要安装而且要起后台服务的程序自然都玩不了,那就Sqlite。
1、首先声明一下,完全没有必要安装,网上很多什么又是编译,又是什么生成lib的玩法,完全是绕路了(我觉得)。直接在工程里面把官方的C和H文件including进去就是了。我不知道其他人实际开发怎么玩的,有一点我知道,Qt的Sqlite3支持就是直接把源代码涵盖进去,而不是使用库再链接。 2、下载官网的源代码http://www.sqlite.com/download.html:
下载后,解压,如下: D:Sqlite开发源代码sqlite-amalgamation-3071700>dir Volume in drive D is D Volume Serial Number is D8DC-109A Directory of D:Sqlite开发源代码sqlite-amalgamation-3071700 2013/05/22 09:09 <DIR> . 2013/05/22 09:09 <DIR> .. 2013/05/20 13:56 99,401 shell.c 2013/05/20 13:56 4,973,863 sqlite3.c 2013/05/20 13:56 348,618 sqlite3.h 2013/05/20 13:56 25,974 sqlite3ext.h 4 File(s) 5,447,856 bytes 2 Dir(s) 37,436,293,120 bytes free
4、完成程序功能,也就是source.cpp的实现代码: #include <stdio.h> #include "sqlite3.h" static int callback(void *NotUsed,int argc,char **argv,char **azColName){ int i; for(i=0; i<argc; i++){ printf("%s = %sn",azColName[i],argv[i] ? argv[i] : "NULL"); } printf("n"); return 0; } int main(){ sqlite3 *db; char *zErrMsg = 0; int rc; rc = sqlite3_open("./test.db",&db); if( rc ){ fprintf(stderr,"Can't open database: %sn",sqlite3_errmsg(db)); sqlite3_close(db); return(1); } rc = sqlite3_exec(db,"create table if not exists work (first integer primary key autoincrement,second text)",callback,&zErrMsg); if( rc!=SQLITE_OK ){ fprintf(stderr,"SQL error: %sn",zErrMsg); sqlite3_free(zErrMsg); } rc = sqlite3_exec(db,"insert into work(second) values ('吃饭')","insert into work(second) values ('asdf')",zErrMsg); sqlite3_free(zErrMsg); } sqlite3_close(db); return 0; }
6、这个时候我们用Sqlite shell工具查看效果: D:Qt_ProjSqlite_cons1Sqlite_cons1>Sqlite3 test.db SQLite version 3.7.17 2013-05-20 00:56:22 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite> .table work sqlite> select * from work; 1|│èZ╣╟δ?┤ 2|asdf sqlite> 上面有乱码,是因为我的cmd我修改了字体和显示,以至于不能显示中文了。 7、再说明一下,编译的文件里面一个是cpp的,而Sqlite3的实现文件是.c文件,没有关系的,在sqlite3.h里面有这么一段: #ifndef _SQLITE3_H_ #define _SQLITE3_H_ #include <stdarg.h> /* Needed for the definition of va_list */ /* ** Make sure we can call this stuff from C++. */ #ifdef __cplusplus extern "C" { #endif 8、如果出现一定要用lib和dll文件的情况,那么,也不用重新编译.c文件,执行如下操作即可: E:Sqlite开发windows 工具>cd sqlite-dll-win32-x86-3071700 E:Sqlite开发windows 工具sqlite-dll-win32-x86-3071700>dir 驱动器 E 中的卷是 E 卷的序列号是 4CD7-E1DC E:Sqlite开发windows 工具sqlite-dll-win32-x86-3071700 的目录 2013/05/22 18:55 <DIR> . 2013/05/22 18:55 <DIR> .. 2013/05/20 12:58 4,385 sqlite3.def 2013/05/20 12:58 620,718 sqlite3.dll 2 个文件 625,103 字节 2 个目录 41,283,182,592 可用字节 E:Sqlite开发windows 工具sqlite-dll-win32-x86-3071700>lib /def:sqlite3.def Microsoft (R) Library Manager Version 11.00.60315.1 Copyright (C) Microsoft Corporation. All rights reserved. LINK : warning LNK4068: /MACHINE not specified; defaulting to X86 Creating library sqlite3.lib and object sqlite3.exp E:Sqlite开发windows 工具sqlite-dll-win32-x86-3071700>dir 驱动器 E 中的卷是 E 卷的序列号是 4CD7-E1DC E:Sqlite开发windows 工具sqlite-dll-win32-x86-3071700 的目录 2013/05/22 19:10 <DIR> . 2013/05/22 19:10 <DIR> .. 2013/05/20 12:58 4,718 sqlite3.dll 2013/05/22 19:10 29,576 sqlite3.exp 2013/05/22 19:10 49,522 sqlite3.lib 4 个文件 704,201 字节 2 个目录 41,092,480 可用字节 也就是在dll文件夹路径下面做一次如下操作,就可以得到相应的lib文件了: lib /def:sqlite3.def
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |