1.sqllite是什么?
sqllite是一个进程库,代码开源,无须配置,库最小可小到600kb,生成的数据库文件支持跨平台使用
2.windows如何使用sqllte3?
从https://www.sqlite.org/download.html官方网站下载对应版本压缩包(例如:
sqlite-dll-win64-x64-3310100.zip (797.73 KiB) |
)
解压完成后,可以看到得到 sqlite3.def、sqlite3.dll 文件。
下载sqlite3源码(例如:sqlite-amalgamation-3310100.zip),解压获得sqlite3.h文件
将sqlite3.dll、sqlite3.h和sqlite3.lib拷贝至工程下,即可使用sqlite数据库
下载sqlite3工具包,用于创建数据库(例如:sqlite-tools-win32-x86-3310100.zip)
3.如何得到sqlite3.lib?
1.运行cmd
2.进入VS安装目录下
3.拷贝sqlite3.dll和sqlite3.def至改目录下
3.运行lib /def:sqlite3.def /machine:ix86
命令如下:
>>D: >>cd D:Program FilesMicrosoft Visual Studio 9.0VCin >>D:Program FilesMicrosoft Visual Studio 9.0VCin> lib /def:sqlite3.def /machine:ix86
4.使用sqlite增删改查
1)写入sqlite(删除同理)
7万条数据6s执行完成
1 // sqllite2.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include "sqlite3.h" 6 #include "time.h" 7 #include <string> 8 using namespace std; 9 10 int _tmain(int argc, _TCHAR* argv[]) 11 { 12 sqlite3 *pDB; 13 sqlite3 *db = NULL; 14 char *errMsg; 15 int result = sqlite3_open("./data_test.db", &pDB); 16 int start = clock(); 17 18 //sqlite3_exec(pDB, "PRAGMA synchronous = OFF ", 0, 0, 0); 19 //sqlite3_exec(pDB, "PRAGMA journal_mode = OFF", 0, 0, 0); 20 sqlite3_exec(pDB, "Begin transaction; ", 0, 0, 0); 21 sqlite3_exec(pDB, "CREATE TABLE 't_test' ('id' INTEGER,'material_name' TEXT,'machine_name' TEXT, 'process_name' TEXT, 'process_id' INTEGER,'int03' INTEGER, 'process_time' INTEGER, 'process_cost' REAL, 'campaign_name' TEXT, 'dest_pos' TEXT, 'int01' INTEGER, 'float01' REAL, 'float02' TEXT, 'float03' TEXT, 'float04' TEXT, 'float05' TEXT, 'float06' TEXT, 'float07' TEXT, 'float08' TEXT, 'float09' TEXT, 'float10' TEXT, 'float11' TEXT, 'float12' TEXT, 'float13' TEXT, 'float14' TEXT, 'float15' TEXT, 'float16' TEXT, 'float17' TEXT, 'float18' TEXT, 'float19' TEXT, 'float20' TEXT, 'int02' INTEGER, 'int04' INTEGER, 'int05' INTEGER, 'int06' TEXT, 'int07' TEXT, 'int08' TEXT, 'int09' TEXT, 'int10' TEXT, 'string01' TEXT, 'string02' TEXT, 'string03' TEXT, 'string04' TEXT, 'string05' TEXT, 'string06' TEXT, 'string07' TEXT, 'string08' TEXT, 'string09' TEXT, 'string10' TEXT, 'string11' TEXT, 'string12' TEXT, 'string13' TEXT, 'string14' TEXT, 'string15' TEXT, 'string16' TEXT, 'string17' TEXT, 'string18' TEXT, 'string19' TEXT, 'string20' TEXT ); CREATE INDEX 'ix_t_process_machine_id' ON 't_process_machine' ('id'); ", 0, 0, 0); 22 23 for (int i = 0; i<70000; i++) 24 { 25 sqlite3_exec(pDB, "INSERT INTO t_test ( material_name , machine_name , process_name , process_id , int03 , process_time , process_cost , campaign_name , dest_pos , int01 , float01 , float02 , float03 , float04 , float05 , float06 , float07 , float08 , float09 , float10 , float11 , float12 , float13 , float14 , float15 , float16 , float17 , float18 , float19 , float20 , int02 , int04 , int05 , int06 , int07 , int08 , int09 , int10 , string01 , string02 , string03 , string04 , string05 , string06 , string07 , string08 , string09 , string10 , string11 , string12 , string13 , string14 , string15 , string16 , string17 , string18 , string19 , string20 ) VALUES ('123', '123', '11', 1, 0, 200, 2000.0, '', '123', 123, 123.0, '123', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 123, 1, 0, '', '', '', '', '', '123', '0', '1', '0', '123', '123', '123', '', '123', '123', '123', '', '', '', '', '', '', '', '', '');", 0, 0, &errMsg); 26 } 27 sqlite3_exec(pDB, "commit transaction;", 0, 0, &errMsg); 28 sqlite3_close(pDB); 29 printf("%f seconds ", (double)(clock() - start) / CLOCKS_PER_SEC); 30 31 //关闭数据库 32 while (1); 33 return 0; 34 }
2)查询sqlite
查询需要执行回调函数
typedef int (*sqlite3_callback)( void*, /* Data provided in the 4th argument of sqlite3_exec() */ int, /* The number of columns in row */ char**, /* An array of strings representing fields in the row */ char** /* An array of strings representing column names */ );
查询代码如下
1 // sqllite2.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include "sqlite3.h" 6 #include "time.h" 7 #include <string> 8 using namespace std; 9 static int callback(void *data, int argc, char **argv, char **azColName){ 10 int i; 11 fprintf(stderr, "%s: ", (const char*)data); 12 for (i = 0; i<argc; i++){ 13 printf("%s = %s ", azColName[i], argv[i] ? argv[i] : "NULL"); 14 } 15 printf(" "); 16 return 0; 17 } 18 19 int _tmain(int argc, _TCHAR* argv[]) 20 { 21 sqlite3 *pDB; 22 char *errMsg; 23 int result = sqlite3_open("./data_test.db", &pDB); 24 int start = clock(); 25 26 int rc; 27 char *sql; 28 const char* data = "Callback function called"; 29 /* Create SQL statement */ 30 sql = "SELECT * from t_test"; 31 32 33 sqlite3_close(pDB); 34 printf("%f seconds ", (double)(clock() - start) / CLOCKS_PER_SEC); 35 36 //关闭数据库 37 while (1); 38 return 0; 39 }
3)更新
1 // sqllite2.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include "sqlite3.h" 6 #include "time.h" 7 #include <string> 8 using namespace std; 9 static int callback(void *data, int argc, char **argv, char **azColName){ 10 int i; 11 fprintf(stderr, "%s: ", (const char*)data); 12 for (i = 0; i<argc; i++){ 13 printf("%s = %s ", azColName[i], argv[i] ? argv[i] : "NULL"); 14 } 15 printf(" "); 16 return 0; 17 } 18 19 int _tmain(int argc, _TCHAR* argv[]) 20 { 21 sqlite3 *pDB; 22 char *errMsg; 23 int result = sqlite3_open("./data_test.db", &pDB); 24 int start = clock(); 25 26 27 int rc; 28 char *sql; 29 const char* data = "Callback function called"; 30 /* Create SQL statement */ 31 sql = "UPDATE t_test set string01 = '1111111111111'; SELECT * from t_test"; 32 33 sqlite3_close(pDB); 34 printf("%f seconds ", (double)(clock() - start) / CLOCKS_PER_SEC); 35 36 //关闭数据库 37 while (1); 38 return 0; 39 }