SQLite C API

sqlite c api记录如下:

//file : helloworld.c
//gcc -g helloworld.c -o helloworld -l sqlite3
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>


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

int main(int argc, char* argv[])
{
   sqlite3 *db;
   char *zErrMsg = 0;
   int rc;
   const char* data = "Callback function called";

   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 table
   char* sql = "CREATE TABLE COMPANY("  \
		"ID INT PRIMARY KEY     NOT NULL," \
		"NAME           TEXT    NOT NULL," \
		"AGE            INT     NOT NULL," \
		"ADDRESS        CHAR(50)," \
		"SALARY         REAL );";

   /* 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, "Table created successfully\n");
   }

   //insert 
   /* Create SQL statement */
   sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "  \
         "VALUES (1, 'Paul', 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, 'Norway', 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");
   }

   //select
   /* Create SQL statement */
   sql = "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");
   }

   //update
   /* 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");
   }

   //prepare-bind
   fprintf(stdout, "Prepare-bind-test\n");
   sql = "SELECT * from COMPANY where id <= (?) and id >= (?)";
   sqlite3_stmt *stmt = NULL;
   sqlite3_prepare(db, sql, -1, &stmt, NULL);

   sqlite3_bind_int(stmt,1, 2);
   sqlite3_bind_int(stmt,2, 4);

   int count = sqlite3_column_count(stmt);
   printf("count = %d\n",count);
   int i;
   for(i = 0; i < count; i++)
   {
       printf("column name:%s\n",sqlite3_column_name(stmt, i));
       printf("column type:%i\n",sqlite3_column_type(stmt, i));
   }

   sqlite3_reset(stmt);

   sqlite3_bind_int(stmt,1, 2);
   sqlite3_bind_int(stmt,2, 4);

   printf("count = %d\n",count);
   for(i = 0; i < count; i++)
   {
       printf("column name:%s\n",sqlite3_column_name(stmt, i));
       printf("column type:%i\n",sqlite3_column_type(stmt, i));
   }
   
   sqlite3_finalize(stmt); 

   sqlite3_close(db);
}

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;
}

联系我: