Linux环境下C++与SQLite数据库交互实战教程

发表时间: 2023-03-07 17:30

背景

公司主要做人工智能相关,包括基于机器视觉方向的图像识别,现需要做一款基于边缘计算设备的产品,需要用到sqlite数据库,下面记录一下安装过程和简单语法。

  1. 安装sqlite相关库

设备信息:ubuntu18.x系统,arm处理器,4G内存...

1.1 下载sqlite源码

wget https://www.sqlite.org/2023/sqlite-autoconf-3410000.tar.gz

下载sqlite源码

也可进入官网手动下载,下载地址:https://www.sqlite.org/download.html,注意下载名称为“sqlite-autoconf”类型的压缩包。

1.2 解压缩包

解压到当前目录:tar -zxvf sqlite-autoconf-3410000.tar.gz

进入解压目录:cd sqlite-autoconf-3410000

1.3 链接/编译

执行命令前需进入root模式,然后执行以下命令

./configure

make install

等待编译完成。

  1. 测试/sqlite简单使用

2.1 重要API介绍

参考文档:https://www.runoob.com/sqlite/sqlite-c-cpp.html

sqlite3_open(const char *filename, sqlite3 **ppDb)

作用:打开一个指向 SQLite 数据库的连接,返回一个用于其他 SQLite 操作的数据库连接对象。

sqlite3_exec(sqlite3*, const char *sql, sqlite_callback, void *data, char **errmsg)
用:提供了一个执行 SQL 命令的快捷方式,SQL 命令由 sql 参数提供,可以由多个 SQL 命令组成。

int sqlite3_exec(       sqlite3*,                                  /* 上述sqlite3_open函数打开的db对象*/       const char *sql,                           /* sql语句 */       int (*callback)(void*,int,char**,char**),  /* 回调函数 */       void *data,                                    /* 回调函数的第一个参数 */       char **errmsg                              /* 异常/异常信息 */    );

sqlite3_close(sqlite3*)

作用:关闭之前sqlite3_open() 打开的数据库连接。

2.2 简单语法介绍

连接/打开数据库并创建数据库表

#include <stdio.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;    const char* data = "Callback function called";    rc = sqlite3_open("myDemo.db", &db);    if( rc ){//打开数据库失败      fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));      sqlite3_close(db);      return(1);    }    //创建sql语句    sql = "CREATE TABLE COMPANY("  \            "ID INT PRIMARY KEY     NOT NULL," \            "NAME           TEXT    NOT NULL," \            "AGE            INT     NOT NULL," \            "ADDRESS        CHAR(50)," \            "SALARY         REAL );";    /* 执行sql */    rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);    if( rc != SQLITE_OK ){    fprintf(stderr, "SQL error(create): %s\n", zErrMsg);        sqlite3_free(zErrMsg);    }else{        fprintf(stdout, "Table created successfully\n");    }		sqlite3_close(db);    return 0;  }

插入操作

   /* 插入sql语句 */    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(insert): %s\n", zErrMsg);        sqlite3_free(zErrMsg);    }else{        fprintf(stdout, "Records created successfully\n");    }

查询操作

    /* 查询sql语句 */    sql = "SELECT * from COMPANY";    /* Execute SQL statement */    rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);    if( rc != SQLITE_OK ){        fprintf(stderr, "SQL error(select): %s\n", zErrMsg);        sqlite3_free(zErrMsg);    }else{        fprintf(stdout, "Operation(select) done successfully\n");    }

更新操作

    /* 更新sql语句 */    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(update): %s\n", zErrMsg);        sqlite3_free(zErrMsg);    }else{        fprintf(stdout, "Operation(update) done successfully\n");    }

删除操作

    /* 删除sql语句 */    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(delete): %s\n", zErrMsg);        sqlite3_free(zErrMsg);    }else{        fprintf(stdout, "Operation(delete) done successfully\n");    }

2.3 编译

执行命令:gcc sqlite_demo.cc -o sqliteDemo -l sqlite3

生成可执行文件:sqliteDemo

2.4 运行

执行:./sqliteDemo,以下为执行结果,供参考。

生成的可执行文件和数据库

数据库执行结果