zoukankan      html  css  js  c++  java
  • C++使用SQLite步骤及示例

    C++使用SQLite步骤及示例
    开发环境:Windows 10+VS2013。

    开发语言:C++。

     

    1、 下载sqlite文件。

    下载网址:http://www.sqlite.org/download.html

    SQLite版本为SQLite 3.11.1,相关文件如下。
    sqlite-dll-win32-x86-3110100.zip:包含sqlite3.def、sqlite3.dll文件。
    sqlite-amalgamation-3110100.zip:包含sqlite3.h 文件。
    sqlite-tools-win32-x86-3110100.zip:包含sqlite3.exe 文件。


    2、 生成sqlite3.lib。

     sqlite-dll-win32-x86-3110100.zip文件解压到D: sqlite。
     运行Visual Studio 2013 lib命令行程序。
     依次执行控制台命令。

    1. cd D:sqlitesqlite-dll-win32-x86-3110100  
    2. D:  
    3. E:Microsoft Visual Studio 12.0VCinlib.exe /def:sqlite3.def /machine:ix86  

    即可生成sqlite3.lib文件。

    3、 创建测试数据。

     sqlite-tools-win32-x86-3110100.zip文件解压到D: sqlite。
     启动命令行,进入D: sqlite目录。
    命令依次为:

    1. cd D:sqlite  
    2. d:  

     创建test.db测试文件。
    创建user表。

    字段Code 字段类型 字段描述
    id integer 主键,自增
    name varchar(64) 用户名
    age integer 年龄

    创建命令依次如下。

    1. D:sqlite>sqlite3.exe test.db  
    2. SQLite version 3.7.13 2012-06-11 02:05:22  
    3. Enter ".help" for instructions  
    4. Enter SQL statements terminated with a ";"  
    5. sqlite> create table user  
    6.    ...> (  
    7.    ...> id integer primary key autoincrement,  
    8.    ...> name varchar(64),  
    9.    ...> age integer  
    10.    ...> );  
    11. sqlite> .quit  


    4、 创建示例工程

     创建win32控制台工程SQLiteTest。
     sqlite3.h(在sqlite-amalgamation-3071300.zip压缩包中)添加到工程。
     sqlite3.lib复制到工程文件夹下。
     工程属性中添加sqlite3.lib库依赖。
    Configuration Properties->Linker->Input->Additional Dependencies添加sqlite3.lib。
     程序代码为:

    [cpp] view plain copy
     
    1. /* 
    2. @brief 本程序测试sqlite数据库的增删改查 
    3. @date 2012-09-03 
    4. */  
    5. // SQLiteTest.cpp : Defines the entry point for the console application.  
    6. //   
    7.   
    8. #include "stdafx.h"  
    9. #include "sqlite3.h"  
    10. #include <iostream>  
    11. using namespace std;  
    12.   
    13. sqlite3 * pDB = NULL;  
    14.   
    15. //增加用户  
    16. bool AddUser(const string& sName, const string& sAge);  
    17. //删除用户  
    18. bool DeleteUser(const string& sName);  
    19. //修改用户  
    20. bool ModifyUser(const string& sName, const string& sAge);  
    21. //查找用户  
    22. bool SelectUser();  
    23.   
    24. int _tmain(int argc, _TCHAR* argv[])  
    25. {  
    26.     //打开路径采用utf-8编码  
    27.     //如果路径中包含中文,需要进行编码转换  
    28.     int nRes = sqlite3_open("D:\sqlite\test.db", &pDB);  
    29.     if (nRes != SQLITE_OK)  
    30.     {  
    31.         cout<<"Open database fail: "<<sqlite3_errmsg(pDB);  
    32.         goto QUIT;  
    33.     }  
    34.   
    35.     //添加“赵钱孙李”  
    36.     if (    !AddUser("zhao", "18")  
    37.         || !AddUser("qian", "19")  
    38.         || !AddUser("sun", "20")  
    39.         || !AddUser("li", "21"))  
    40.     {  
    41.         goto QUIT;  
    42.     }  
    43.   
    44.     //删除“赵”  
    45.     if (!DeleteUser("zhao"))  
    46.     {  
    47.         goto QUIT;  
    48.     }  
    49.   
    50.     //修改“孙”  
    51.     if (!ModifyUser("sun", "15"))  
    52.     {  
    53.         goto QUIT;  
    54.     }  
    55.   
    56.     //查找用户  
    57.     if (!SelectUser())  
    58.     {  
    59.         goto QUIT;  
    60.     }  
    61.   
    62. QUIT:  
    63.     sqlite3_close(pDB);  
    64.   
    65.     return 0;  
    66. }  
    67.   
    68. bool AddUser(const string& sName, const string& sAge)  
    69. {  
    70.     string strSql = "";  
    71.     strSql += "insert into user(name,age)";  
    72.     strSql += "values('";  
    73.     strSql += sName;  
    74.     strSql += "',";  
    75.     strSql += sAge;  
    76.     strSql += ");";  
    77.   
    78.     char* cErrMsg;  
    79.     int nRes = sqlite3_exec(pDB , strSql.c_str() ,0 ,0, &cErrMsg);  
    80.     if (nRes != SQLITE_OK)    
    81.     {  
    82.         cout<<"add user fail: "<<cErrMsg<<endl;  
    83.         return false;  
    84.     }  
    85.     else  
    86.     {  
    87.         cout<<"add user success: "<<sName.c_str()<<" "<<sAge.c_str()<<endl;  
    88.     }  
    89.   
    90.     return true;  
    91. }  
    92.   
    93. bool DeleteUser(const string& sName)  
    94. {  
    95.     string strSql = "";  
    96.     strSql += "delete from user where name='";  
    97.     strSql += sName;  
    98.     strSql += "';";  
    99.   
    100.     char* cErrMsg;  
    101.     int nRes = sqlite3_exec(pDB , strSql.c_str() ,0 ,0, &cErrMsg);  
    102.     if (nRes != SQLITE_OK)    
    103.     {  
    104.         cout<<"delete user fail: "<<cErrMsg<<endl;  
    105.         return false;  
    106.     }  
    107.     else  
    108.     {  
    109.         cout<<"delete user success: "<<sName.c_str()<<endl;  
    110.     }  
    111.   
    112.     return true;  
    113. }  
    114.   
    115. bool ModifyUser(const string& sName, const string& sAge)  
    116. {  
    117.     string strSql = "";  
    118.     strSql += "update user set age =";  
    119.     strSql += sAge;  
    120.     strSql += " where name='";  
    121.     strSql += sName;  
    122.     strSql += "';";  
    123.   
    124.     char* cErrMsg;  
    125.     int nRes = sqlite3_exec(pDB , strSql.c_str() ,0 ,0, &cErrMsg);  
    126.     if (nRes != SQLITE_OK)    
    127.     {  
    128.         cout<<"modify user fail: "<<cErrMsg<<endl;  
    129.         return false;  
    130.     }  
    131.     else  
    132.     {  
    133.         cout<<"modify user success: "<<sName.c_str()<<" "<<sAge.c_str()<<endl;  
    134.     }  
    135.   
    136.     return true;  
    137. }  
    138.   
    139. static int UserResult(void *NotUsed, int argc, char **argv, char **azColName)  
    140. {  
    141.     for(int i = 0 ; i < argc ; i++)  
    142.     {  
    143.         cout<<azColName[i]<<" = "<<(argv[i] ? argv[i] : "NULL")<<", ";  
    144.     }  
    145.     cout<<endl;  
    146.   
    147.     return 0;  
    148. }  
    149.   
    150. bool SelectUser()  
    151. {  
    152.     char* cErrMsg;  
    153.     int res = sqlite3_exec(pDB, "select * from user;", UserResult , 0 , &cErrMsg);    
    154.   
    155.     if (res != SQLITE_OK)  
    156.     {  
    157.         cout<<"select fail: "<<cErrMsg<<endl;  
    158.         return false;  
    159.     }  
    160.   
    161.     return true;  
    162. }  


     编译成功后,将sqlite3.dll复制到SQLiteTest.exe同一目录下,运行SQLiteTest.exe。
    运行结果:

    [plain] view plain copy
     
    1. add user success: zhao  18  
    2. add user success: qian  19  
    3. add user success: sun   20  
    4. add user success: li    21  
    5. delete user success: zhao  
    6. modify user success: sun        15  
    7. id = 2, name = qian, age = 19,  
    8. id = 3, name = sun, age = 15,  
    9. id = 4, name = li, age = 21,  

    5、 SQLite管理工具

    可视化管理工具,推荐使用:SQLite Expert,见:http://www.sqliteexpert.com/

     1 #include "stdafx.h"  
     2 #include <stdio.h>    
     3 #include "sqlite3.h"    
     4 #include <process.h>  
     5 #pragma comment(lib,"sqlite3.lib")    
     6   
     7 static int callback(void *NotUsed, int argc, char **argv, char **azColName){  
     8     int i;  
     9     for(i=0; i<argc; i++){  
    10         printf("%s = %s
    ", azColName[i], argv[i] ? argv[i] : "NULL");  
    11     }  
    12     printf("
    ");  
    13     return 0;  
    14 }  
    15   
    16 int _tmain(int argc, char* argv[])  
    17 {  
    18     sqlite3 *db;  
    19     char *zErrMsg = 0;  
    20     int rc;  
    21   
    22     if( argc!=2 ){  
    23         fprintf(stderr, "Usage: %s DATABASE
    ", argv[0]);  
    24         system("pause");  
    25         return(1);  
    26     }  
    27     rc = sqlite3_open(argv[1], &db);  
    28     if( rc ){  
    29         fprintf(stderr, "Can't open database: %s
    ", sqlite3_errmsg(db));  
    30         sqlite3_close(db);  
    31         return(1);  
    32     }  
    33   
    34     //char* sqlstatement = "create table test(int id,varchar name);";  
    35     //char* sqlstatement = "insert into test values(1,'hello');";  
    36     char* sqlstatement = "select * from test;";  
    37     rc = sqlite3_exec(db, sqlstatement, callback, 0, &zErrMsg);  
    38     if( rc!=SQLITE_OK ){  
    39         printf("%s
    ",argv[2]);  
    40         fprintf(stderr, "SQL error: %s
    ", zErrMsg);  
    41         sqlite3_free(zErrMsg);  
    42     }  
    43     sqlite3_close(db);  
    44   
    45     system("pause");  
    46     return 0;  
    47 } 
  • 相关阅读:
    angular2+ 使用ant.design 的 select组件时(nz-select)下拉框没有脱离文档流,直接撑开页面展示的问题
    element 获取table组件的下标
    调幅调频调相位
    Mongoose基于MongoDB建模并设置关联
    Xavier上TensorRT和Pytorch运行时间对比
    理解vue实例的生命周期和钩子函数
    [Vue]组件——.sync 修饰符实现对prop 进行“双向绑定”(子组件向父组件传值)
    vm.$attrs 【Vue 2.4.0新增inheritAttrs,attrs详解】
    (转)vue v-on修饰符
    Vue中的computed属性
  • 原文地址:https://www.cnblogs.com/hushaojun/p/5257935.html
Copyright © 2011-2022 走看看