SQLite I like

Source: Internet
Author: User
Recently, a small database was needed for the project. I found SQLite, the simplest and most convenient database I have ever seen, and I can get the original code for free, I especially admire the author of SQLite!
SQLite is: http://www.sqlite.org/download.html
I downloaded sqlite3. If you only use the simple addition, deletion, and modification functions, the following APIs are sufficient for your use!

Typedef struct sqlite3 sqlite3; // a structure that marks the Data Object
Int sqlite3_open (const char *, sqlite3 **); // open the database. The first parameter is the database file name, and the second returns the structure pointer of the opened database object.
Int sqlite3_open16 (const void *, sqlite3 **); // enable data in the same way. The difference is that the encoding method is UTF16 and the above format is utf8. Note that
Int sqlite3_close (sqlite3 *); // closes the database
Const char * sqlite3_errmsg (sqlite3 *); // gets the last operation error message. The mode without the 16 mark in the API is in utf8 format.
Const void * sqlite3_errmsg16 (sqlite3 *); // Needless to say
Int sqlite3_errcode (sqlite3 *); // This is the returned error code
Int sqlite3_exec (sqlite3 * dB, const char * zsql, sqlite3_callback xcallback, void * parg, char ** pzerrmsg); // This API is mostly used.
// Specifically, the SQL statement is executed. The third and fourth parameters are used to set the callback function. The last parameter returns an error message.

There is also a convenient API:
Int sqlite3_get_table (
Sqlite3 * dB,/* the database on which the SQL executes */
Const char * zsql,/* the SQL to be executed */
Char *** pazresult,/* write the result table here */
Int * pnrow,/* write the number of rows in the result here */
Int * pncolumn,/* write the number of columns of result here */
Char ** pzerrmsg/* write error messages here */
)
This API returns the results directly to pazresult when using SQL statement query, but you must remember to call sqlite3_free_table (pazresult) for release.
Put it!
Well, this API is enough for you to handle. If you need a deeper understanding, you just need to read its code and watch it write completely, but it's all e-text ,:)

Note: The source code I downloaded is

Sqlite-amalgamation-3_4_2.zip
(616.06 kib)
  This ZIP archive contains all preprocessed C code combined into a single source file (the amalgamation ).

This code is simple, and it is just a header file. Haha,
The following is a simple example:
// Sqlitedemo. cpp: defines the entry point of the console application.
//

# Include "stdafx. H"

Extern "C"
{
# Include "sqlite3.h"
};

// Sqlite3 callback function

// Each time SQLite finds a record, this callback is called once.

Int loadmyinfo (void * para, int n_column, char ** column_value, char ** column_name)

{

// Para is the void * parameter you pass in sqlite3_exec

// Using the Para parameter, you can pass in some special pointers (such as class pointers and structure pointers), and then forcibly convert them to the corresponding type (here it is the void * type, must be forcibly converted to your type ). Then operate on the data
// N_column indicates the number of fields in the record (that is, the number of columns in the record)
// Char ** column_value is a key value. The retrieved data is saved here. It is actually a one-dimensional array (not a two-dimensional array ), each element is a char * value and a field content (expressed as a string and ended with/0)
// Char ** column_name corresponds to column_value, indicating the field name of this field

// Here, I do not use the Para parameter. Ignore its existence.
Int I;
Printf ("the record contains % d fields/N", n_column );

For (I = 0; I <n_column; I ++)
{
Printf ("field name: % s?> Field Value: % s/n ", column_name [I], column_value [I]);
}

Printf ("------------------/N ");
Return 0;
}

Int _ tmain (INT argc, _ tchar * argv [])
{
Sqlite3 * DB = NULL; // declare SQLite key structure pointer

Int result;

// Open the database
// You need to pass in the pointer of the database, because the sqlite3_open function needs to allocate memory for this pointer, And the DB pointer must point to this memory Zone
Result = sqlite3_open ("D: // projects // demo // sqlitedemo // wwxsqlite. DB", & dB );
If (result! = Sqlite_ OK)
{
// Failed to open the database
Return-1;
}

// Database operation code
// Create a test table named mytable_1 with two fields: ID and name. ID is an automatically added type. This field can not be specified during future insert operations. It will increase from 0.
Char * errmsg = NULL;
Result = sqlite3_exec (dB, "create table mytable_2 (ID integer primary key autoincrement, name nvarchar (32), translate nvarchar (1024)", null, null, & errmsg );
If (result! = Sqlite_ OK)
{
Printf ("failed to create table, error code: % d, error cause: % s/n", result, errmsg );
}

// Insert some records
Result = sqlite3_exec (dB, "insert into mytable_2 (name, translate) values ('Walk ', 'hahaha')", 0, 0, & errmsg );
If (result! = Sqlite_ OK)
{
Printf ("insert record failed, error code: % d, error cause: % s/n", result, errmsg );
}

Result = sqlite3_exec (dB, "insert into mytable_2 (name, translate) values ('Cyclists ', 'Hello')", 0, 0, & errmsg );
If (result! = Sqlite_ OK)
{
Printf ("insert record failed, error code: % d, error cause: % s/n", result, errmsg );
}

Result = sqlite3_exec (dB, "insert into mytable_2 (name, translate) values ('chelay', 'good')", 0, 0, & errmsg );
If (result! = Sqlite_ OK)
{
Printf ("insert record failed, error code: % d, error cause: % s/n", result, & errmsg );
}

// Start querying the database. Use the callback function to query the database.
Result = sqlite3_exec (dB, "select * From mytable_2", loadmyinfo, null, & errmsg );

// Use sqlite3_get_table // to start the query. The input dbresult is a char **, and an & get address character is added here. The passed result is a char ***
Char ** dbresult;
Int nrow, ncolumn;
Int index;
Result = sqlite3_get_table (dB, "select * From mytable_2", & dbresult, & nrow, & ncolumn, & errmsg );

If (sqlite_ OK = Result)
{
// Query successful
Index = ncolumn; // the data in the first row before dbresult is the field name, which is the real data starting from the ncolumn index.
Printf ("% d records/N", nrow );

For (INT I = 0; I <nrow; I ++)
{
Printf ("% d records/N", I + 1 );
For (Int J = 0; j <ncolumn; j ++)
{
Printf ("field name: % s?> Field Value: % s/n ", dbresult [J], dbresult [Index]);
+ Index; // The Field Value of dbresult is continuous. From The 0th index to the ncolumn-1 index, the field name starts from the ncolumn index, followed by the field value, it uses a flat form to represent a two-dimensional table (traditional row-column representation ).
}
Printf ("-------/N ");
}
}

// Here, the char query result is released no matter whether the database query is successful or not, and the function provided by SQLite is used for release.
Sqlite3_free_table (dbresult );

// Close the database
Sqlite3_close (db );

System ("pause ");
Return 0;
}

The above Code was changed by someone else's code, which is very simple! Hope to be useful to you ,:)

Author: wangweixing2000 2007.8.24


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.