Tasks.json中添加配置"-lsqlite3"
#include<sqlite3.h> #include<iostream> #include<errno.h>static int callback1(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; }
static int callback_insert(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; }
/* void *data: 这是用户定义的数据,通常在调用 sqlite3_exec 时传入。 int argc: 这是结果集中列的数量。 char **argv[]: 这是一个指向字符串数组的指针,数组中的每个元素都是结果集中对应列的值。 char **azColName[]: 这是一个指向列名的字符串数组的指针。 */ static int callback_select(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; }
static int callback_update(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; }
static int callback_delete(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(){ std::cout<<"asdf"<<std::endl; sqlite3 *db; char *sql; char *zErrMsg = 0; const char* data = "Callback function called";
int rc = sqlite3_open("/home/trc/cPlus/data.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 = "CREATE TABLE INF(" \ "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, callback1, 0, &zErrMsg); if( rc != SQLITE_OK ){ fprintf(stderr, "SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); }else{ fprintf(stdout, "Table created successfully\n"); }
/* Create SQL statement */ sql = "INSERT INTO INF (ID,NAME,AGE,ADDRESS,SALARY) " \ "VALUES (1, 'Paul', 32, 'California', 20000.00 ); " \ "INSERT INTO INF (ID,NAME,AGE,ADDRESS,SALARY) " \ "VALUES (2, 'Allen', 25, 'Texas', 15000.00 ); " \ "INSERT INTO INF (ID,NAME,AGE,ADDRESS,SALARY)" \ "VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );" \ "INSERT INTO INF (ID,NAME,AGE,ADDRESS,SALARY)" \ "VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );";
/* Execute SQL statement */ rc = sqlite3_exec(db, sql, callback_insert, 0, &zErrMsg); if( rc != SQLITE_OK ){ fprintf(stderr, "SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); }else{ fprintf(stdout, "Records created successfully\n"); }
/* Create SQL statement */ sql = "SELECT * from INF";
/* Execute SQL statement */ rc = sqlite3_exec(db, sql, callback_select, (void*)data, &zErrMsg); /*SQLITE_OK:成功。 SQLITE_ERROR:一般性错误。 SQLITE_BUSY:数据库忙。*/ if( rc != SQLITE_OK ){ fprintf(stderr, "SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); }else{ fprintf(stdout, "Operation done successfully\n"); }
/* Create merged SQL statement */ sql = "UPDATE INF set SALARY = 25000.00 where ID=1; " \ "SELECT * from INF";
/* Execute SQL statement */ rc = sqlite3_exec(db, sql, callback_update, (void*)data, &zErrMsg); if( rc != SQLITE_OK ){ fprintf(stderr, "SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); }else{ fprintf(stdout, "Operation done successfully\n"); }
/* Create merged SQL statement */ sql = "DELETE from INF where ID=2; " \ "SELECT * from INF";
/* Execute SQL statement */ rc = sqlite3_exec(db, sql, callback_delete, (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; } 标签:char,sqlite,zErrMsg,int,c++,vsode,fprintf,SQL,sqlite3 From: https://www.cnblogs.com/trc258/p/18284192