SQLite C/C ++ tutorial

Source: Internet
Author: User

1 Installation

2 C/C ++ Interface APIs

3. Connect to the database

4. Create a table

5 insert operations

6. Update operations

7. Delete

 

 

 

 

S. N

API

1

Sqlite3_open (const char * filename, sqlite3 ** ppDb)

 

 

 

2

Sqlite3_exec (sqlite3 *, const char * SQL, sqlite_callback, void * data, char ** errmsg)

 

 

 

3

Sqlite3_close (sqlite3 *)

 

 

 

The following code requires a prompt:

The databases created in each example are created based on the previous database. Otherwise, the following code cannot be run !!

# Include <stdio. h>

# Include <stdlib. h>

# Include <sqlite3.h>

 

Int main (int argc, char * argv [])

{

Sqlite3 * db;

Char * zErrMsg = 0;

Int rc;

 

Rc = sqlite3_open ("test. db", & db );

 

If (rc ){

Fprintf (stderr, "Can't open database: % s \ n", sqlite3_errmsg (db ));

Exit (0 );

} Else {

Fprintf (stderr, "Opened database successfully \ n ");

}

Sqlite3_close (db );

}

-rwxr-xr-x. 1 root root 7383 May  8 02:06 a.out
-rw-r--r--. 1 root root  323 May  8 02:05 test.c
-rw-r--r--. 1 root root    0 May  8 02:06 test.db

 

 

 

 

# Include <stdio. h>

# Include <stdlib. 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;

Char * SQL;

 

/* Open database */

Rc = sqlite3_open ("test. db", & db );

If (rc ){

Fprintf (stderr, "Can't open database: % s \ n", sqlite3_errmsg (db ));

Exit (0 );

} Else {

Fprintf (stdout, "Opened database successfully \ n ");

}

 

/*** Generate an SQL statement ***/

SQL = "CREATE TABLE COMPANY ("\

"Id int primary key not null ,"\

"Name text not null ,"\

"Age int not null ,"\

"Address char (50 ),"\

"Salary real );";

 

/*** Execute the SQLite statement ***/

Rc = sqlite3_exec (db, SQL, callback, 0, & zErrMsg );

If (rc! = SQLITE_ OK ){

Fprintf (stderr, "SQL error: % s \ n", zErrMsg );

Sqlite3_free (zErrMsg );

} Else {

Fprintf (stdout, "Table created successfully \ n ");

}

Sqlite3_close (db );

Return 0;

}

-rwxr-xr-x. 1 root root 9567 May 8 02:31 a.out
-rw-r--r--. 1 root root 1207 May  8 02:31 test.c
-rw-r--r--. 1 root root 3072 May  8 02:31 test.db

 

 

# Include <stdio. h>

# Include <stdlib. 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;

Char * SQL;

 

/* Open database */

Rc = sqlite3_open ("test. db", & db );

If (rc ){

Fprintf (stderr, "Can't open database: % s \ n", sqlite3_errmsg (db ));

Exit (0 );

} Else {

Fprintf (stderr, "Opened database successfully \ n ");

}

 

/* Create SQL statement */

SQL = "INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY )"\

"VALUES (1, 'paol', 32, 'california, 20000.00 );"\

"Insert into company (ID, NAME, AGE, ADDRESS, SALARY )"\

"VALUES (2, 'allen ', 25, 'texas, 15000.00 );"\

"Insert into company (ID, NAME, AGE, ADDRESS, SALARY )"\

"VALUES (3, 'Teddy ', 23, 'norve', 20000.00 );"\

"Insert into company (ID, NAME, AGE, ADDRESS, SALARY )"\

"VALUES (4, 'mark', 25, 'Rich-Mond, 65000.00 );";

 

/* Execute SQL statement */

Rc = sqlite3_exec (db, SQL, callback, 0, & zErrMsg );

If (rc! = SQLITE_ OK ){

Fprintf (stderr, "SQL error: % s \ n", zErrMsg );

Sqlite3_free (zErrMsg );

} Else {

Fprintf (stdout, "Records created successfully \ n ");

}

Sqlite3_close (db );

Return 0;

}

Opened database successfully
Records created successfully

 

 

 

 

 

 

 

 

 

 

 

# Include <stdio. h>

# Include <stdlib. h>

# Include <sqlite3.h>

Static int callback (void * data, int argc, char ** argv, char ** azColName ){

Int I;

Fprintf (stderr, "% s:", (const char *) data );

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;

Char * SQL;

Const char * data = "Callback function called ";

 

/* Open database */

Rc = sqlite3_open ("test. db", & db );

If (rc ){

Fprintf (stderr, "Can't open database: % s \ n", sqlite3_errmsg (db ));

Exit (0 );

} Else {

Fprintf (stderr, "Opened database successfully \ n ");

}

 

/* Create merged SQL statement */

SQL = "UPDATE COMPANY set SALARY = 25000.00 where ID = 1 ;"\

"SELECT * from COMPANY ";

 

/* Execute SQL statement */

Rc = sqlite3_exec (db, SQL, callback, (void *) data, & zErrMsg );

If (rc! = SQLITE_ OK ){

Fprintf (stderr, "SQL error: % s \ n", zErrMsg );

Sqlite3_free (zErrMsg );

} Else {

Fprintf (stdout, "Operation done successfully \ n ");

}

Sqlite3_close (db );

Return 0;

}

 

# Include <stdio. h>

# Include <stdlib. h>

# Include <sqlite3.h>

 

Static int callback (void * data, int argc, char ** argv, char ** azColName ){

Int I;

Fprintf (stderr, "% s:", (const char *) data );

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;

Char * SQL;

Const char * data = "Callback function called ";

 

/* Open database */

Rc = sqlite3_open ("test. db", & db );

If (rc ){

Fprintf (stderr, "Can't open database: % s \ n", sqlite3_errmsg (db ));

Exit (0 );

} Else {

Fprintf (stderr, "Opened database successfully \ n ");

}

 

/* Create merged SQL statement */

SQL = "DELETE from COMPANY where ID = 2 ;"\

"SELECT * from COMPANY ";

 

/* Execute SQL statement */

Rc = sqlite3_exec (db, SQL, callback, (void *) data, & zErrMsg );

If (rc! = SQLITE_ OK ){

Fprintf (stderr, "SQL error: % s \ n", zErrMsg );

Sqlite3_free (zErrMsg );

} Else {

Fprintf (stdout, "Operation done successfully \ n ");

}

Sqlite3_close (db );

Return 0;

}

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.