Recommendation of common SQLite Functions

Source: Internet
Author: User
Tags sql error sqlite db

1. Open the database:
Note: To open a database, the file name does not have to exist. If the file does not exist, SQLite will be automatically created. The first parameter refers to the file name, and the second parameter is the defined sqlite3 ** struct pointer (key data structure). You don't need to worry about the underlying details of this structure.
Int sqlite3_open (
Const char * filename,/* database filename (UTF-8 )*/
Sqlite3 ** ppdb/* Out: SQLite dB handle */
);
Returned value: indicates whether the operation is correct (sqlite_ OK is normal)

2. Shut down the database:
Note: If you enable a database with sqlite3_open, do not forget to use this function to close the database at the end.
Int sqlite3_close (sqlite3 *); // The parameter is the struct, that is, the database handle.

3. Execute an SQL statement:
Note: This function is used to execute one or more SQL statements, separated by the ";" sign. We recommend that you specify the third parameter callback function when executing one or more SQL statements. In the callback function, you can obtain detailed procedures for executing SQL statements, if all SQL statements are executed, 0 is returned. Otherwise, the execution is not completely successful. Fifth parameter: if the execution fails (no 0 is returned), you can view the fifth elaborated value. To view detailed error information.
int sqlite3_exec (
sqlite3 *,/* opened database handle */
const char * SQL, /* the SQL statement to be executed */
sqlite_callback,/* callback function */
void *, /* parameters passed to the callback function */
char ** errmsg/* Save the error message */
);
sqlite3_callback and void * after sqlite3_callback can both be null, indicating that callback is not required. For example, if you perform insert and delete operations, you do not need to use callback. When used as a SELECT statement, a callback is required, because sqlite3 checks the data and uses the callback to tell you what data has been found.

4. Exec callback
Typedef int (* sqlite3_callback) (void *, Int, char **, char **);
Note: Your callback function must be defined as the type of the above function.
For example:
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

5. Take the current insert position:
Function: return the last time you inserted the data. Starting from 1, sqlite3 * opens the database for you to get the handle.
Long long int sqlite3_last_insert_rowid (sqlite3 *);
6. Non-callback SELECT query:
Function: execute a query SQL statement and return a record set.
Int sqlite3_get_table (
Sqlite3 *,/* opened database handle */
Const char * SQL,/* SQL statement to be executed */
Char *** resultp,/* Save the pointer to the returned record set */
Int * nrow,/* Number of records returned (and the number of rows identified )*/
Int * ncolumn,/* Number of returned fields (How many columns )*/
Char ** errmsg/* error message returned */
)
Note: The third parameter is the query result, which is a one-dimensional array. The memory layout is: the first line is the field name, followed by the value of each field.
Instance:
Int main (INT, char **)
{
Sqlite3 * dB;
Int result;
Char * errmsg = NULL;
Char ** dbresult;
Int nrow, ncolumn;
Int I, J;
Int index;
Result = sqlite3_open ("c: \ dcg_database.db", & dB );
If (result! = Sqlite_ OK)
{
Return-1;
}
// Database operations Code
// Assume that the mytable_1 table has been created.
// Start the query. The passed dbresult is already char **. Here, an & get address character is added, and the passed result is Char ***
Result = sqlite3_get_table (dB, "select * From mytable_1", & 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 (I = 0; I <nrow; I ++)
{
Printf ("Number % d records \ n", I + 1 );
For (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 );
Return 0;
}

7. Release query results:
Function: releases the memory occupied by the currently queried record set.
Void sqlite3_free_table (char ** result );

Example: (a simple C-language example using callback functions in the SQLite database) Copy code The Code is as follows: # include <stdio. h>
# Include <sqlite3.h>
Static int callback (void * notused, int argc, char ** argv, char ** azcolname)
{
Int I;
For (I = 0; I <argc; I ++)
{
Printf ("% s = % s \ n", azcolname [I], argv [I]? Argv [I]: "null ");
}
Printf ("\ n ");
Return 0;
}
Int main (INT argc, char ** argv)
{
Sqlite3 * dB;
Char * zerrmsg = 0;
Int RC;
If (argc! = 3)
{
Fprintf (stderr, "Usage: % s Database SQL-STATEMENT \ n", argv [0]);
Return 1;
}
Rc = sqlite3_open (argv [1], & dB );
If (RC)
{
Fprintf (stderr, "can't open database: % s \ n", sqlite3_errmsg (db ));
Sqlite3_close (db );
Return 1;
}
Rc = sqlite3_exec (dB, argv [2], callback, 0, & zerrmsg );
If (RC! = Sqlite_ OK)
{
Fprintf (stderr, "SQL error: % s \ n", zerrmsg );
Sqlite3_close (db );
Return 1;
}
Sqlite3_close (db );
Return 0;
}

Compile:
[Root @ localhost test] # gcc SQL. C-o SQL-l sqlite3

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.