SQLite – C/C++

扫码关注公众号:Java 技术驿站

发送:vip
将链接复制到本浏览器,永久解锁本站全部文章

【公众号:Java 技术驿站】 【加作者微信交流技术,拉技术群】

SQLite - C/C++

安装

在 C/C++ 程序中使用 SQLite 之前,我们需要确保机器上已经有 SQLite 库。可以查看 SQLite 安装章节了解安装过程。

C/C++ 接口 API

以下是重要的 C&C++ / SQLite 接口程序,可以满足您在 C/C++ 程序中使用 SQLite 数据库的需求。如果您需要了解更多细节,请查看 SQLite 官方文档。

序号 API&描述
1 sqlite3_open(constchar*filename,sqlite3**ppDb)该例程打开一个指向SQLite数据库文件的连接,返回一个用于其他SQLite程序的数据库连接对象。如果filename参数是NULL或':memory:',那么sqlite3_open()将会在RAM中创建一个内存数据库,这只会在session的有效时间内持续。如果文件名filename不为NULL,那么sqlite3_open()将使用这个参数值尝试打开数据库文件。如果该名称的文件不存在,sqlite3_open()将创建一个新的命名为该名称的数据库文件并打开。
2 sqlite3_exec(sqlite3,constcharsql,sqlite_callback,void*data,char**errmsg)该例程提供了一个执行SQL命令的快捷方式,SQL命令由sql参数提供,可以由多个SQL命令组成。在这里,第一个参数sqlite3是打开的数据库对象,sqlite_callback是一个回调,data作为其第一个参数,errmsg将被返回用来获取程序生成的任何错误。sqlite3_exec()程序解析并执行由sql参数所给的每个命令,直到字符串结束或者遇到错误为止。
3 sqlite3_close(sqlite3*)该例程关闭之前调用sqlite3_open()打开的数据库连接。所有与连接相关的语句都应在连接关闭之前完成。如果还有查询没有完成,sqlite3_close()将返回SQLITE_BUSY禁止关闭的错误消息。

连接数据库

下面的 C 代码段显示了如何连接到一个现有的数据库。如果数据库不存在,那么它就会被创建,最后将返回一个数据库对象。

    #include <stdio.h>
    #include <sqlite3.h>

    int main(int argc, char* argv[])
    {
       sqlite3 *db;
       char *zErrMsg = 0;
       int rc;

       rc = sqlite3_open("test.db", &db);

       if( rc ){
          fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
          exit(0);
       }else{
          fprintf(stderr, "Opened database successfully\n");
       }
       sqlite3_close(db);
    }

现在,让我们来编译和运行上面的程序,在当前目录中创建我们的数据库 test.db。您可以根据需要改变路径。

    $gcc test.c -l sqlite3
    $./a.out
    Opened database successfully

如果要使用 C++ 源代码,可以按照下列所示编译代码:

    $g++ test.c -l sqlite3

在这里,把我们的程序链接上 sqlite3 库,以便向 C 程序提供必要的函数。这将在您的目录下创建一个数据库文件 test.db,您将得到如下结果:

    -rwxr-xr-x. 1 root root 7383 May  8 02:06 a.out
    -rw-r--r--. 1 root root  323 May  8 02:05 test.c
    -rw-r--r--. 1 root root    0 May  8 02:06 test.db

创建表

下面的 C 代码段将用于在先前创建的数据库中创建一个表:

    #include <stdio.h>
    #include <stdlib.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 = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
       }
       printf("\n");
       return 0;
    }

    int main(int argc, char* argv[])
    {
       sqlite3 *db;
       char *zErrMsg = 0;
       int  rc;
       char *sql;

       /* Open database */
       rc = sqlite3_open("test.db", &db);
       if( rc ){
          fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
          exit(0);
       }else{
          fprintf(stdout, "Opened database successfully\n");
       }

       /* Create SQL statement */
       sql = "CREATE TABLE COMPANY("  \
             "ID INT PRIMARY KEY     NOT NULL," \
             "NAME           TEXT    NOT NULL," \
             "AGE            INT     NOT NULL," \
             "ADDRESS        CHAR(50)," \
             "SALARY         REAL );";

       /* Execute SQL statement */
       rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
       if( rc != SQLITE_OK ){
       fprintf(stderr, "SQL error: %s\n", zErrMsg);
          sqlite3_free(zErrMsg);
       }else{
          fprintf(stdout, "Table created successfully\n");
       }
       sqlite3_close(db);
       return 0;
    }

