sqllite 使用(C++)


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 Files\Microsoft Visual Studio 9.0\VC\bin
>>D:\Program Files\Microsoft Visual Studio 9.0\VC\bin> 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\n", (double)(clock() - start) / CLOCKS_PER_SEC);
30 
31     //關閉數據庫 
32     while (1);
33     return 0;
34 }
View Code

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\n", azColName[i], argv[i] ? argv[i] : "NULL");
14     }
15     printf("\n");
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\n", (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\n", azColName[i], argv[i] ? argv[i] : "NULL");
14     }
15     printf("\n");
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\n", (double)(clock() - start) / CLOCKS_PER_SEC);
35 
36     //關閉數據庫 
37     while (1);
38     return 0;
39 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM