The basic content of the database has been mentioned earlier. Next, let's take a look at how to use sqlite in the C language. One interface: sqlite3_open (constchar * filename, sqlite3 ** ppDb) Open the database. If the database does not exist, create a new database and open sqlite3_close (sqlite3 *) to close the database.
The basic content of the database has been mentioned earlier. Next, let's take a look at how to use sqlite in the C language. One interface: sqlite3_open (const char * filename, sqlite3 ** ppDb) Open the database. If the database does not exist, create a new database and open sqlite3_close (sqlite3 *) to close the database.
The basic content of the database has been mentioned earlier. Next, let's take a look at how to use sqlite in the C language.
Interface 1
sqlite3_open(const char *filename, sqlite3 **ppDb)
Open the database. If the database does not exist, create a new database and open
sqlite3_close(sqlite3*)
Shut down the database. If you close a statement that has not been executed, SQLITE_BUSY is returned.
Example 2
1. directory structure
Projects {
Main. c // file where the code is located
Sqlite {// directory of the decompressed sqlite package downloaded from the official website
Shell. c // This file is not actually used in the project. This file is used to generate the sqlite command tool. For details, refer to: sqlite study note 1.
Sqlite3.c
Sqlite3.h
Sqlite3ext. h
}
}
2 source code
// main.c#include
#include
#include "sqlite/sqlite3.h"#define DB_NAME "hanfeng.db"int main(){ sqlite3* db = NULL ; char* msg = NULL ; int ret = 0 ; ret = sqlite3_open(DB_NAME, &db); if (ret){ fprintf(stderr, "error open datebase:%s\n.", DB_NAME) ; exit(0) ; } else{ fprintf(stderr, "successfully open datebase.\n") ; } sqlite3_close(db) ; return 0;}
3 compile and run
There are two ways to run the command. Based on the preceding notes, we didn't configure the sqlite environment. We just downloaded and decompressed the file to get a folder named sqlite. Therefore, we need to use the following command:
gcc -o main main.c ./sqlite/sqlite3.c -lpthread -ldl
If sqlite is installed in the download configuration, you need to change the header file:
#include
Then run the following command:
gcc -o main main.c -lsqlite3
After the command is executed, an executable file named main is generated. Enter:
./main
You can see the result.
# If g ++ is used during compilation, the following error occurs: error: invalid conversion from 'const void * 'to 'const char *'
G ++ seems to have stricter requirements on type conversion and does not support such conversion.
If you know how to compile with g ++, please advise ......