📅  最后修改于: 2021-01-04 05:11:18             🧑  作者: Mango
在本章中,您将学习如何在C / C++程序中使用SQLite。
在我们的C / C++程序中开始使用SQLite之前,您需要确保在计算机上设置了SQLite库。您可以查看“ SQLite安装”一章以了解安装过程。
以下是重要的C / C++ SQLite接口例程,这些例程足以满足您从C / C++程序使用SQLite数据库的要求。如果您正在寻找更复杂的应用程序,则可以查看SQLite官方文档。
Sr.No. | API & Description |
---|---|
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 the 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, the first argument sqlite3 is an open database object, sqlite_callback is a call back for which data is the 1st argument and errmsg will be returned to capture any error raised by the routine. 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
#include
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));
return(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
#include
#include
static int callback(void *NotUsed, int argc, char **argv, char **azColName) {
int i;
for(i = 0; i
编译并执行上述程序后,它将在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
以下C代码段显示了如何在上述示例中创建的COMPANY表中创建记录-
#include
#include
#include
static int callback(void *NotUsed, int argc, char **argv, char **azColName) {
int i;
for(i = 0; i
编译并执行上述程序后,它将在COMPANY表中创建给定记录,并显示以下两行-
Opened database successfully
Records created successfully
在继续实际示例以获取记录之前,让我们看一下在示例中使用的有关回调函数的一些细节。该回调提供了一种从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
#include
#include
static int callback(void *data, int argc, char **argv, char **azColName){
int i;
fprintf(stderr, "%s: ", (const char*)data);
for(i = 0; i
编译并执行上述程序后,将产生以下结果。
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
以下C代码段显示了如何使用UPDATE语句更新任何记录,然后从COMPANY表中获取并显示更新的记录。
#include
#include
#include
static int callback(void *data, int argc, char **argv, char **azColName){
int i;
fprintf(stderr, "%s: ", (const char*)data);
for(i = 0; i
编译并执行上述程序后,将产生以下结果。
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
#include
#include
static int callback(void *data, int argc, char **argv, char **azColName) {
int i;
fprintf(stderr, "%s: ", (const char*)data);
for(i = 0; i
编译并执行上述程序后,将产生以下结果。
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