上述程序编译和执行时,它会在 test.db 文件中创建 COMPANY 表,最终文件列表如下所示:

    -rwxr-xr-x. 1 root root 9567 May  8 02:31 a.out
    -rw-r--r--. 1 root root 1207 May  8 02:31 test.c
    -rw-r--r--. 1 root root 3072 May  8 02:31 test.db

INSERT 操作

下面的 C 代码段显示了如何在上面创建的 COMPANY 表中创建记录:

    #include <stdio.h>
    #include <stdlib.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 = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
       }
       printf("\n");
       return 0;
    }

    int main(int argc, char* argv[])
    {
       sqlite3 *db;
       char *zErrMsg = 0;
       int rc;
       char *sql;

       /* Open database */
       rc = sqlite3_open("test.db", &db);
       if( rc ){
          fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
          exit(0);
       }else{
          fprintf(stderr, "Opened database successfully\n");
       }

       /* Create SQL statement */
       sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "  \
             "VALUES (1, 'Paul', 32, 'California', 20000.00 ); " \
             "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "  \
             "VALUES (2, 'Allen', 25, 'Texas', 15000.00 ); "     \
             "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)" \
             "VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );" \
             "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)" \
             "VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );";

       /* Execute SQL statement */
       rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
       if( rc != SQLITE_OK ){
          fprintf(stderr, "SQL error: %s\n", zErrMsg);
          sqlite3_free(zErrMsg);
       }else{
          fprintf(stdout, "Records created successfully\n");
       }
       sqlite3_close(db);
       return 0;
    }

上述程序编译和执行时,它会在 COMPANY 表中创建给定记录,并会显示以下两行:

    Opened database successfully
    Records created successfully

SELECT 操作

在我们开始讲解获取记录的实例之前,让我们先了解下回调函数的一些细节,这将在我们的实例使用到。这个回调提供了一个从 SELECT 语句获得结果的方式。它声明如下:

    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 */
    );

如果上面的回调在 sqlite_exec() 程序中作为第三个参数,那么 SQLite 将为 SQL 参数内执行的每个 SELECT 语句中处理的每个记录调用这个回调函数。

下面的 C 代码段显示了如何从前面创建的 COMPANY 表中获取并显示记录:

    #include <stdio.h>
    #include <stdlib.h>
    #include <sqlite3.h>

    static int callback(void *data, int argc, char **argv, char **azColName){
       int i;
       fprintf(stderr, "%s: ", (const char*)data);
       for(i=0; i<argc; i++){
          printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
       }
       printf("\n");
       return 0;
    }

    int main(int argc, char* argv[])
    {
       sqlite3 *db;
       char *zErrMsg = 0;
       int rc;
       char *sql;
       const char* data = "Callback function called";

       /* Open database */
       rc = sqlite3_open("test.db", &db);
       if( rc ){
          fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
          exit(0);
       }else{
          fprintf(stderr, "Opened database successfully\n");
       }

       /* Create SQL statement */
       sql = "SELECT * from COMPANY";

       /* Execute SQL statement */
       rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
       if( rc != SQLITE_OK ){
          fprintf(stderr, "SQL error: %s\n", zErrMsg);
          sqlite3_free(zErrMsg);
       }else{
          fprintf(stdout, "Operation done successfully\n");
       }
       sqlite3_close(db);
       return 0;
    }

上述程序编译和执行时,它会产生以下结果:

    Opened database successfully
    Callback function called: ID = 1
    NAME = Paul
    AGE = 32
    ADDRESS = California
    SALARY = 20000.0

    Callback function called: ID = 2
    NAME = Allen
    AGE = 25
    ADDRESS = Texas
    SALARY = 15000.0

    Callback function called: ID = 3
    NAME = Teddy
    AGE = 23
    ADDRESS = Norway
    SALARY = 20000.0

    Callback function called: ID = 4
    NAME = Mark
    AGE = 25
    ADDRESS = Rich-Mond
    SALARY = 65000.0

    Operation done successfully

