SQLite is a lightweight database and an acid-compliant associated database management system. It is designed to be embedded and has been used in many embedded products, it occupies very low resources. In embedded devices, it may only need several hundred KB of memory. It supports mainstream operating systems such as Windows, Linux, and UNIX, and can be combined with many programming languages, such
TCL, C #, PHP, Java, and ODBC interfaces are also faster than MySQL and PostgreSQL, two world-renowned open-source database management systems.
# Include <stdio. h>
# Include <sqlite3.h>
# Include <stdlib. h>
# DEFINE _ debug _
Int main (void)
{
Sqlite3 * DB = NULL; // declare SQLite key structure pointer
Char * zerrmsg = 0;
Int RC;
Rc = sqlite3_open ("/home/Xue/DB/person", & dB); // terminal to this directory, create a table using shell
If (RC! = Sqlite_ OK ){
Fprintf (stderr, "can't open database: % s \ n", sqlite3_errmsg (db ));
Sqlite3_close (db );
Exit (1 );
}
Else printf ("successfully! \ Ncongratulation! Have fun! \ N ");
Int ROW = 1, column = 1;
Char ** result;
Char * SQL = "select * From per ";
Sqlite3_get_table (dB, SQL, & result, & Row, & column, & zerrmsg );
Int I = 0;
Printf ("row: % d column: % d \ n", row, column );
Printf ("\ nthe result of querying is: \ n ");
// Output the queried data
For (I = 0; I <(row + 1) * column; I ++)
Printf ("result [% d] = % s \ n", I, result [I]);
Sqlite3_free_table (result); // release the memory space of result
# Ifdef _ debug _ // If _ debug _ has been defined earlier, the information of the created table is output.
Printf ("zerrmsg = % s \ n", zerrmsg); // retain the error message. If it is null, no error message is returned during execution.
# Endif
Sqlite3_close (db); // close the database
Return 0;
}
// The above code has been debugged;
# Include <stdio. h>
# Include <sqlite3.h>
# Include <stdlib. h>
# DEFINE _ debug _
Int main (void)
{
Sqlite3 * DB = NULL; // declare SQLite key structure pointer
Char * zerrmsg = 0;
Int RC;
// Open or create a database file
Rc = sqlite3_open ("person", & dB); // open the specified database file. If the specified database file does not exist, a database file with the same name will be created. You need to input the pointer of the database pointer, because sqlite3_open functions need to allocate memory for these pointers so that the DB Pointer Points to the memory zone.
If (RC! = Sqlite_ OK) {// or RC
Fprintf (stderr, "can't open database: % s \ n", sqlite3_errmsg (db ));
Sqlite3_close (db );
Exit (1); // open failed, exit
}
Else printf ("You have opened a sqlite3 database named query. dbsuccessfully! \ Ncongratulation! Have fun! \ N ");
/* // Create a table
Char * SQL = "CREATE TABLE sensordata (ID integer primarykey, sensorid integer, sitenum integer, name varchar (12), sensorparamterreal );";
Sqlite3_exec (dB, SQL, 0, 0, & zerrmsg );
# Ifdef _ debug _ // If _ debug _ has been defined earlier, the information of the created table is output.
Printf ("% s \ n", zerrmsg );
# Endif
// Insert data
SQL = "insert into \" sensordata \ "values (null, 200708081008, '000000', 18.9);"; // input content to the table
Sqlite3_exec (dB, SQL, 0, 0, & zerrmsg );
SQL = "insertinto \" sensordata \ "values (null, 200708081010, '000000', 16.4 );";
Sqlite3_exec (dB, SQL, 0, 0, & zerrmsg );*/
Int ROW = 0, column = 0; // used to record the number of rows and columns in the following result set
Char ** result; // a two-dimensional array is used to store results.
// Query data
Char * SQL = "select * From per"; // query statement. In C, the corresponding function interface is sqlite3_get_table (dB, SQL, & result, & Row, & column, & zerrmsg)
Sqlite3_get_table (dB, SQL, & result, & Row, & column, & zerrmsg); // result: stores the data to be queried in an array. The first is the table name, then the data
Int I = 0;
Printf ("row: % d column: % d \ n", row, column );
Printf ("\ nthe result of querying is: \ n ");
// Output the queried data
For (I = 0; I <(row + 1) * column; I ++)
Printf ("result [% d] = % s \ n", I, result [I]);
Sqlite3_free_table (result); // release the memory space of result
# Ifdef _ debug _ // If _ debug _ has been defined earlier, the information of the created table is output.
Printf ("zerrmsg = % s \ n", zerrmsg); // retain the error message. If it is null, no error message is returned during execution.
# Endif
Sqlite3_close (db); // close the database
Return 0;
}