Sqlite3_exec Although good, but generally not recommended for direct use.
A common set of actions is:
For sqlite3_exec and SQLITE3_PREPARE_V2 usage scenarios, the following are recommended:
A small demo:
#include <stdio.h>#include<sqlite3.h>intMainintargcChar**argv) {Sqlite3*DB; intRC; RC= Sqlite3_open ("test.db", &db); if(RC) {printf ("Can ' t Open database:%s\n", sqlite3_errmsg (db)); Sqlite3_close (DB); return 1; } Else{printf ("Open Database successfully\n"); } sqlite3_stmt*STMT =NULL; Const Char*sql ="SELECT * from company";//const char *sql = "SELECT * from company WHERE age=?;";SQLITE3_PREPARE_V2 (DB, SQL,-1, &stmt, NULL); //Sqlite3_bind_int (stmt, 1, +); intCol_count =Sqlite3_column_count (stmt); printf ("This record has%d columns \ n", Col_count); while(Sqlite_row = =Sqlite3_step (stmt)) { intColumn0_type = Sqlite3_column_type (stmt,0); Const Char*column0_name = Sqlite3_column_name (stmt,0); intColumn0_value = Sqlite3_column_int (stmt,0); printf ("col:0 Type:%d name:%-10s value:%d\n", Column0_type, Column0_name, Column0_value); intColumn1_type = Sqlite3_column_type (stmt,1); Const Char*column1_name = Sqlite3_column_name (stmt,1); ConstUnsignedChar*column1_value = Sqlite3_column_text (stmt,1); printf ("col:1 Type:%d name:%-10s value:%s\n", Column1_type, Column1_name, Column1_value); intColumn2_type = Sqlite3_column_type (stmt,2); Const Char*column2_name = Sqlite3_column_name (stmt,2); intColumn2_value = Sqlite3_column_int (stmt,2); printf ("col:2 Type:%d name:%-10s value:%d\n", Column2_type, Column2_name, Column2_value); intColumn3_type = Sqlite3_column_type (stmt,3); Const Char*column3_name = Sqlite3_column_name (stmt,3); ConstUnsignedChar*column3_value = Sqlite3_column_text (stmt,3); printf ("col:3 Type:%d name:%-10s value:%s\n", Column3_type, Column3_name, Column3_value); intColumn4_type = Sqlite3_column_type (stmt,4); Const Char*column4_name = Sqlite3_column_name (stmt,4); DoubleColumn4_value = sqlite3_column_double (stmt,4); printf ("col:4 Type:%d name:%-10s value:%.2f\n", Column4_type, Column4_name, Column4_value); printf ("\ n"); } sqlite3_finalize (stmt); //char *zerrmsg = 0;//sqlite3_stmt *stmt_insert;//const char *sql_insert = "INSERT INTO Company" (Id,name,age,address,salary)//"VALUES (5, ' Logan ', ' California ', ' 20000.00 ');";//SQLITE3_PREPARE_V2 (DB, Sql_insert,-1, &stmt_insert, NULL);//if (Sqlite3_step (stmt_insert)! = Sqlite_done) {//printf ("Insert Table failed\n");//Sqlite3_free (zerrmsg);// }//sqlite3_finalize (stmt_insert);sqlite3_close (DB); return 0;}
Resources:
sqlite3_prepare_v2/sqlite3_exec[Reprint] Sqlite/C + + API learning
SQLite Use (ii)