c/c++两种方式操作sqlite
    		       		warning:
    		            这篇文章距离上次修改已过421天,其中的内容可能已经有所变动。
    		        
        		                
                在C/C++中操作SQLite数据库,可以使用SQLite的C API或者使用SQLite的C++接口(如SQLiteC++ Amalgamation)。
C API 示例:
#include <sqlite3.h>
 
int main() {
    sqlite3 *db;
    char *err_msg = 0;
    int rc;
 
    rc = sqlite3_open("test.db", &db);
    if (rc != SQLITE_OK) {
        // 数据库打开失败
        sqlite3_close(db);
        return 1;
    }
 
    const char *sql = "INSERT INTO Cars VALUES('Mercedes', 57, '2021-01-01')";
    rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
    if (rc != SQLITE_OK) {
        // 错误处理
        sqlite3_free(err_msg);
        sqlite3_close(db);
        return 1;
    }
 
    sqlite3_close(db); // 关闭数据库连接
    return 0;
}C++接口示例 (使用SQLiteC++ Amalgamation):
#include <sqlite3.h>
#include <iostream>
 
int main() {
    sqlite3 *db;
    sqlite3_open("test.db", &db);
 
    sqlite3_exec(db, "CREATE TABLE Cars(Name text, Age int, Date text)", 0, 0, 0);
 
    sqlite3_exec(db, "INSERT INTO Cars VALUES('Mercedes', 57, '2021-01-01')", 0, 0, 0);
 
    sqlite3_close(db);
    return 0;
}在实际应用中,你需要确保已经安装了SQLite3库,并且在编译时链接了相应的库。对于C++接口,你可能需要下载SQLiteC++ Amalgamation或者其他类似的库,并将其源码包含在你的项目中。
评论已关闭