1, open the database:
Note: Open a database, the filename does not necessarily exist, if this file does not exist, SQLite will be created automatically. The first parameter refers to the file name, and the second parameter is the defined sqlite3 * * struct pointer (the key data structure), the underlying detail of the structure, and you don't have to worry about it.
int Sqlite3_open (
const char *filename, /* Database filename (UTF-8) * *
Sqlite3 **ppdb/* Out:sqlite DB handle * *
);
Return value: Indicates whether the exercise is correct (SQLITE_OK operation is normal)
2. Close the database:
Note: If you open a database with Sqlite3_open, do not forget to close the database with this function at the end.
int Sqlite3_close (sqlite3*); parameter is just the structure, which is the database handle
Note After this error, you can use SQLITE3_ERRMSG (db) to get the error code
3. Execute SQL statement:
Description: This function is to execute one or more SQL statements, separated by the ";" between the SQL statements. It is recommended that you specify a third parameter callback function when executing one or more SQL statements, that you can get a detailed procedure for executing SQL in a callback function, or that you should return 0 if all SQL execution is completed, otherwise this execution is not completely successful. Fifth parameter: If execution fails (no return 0), you can view the fifth stated value. To view detailed error messages.
int Sqlite3_exec (
sqlite3*,/* already open database handle * *
const char *SQL,/* SQL statement to execute * *
Sqlite_callback,//callback function * *
void *,/* parameters passed to the callback function * *
Char **errmsg/* Save the error message, pay attention to Sqlite3_free (errmsg) to release the resource * *
);
Typically, both sqlite3_callback and subsequent void* can be filled with NULL, indicating that no callback is required. For example, you do insert,update,delete operations, there is no need to use callbacks. As a SELECT, you use callbacks, because sqlite3 the data, you have to tell you what data is detected by the callback.
4, Exec's callback
Type def int (*sqlite3_callback) (void* data, int col_count, char** col_value, char** col_name);
Description: Your callback function must be defined as the type of the function above.
Select_callback is the callback function for select
@data: is the fourth parameter in Sqlite3_exec ()
@col_count: Number of Selected columns
@col_values: The value of each column in each record
@col_Name: column names for each column
static int Select_callback (void * data, int col_count, char * * col_values, char * * col_name)
#include ". /sqlite3_lib/sqlite3.h "//header file
#pragma comment (lib, "...). /sqlite3_lib/sqlite3.lib ")//lib Static library
Select_callback is the callback function for select
@data: is the fourth parameter in Sqlite3_exec ()
@col_count: Number of Selected columns
@col_values: The value of each column in each record
@col_Name: column names for each column
static int Select_callback (void * notused, int argc, char * * values, char * * colname)
{
Each record recalls the function once, how many times it is recalled
int i;
for (i=0 i < argc; i++)
{
printf ("%s =%s\n", Colname[i], values[i] = = 0? "NUL": Values[i]);
}
return 0;
}
int main (int argc, char * argv[])
{
const char * sSQL1 = "CREATE TABLE users" (userid varchar () PRIMARY KEY, age int, birthday datetime); ";
char * perrmsg = 0;
Connecting to a database
Sqlite3 * db = 0;
int ret = Sqlite3_open ("./test.db", &db);
if (ret!= SQLITE_OK)
{
fprintf (stderr, "Cannot Open database:%s", sqlite3_errmsg (db));
return (1);
}
printf ("Database connection succeeded!\n");
Execute the Build table SQL
Sqlite3_exec (db, sSQL1, 0, 0, &perrmsg);
if (ret!= SQLITE_OK)
{
fprintf (stderr, "SQL Error:%s\n", perrmsg);
Sqlite3_free (PERRMSG);
}
Execute INSERT Record SQL
Sqlite3_exec (db, "insert into users values" (' John ', M, ' 2001-5-4 '); ", 0, 0, &perrmsg);
Sqlite3_exec (db, "insert into users values" (' Dick ', M, ' 1970-5-4 '); ", 0, 0, &perrmsg);
Querying data tables
Sqlite3_exec (DB, "select * from Users;", Select_callback, 0, &perrmsg);
Close Database
Sqlite3_close (DB);
db = 0;
return 0;
}
5, take the current insertion position:
Long Long int sqlite3_last_insert_rowid (sqlite3*);
Function: Return to your previous insertion position, starting at 1, sqlite3* the handle to open the database for you.
6, non-callback select query: &NBSP
Feature: Execute Query Sql and return a recordset. &NBSP
Int sqlite3_get_table (
sqlite3*, /* the database handle */ is already open;
const char *sql, /* The Sql statement to be executed */
char ***resultp, /* Saves a pointer */ that returns a recordset;