The basic content of the database has been said before, and then see how to use SQLite in C language.
One interface
Sqlite3_open (const char *filename, Sqlite3 **ppdb)
Open the database, create a new database if the database does not exist, and open the
Sqlite3_close (sqlite3*)
Close the database, and if there are statements that are not finished before closing, it will returnSqlite_busy
Two examples
1 directory structure
projects{
main.c//code is located in the file
sqlite{//Download down the SQLite compressed package files directory after decompression
shell.c//This file is actually not used in the project, this file is used to generate the SQLite command tool, the specific reference: SQLite Learning Note 1
Sqlite3.c
Sqlite3.h
Sqlite3ext.h
}
}
2 Source code
Main.c#include <stdio.h> #include <stdlib.h> #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 Compiling and running
Run there are two ways, based on the previous notes, we did not configure the SQLite environment, just download the extract to get the column a folder SQLite, so you need to use the following command:
Gcc-o main main.c./SQLITE/SQLITE3.C-LPTHREAD-LDL
If the download configuration has SQLite installed, you will need to change the header file above to include:
#include <sqlite3.h>
Then execute the command:
Gcc-o main MAIN.C -lsqlite3
When the command executes, it generates an executable file called Main, entering:
./main
You can see both results.
#在编译时使用g + + will error: error:invalid conversion from ' const void* ' to ' const char* '
G++ seems to be more demanding on type conversions and does not support such conversions.
Passing the warrior, know how to compile with g++, please advise ...