最后一节,这里记录下如何删除数据。
前面所有的代码都继承在这里了,在Ubuntu14.04和Mac10.9上亲测通过。
#include <stdio.h> #include <stdlib.h> #include "sqlite/sqlite3.h" #define DB_NANE "sqlite/test.db" sqlite3 *db = NULL; char* sql = NULL; char *zErrMsg = NULL; const char* data = "Callback function called"; int ret = 0; typedef enum{ false,true } bool; /* typedef int (*sqlite3_callback)( void*,Data provided in the 4th argument of sqlite3_exec() int,The number of columns in row char**,An array of strings representing fields in the row char** An array of strings representing column names ); */ static int callback(void *NotUsed,int argc,char **argv,char **azColName) { int i = 0; for(i=0; i < argc; i++){ printf("%s = %s\n",azColName[i],argv[i] ? argv[i] : "NULL"); } printf("\n"); return 0; } bool connectDB() { ret = sqlite3_open(DB_NANE,&db); if( ret != sqlITE_OK){ fprintf(stderr,"Error open database: %s\n",sqlite3_errmsg(db)); sqlite3_free(zErrMsg); return false; } fprintf(stdout,"Successfully opened database\n"); return true; } bool createTable() { /* Create 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 sql statement */ ret = sqlite3_exec(db,sql,callback,&zErrMsg); if( ret != sqlITE_OK ){ fprintf(stderr,"Error sql: %s\n",zErrMsg); sqlite3_free(zErrMsg); return false; } fprintf(stdout,"Successfully table created\n"); return true; } bool insertRecords() { /* Create sql statement */ sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " \ "VALUES (1,'Paul',32,'California',20000.00 ); " \ "INSERT INTO COMPANY (ID,SALARY) " \ "VALUES (2,'Allen',25,'Texas',15000.00 ); " \ "INSERT INTO COMPANY (ID,SALARY)" \ "VALUES (3,'Teddy',23,'Norway',20000.00 );" \ "INSERT INTO COMPANY (ID,SALARY)" \ "VALUES (4,'Mark','Rich-Mond ',65000.00 );"; /* Execute sql statement */ ret = sqlite3_exec(db,"sql error: %s\n",zErrMsg); sqlite3_free(zErrMsg); return false; } fprintf(stdout,"successfully records created\n"); return true; } bool selectFromTable() { /* Create sql statement */ sql = "SELECT * from COMPANY"; /* Execute sql statement */ ret = sqlite3_exec(db,(void*)data,"successfully operation done\n"); return true; } bool updateTable() { /* Create merged sql statement */ sql = "UPDATE COMPANY set SALARY = 25000.00 where ID=1; " \ "SELECT * from COMPANY"; /* Execute sql statement */ ret = sqlite3_exec(db,"Successfully operation done \n"); return true; } bool deleteTable()/*新加*/ { /* Create merged sql statement */ sql = "DELETE from COMPANY where ID=2; " \ "SELECT * from COMPANY"; /* Execute sql statement */ ret = sqlite3_exec(db,"Successfully operation done\n"); return true; } bool closeDB() { int ret = 0; ret = sqlite3_close(db); if ( ret == sqlITE_BUSY ){ return false; } return true; } int main(int argc,char* argv[]) { connectDB(); /*createTable();*/ /*insertRecords();*/ selectFromTable(); /*updateTable();*/ deleteTable(); selectFromTable(); closeDB(); return 0; }原文链接:https://www.f2er.com/sqlite/200311.html