SQLite C/C++
安装
在我们开始之前,我们需要在我们的C/ C + +程序中使用SQLite,以确保我们有SQLite库设立在机器上。可以查看SQLite的安装章节,以了解安装过程。
C/C++ 接口API
以下是重要的C&C++/ SQLite 的接口程序就足够SQLite数据库从C/ C + +程序的使用需求。如果正在寻找一个更复杂的应用程序,那么可以看到 SQLite 的官方文档。
S.N. | API & 描述 |
---|---|
1 |
sqlite3_open(const char *filename, sqlite3 **ppDb) This routine opens a connection to an SQLite database file and returns a database connection object to be used by other SQLite routines. If the filename argument is NULL or ':memory:', sqlite3_open() will create an in-memory database in RAM that lasts only for the duration of the session. If filename is not NULL, sqlite3_open() attempts to open the database file by using its value. If no file by that name exists, sqlite3_open() will open a new database file by that name. |
2 |
sqlite3_exec(sqlite3*, const char *sql, sqlite_callback, void *data, char **errmsg) This routine provides a quick, easy way to execute SQL commands provided by sql argument which can consist of more than one SQL command. Here, first argument sqlite3 is open database object, sqlite_callback is a call back for whichdata is the 1st argument and errmsg will be return to capture any error raised by the routine. The sqlite3_exec() routine parses and executes every command given in the sql argument until it reaches the end of the string or encounters an error. |
3 |
sqlite3_close(sqlite3*) This routine closes a database connection previously opened by a call to sqlite3_open(). All prepared statements associated with the connection should be finalized prior to closing the connection. If any queries remain that have not been finalized, sqlite3_close() will return SQLITE_BUSY with the error message Unable to close due to unfinalized statements. |
连接到数据库
下面的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", sqlite3_errmsg(db)); exit(0); }else{ fprintf(stderr, "Opened database successfully"); } 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", azColName[i], argv[i] ? argv[i] : "NULL"); } printf(""); 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", sqlite3_errmsg(db)); exit(0); }else{ fprintf(stdout, "Opened database successfully"); } /* 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", zErrMsg); sqlite3_free(zErrMsg); }else{ fprintf(stdout, "Table created successfully"); } 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", azColName[i], argv[i] ? argv[i] : "NULL"); } printf(""); 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", sqlite3_errmsg(db)); exit(0); }else{ fprintf(stderr, "Opened database successfully"); } /* 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", zErrMsg); sqlite3_free(zErrMsg); }else{ fprintf(stdout, "Records created successfully"); } 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将调用这个回调函数中处理的每个记录在每个执行SELECT语句在SQL参数。
下面的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", azColName[i], argv[i] ? argv[i] : "NULL"); } printf(""); 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", sqlite3_errmsg(db)); exit(0); }else{ fprintf(stderr, "Opened database successfully"); } /* 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", zErrMsg); sqlite3_free(zErrMsg); }else{ fprintf(stdout, "Operation done successfully"); } 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", azColName[i], argv[i] ? argv[i] : "NULL"); } printf(""); 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", sqlite3_errmsg(db)); exit(0); }else{ fprintf(stderr, "Opened database successfully"); } /* 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", zErrMsg); sqlite3_free(zErrMsg); }else{ fprintf(stdout, "Operation done successfully"); } 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
删除操作
下面的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", azColName[i], argv[i] ? argv[i] : "NULL"); } printf(""); 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", sqlite3_errmsg(db)); exit(0); }else{ fprintf(stderr, "Opened database successfully"); } /* 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", zErrMsg); sqlite3_free(zErrMsg); }else{ fprintf(stdout, "Operation done successfully"); } 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
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动共创优秀实例教程
转载请注明:文章转载自:代码驿站 [http:/www.codeinn.net]
本文标题:SQLite C/C++
本文地址:http://www.codeinn.net/sqlite/630.html