UPDATE 操作

下面的 C 代码段显示了如何使用 UPDATE 语句来更新任何记录,然后从 COMPANY 表中获取并显示更新的记录:

    #include <stdio.h>
    #include <stdlib.h>
    #include <sqlite3.h> 

    static int callback(void *data, int argc, char **argv, char **azColName){
       int i;
       fprintf(stderr, "%s: ", (const char*)data);
       for(i=0; i<argc; i++){
          printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
       }
       printf("\n");
       return 0;
    }

    int main(int argc, char* argv[])
    {
       sqlite3 *db;
       char *zErrMsg = 0;
       int rc;
       char *sql;
       const char* data = "Callback function called";

       /* Open database */
       rc = sqlite3_open("test.db", &db);
       if( rc ){
          fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
          exit(0);
       }else{
          fprintf(stderr, "Opened database successfully\n");
       }

       /* Create merged SQL statement */
       sql = "UPDATE COMPANY set SALARY = 25000.00 where ID=1; " \
             "SELECT * from COMPANY";

       /* Execute SQL statement */
       rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
       if( rc != SQLITE_OK ){
          fprintf(stderr, "SQL error: %s\n", zErrMsg);
          sqlite3_free(zErrMsg);
       }else{
          fprintf(stdout, "Operation done successfully\n");
       }
       sqlite3_close(db);
       return 0;
    }

上述程序编译和执行时,它会产生以下结果:

    Opened database successfully
    Callback function called: ID = 1
    NAME = Paul
    AGE = 32
    ADDRESS = California
    SALARY = 25000.0

    Callback function called: ID = 2
    NAME = Allen
    AGE = 25
    ADDRESS = Texas
    SALARY = 15000.0

    Callback function called: ID = 3
    NAME = Teddy
    AGE = 23
    ADDRESS = Norway
    SALARY = 20000.0

    Callback function called: ID = 4
    NAME = Mark
    AGE = 25
    ADDRESS = Rich-Mond
    SALARY = 65000.0

    Operation done successfully

DELETE 操作

下面的 C 代码段显示了如何使用 DELETE 语句删除任何记录,然后从 COMPANY 表中获取并显示剩余的记录:

    #include <stdio.h>
    #include <stdlib.h>
    #include <sqlite3.h> 

    static int callback(void *data, int argc, char **argv, char **azColName){
       int i;
       fprintf(stderr, "%s: ", (const char*)data);
       for(i=0; i<argc; i++){       printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");    }    printf("\n");    return 0; }  int main(int argc, char* argv[]) {    sqlite3 *db;    char *zErrMsg = 0;    int rc;    char *sql;    const char* data = "Callback function called";     /* Open database */    rc = sqlite3_open("test.db", &db);    if( rc ){       fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));       exit(0);    }else{       fprintf(stderr, "Opened database successfully\n");    }     /* Create merged SQL statement */    sql = "DELETE from COMPANY where ID=2; " \          "SELECT * from COMPANY";     /* Execute SQL statement */    rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);    if( rc != SQLITE_OK ){       fprintf(stderr, "SQL error: %s\n", zErrMsg);       sqlite3_free(zErrMsg);    }else{       fprintf(stdout, "Operation done successfully\n");    }    sqlite3_close(db);    return 0; } 

上述程序编译和执行时,它会产生以下结果:

    Opened database successfully
    Callback function called: ID = 1
    NAME = Paul
    AGE = 32
    ADDRESS = California
    SALARY = 20000.0

    Callback function called: ID = 3
    NAME = Teddy
    AGE = 23
    ADDRESS = Norway
    SALARY = 20000.0

    Callback function called: ID = 4
    NAME = Mark
    AGE = 25
    ADDRESS = Rich-Mond
    SALARY = 65000.0

    Operation done successfully
赞(0) 打赏
版权归原创作者所有,任何形式的转载请联系博主:daming_90:Java 技术驿站 » SQLite – C/C++

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