• 【SQLite】教程07-C/C++上使用SQLite3


    1、配置好C/C++项目环境

    2.源码

      1 #include <iostream>
      2 #include <vector>
      3 #include <string>
      4 #include "sqlite3.h"
      5 using namespace std;
      6 
      7 //NotUsed:sqlite3_exec()的第4个参数提供的数据
      8 //argc:字段数量
      9 //argv:元素内容的数组
     10 //azColName:字段名称的数组
     11 static int callback(void *NotUsed, int argc, char **argv, char **azColName)
     12 {
     13     for (int i = 0; i < argc; i++)
     14     {
     15         //printf("azColName=%s
    ", azColName[i]);
     16         //printf("argv=%s
    ", argv[i] ? argv[i] : "NULL");
     17         printf("%s=%s
    ", azColName[i], argv[i] ? argv[i] : "NULL");
     18     }
     19     printf("
    ");
     20     return 0;
     21 }
     22 
     23 int main()
     24 {
     25     //------------------------------------------------------------创建、打开数据库----------------------------------------------------------------
     26     sqlite3 *db = NULL; // 一个打开的数据库实例
     27     const char * path = "..\Test-SQLite\Test.db";
     28     int result = sqlite3_open(path, &db);
     29     if (result == SQLITE_OK)
     30     {
     31         std::clog << "打开数据库连接成功!" << endl;
     32     }
     33     else
     34     {
     35         std::clog << "打开数据库连接失败!" << endl;
     36         exit(0);
     37     }
     38     //------------------------------------------------------------创建表----------------------------------------------------------------
     39     char *sql;
     40     char *zErrMsg = 0;
     41     sql = "CREATE TABLE COMPANY("  
     42         "ID INT PRIMARY KEY     NOT NULL," 
     43         "NAME           TEXT    NOT NULL," 
     44         "AGE            INT     NOT NULL," 
     45         "ADDRESS        CHAR(50)," 
     46         "SALARY         REAL );";
     47 
     48     int rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
     49 
     50     if (rc != SQLITE_OK)
     51     {
     52         fprintf(stderr, "SQL error: %s
    ", zErrMsg);
     53         sqlite3_free(zErrMsg);
     54     }
     55     else
     56     {
     57         fprintf(stdout, "表格创建成功!
    ");
     58     }
     59     //--------------------------------------------------------------添加记录--------------------------------------------------------------------
     60     sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "  
     61         "VALUES (1, 'Paul', 32, 'California', 20000.00 ); " 
     62         "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "  
     63         "VALUES (2, 'Allen', 25, 'Texas', 15000.00 ); "     
     64         "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)" 
     65         "VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );" 
     66         "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)" 
     67         "VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );";
     68     rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
     69     if (rc != SQLITE_OK)
     70     {
     71         fprintf(stderr, "SQL error: %s
    ", zErrMsg);
     72         sqlite3_free(zErrMsg);
     73     }
     74     else
     75     {
     76         fprintf(stdout, "记录创建成功!
    ");
     77     }
     78     //--------------------------------------------------------------查询表--------------------------------------------------------------------
     79     sql = "SELECT * from COMPANY";
     80     //处理每个记录将会用到这个回调callback
     81     rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
     82 
     83     if (rc != SQLITE_OK)
     84     {
     85         fprintf(stderr, "SQL error: %s
    ", zErrMsg);
     86         sqlite3_free(zErrMsg);
     87     }
     88     else
     89     {
     90         fprintf(stdout, "查询操作成功!
    ");
     91     }
     92     //--------------------------------------------------------------更新表--------------------------------------------------------------------
     93     sql = "UPDATE COMPANY set SALARY = 88888.00 where ID=1; " 
     94         "SELECT * from COMPANY";
     95     rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
     96     if (rc != SQLITE_OK)
     97     {
     98         fprintf(stderr, "SQL error: %s
    ", zErrMsg);
     99         sqlite3_free(zErrMsg);
    100     }
    101     else
    102     {
    103         fprintf(stdout, "更新表格成功!
    ");
    104     }
    105     //--------------------------------------------------------------查询表 ID=1--------------------------------------------------------------------
    106     sql = "SELECT * from COMPANY where ID=1";
    107     //处理每个记录将会用到这个回调callback
    108     rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
    109 
    110     if (rc != SQLITE_OK)
    111     {
    112         fprintf(stderr, "SQL error: %s
    ", zErrMsg);
    113         sqlite3_free(zErrMsg);
    114     }
    115     else
    116     {
    117         fprintf(stdout, "查询操作成功!
    ");
    118     }
    119     //--------------------------------------------------------------删除记录 ID=2--------------------------------------------------------------------
    120     sql = "DELETE from COMPANY where ID=2; " 
    121         "SELECT * from COMPANY";
    122     rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
    123     if (rc != SQLITE_OK)
    124     {
    125         fprintf(stderr, "SQL error: %s
    ", zErrMsg);
    126         sqlite3_free(zErrMsg);
    127     }
    128     else {
    129         fprintf(stdout, "删除记录成功!
    ");
    130     }
    131     //--------------------------------------------------------------查询表--------------------------------------------------------------------
    132     sql = "SELECT * from COMPANY";
    133     //处理每个记录将会用到这个回调callback
    134     rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
    135 
    136     if (rc != SQLITE_OK)
    137     {
    138         fprintf(stderr, "SQL error: %s
    ", zErrMsg);
    139         sqlite3_free(zErrMsg);
    140     }
    141     else
    142     {
    143         fprintf(stdout, "查询操作成功!
    ");
    144     }
    145     //--------------------------------------------------------------关闭数据库--------------------------------------------------------------------
    146     sqlite3_close(db);
    147 
    148     system("pause");
    149     return 0;
    150 }

     结果:

     

    4、编码转换

    Sqlite的编码默认为UTF-8编码,而vc++工程中所编写的SQL语句,一般是Unciode。由于编码不一致会导致乱码,下面这段程序可以解决:

    1).sql语句由GB2312转为UTF-8再用sqlite3_exec()查询

    2).查询结果在callback()中由UTF-8转为GB2312再输出

    下面是编码转换函数:

    //UTF-8到GB2312的转换
    char* U2G_(const char* utf8)
    {
        int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
        wchar_t* wstr = new wchar_t[len + 1];
        memset(wstr, 0, len + 1);
        MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
        len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
        char* str = new char[len + 1];
        memset(str, 0, len + 1);
        WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
        if (wstr) delete[] wstr;
        return str;
    }
    string U2G(string strutf8)
    {
        char cUTF8[500];
        sprintf(cUTF8, "%s", strutf8.c_str());
        string strGB2312;
        strGB2312 = U2G_(cUTF8);
        return strGB2312;
    }
    int Tools_Use_Record(string strToolName);
    //GB2312到UTF-8的转换
    char* G2U_(const char* gb2312)
    {
        int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
        wchar_t* wstr = new wchar_t[len + 1];
        memset(wstr, 0, len + 1);
        MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
        len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
        char* str = new char[len + 1];
        memset(str, 0, len + 1);
        WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
        if (wstr) delete[] wstr;
        return str;
    }
    string G2U(string strGB2312)
    {
        char cGB2312[500];
        sprintf(cGB2312, "%s", strGB2312.c_str());
        string strutf8;
        strutf8 = G2U_(cGB2312);
        return strutf8;
    }

    5、查询注意

    以INTEGER作为条件用=

    select * from 表名 where 序号=55

    以TEXT作为条件用like 

    select * from 表名 where 姓名 like '里海'

    下面这种写法会出错

    select * from 表名 where 姓名='里海'
  • 相关阅读:
    C#和sqlserver中生成新的32位GUID
    IIS7下swfupload上传大文件出现404错误
    jQuery 判断是否为数字的方法 及 转换数字函数
    js数组与字符串的相互转换方法
    jquery 中如何将数组转化为json字符串,然后再转化回来?
    Firemonkey Android 虚拟机
    Eclipse apk 签名
    win10 修改hosts
    eclipse 预览Android界面报错
    夜神模拟器
  • 原文地址:https://www.cnblogs.com/KMould/p/13441645.html
Copyright © 2020-2023  润新知