A table was created earlier and now inserts some data into it. Inserting data is almost the same as creating a table, but the SQL language is different, and the complete code is as follows:
#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;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 rowchar**, an Array of strings representing fields in the rowchar** a array of strings representing column names); */static int call Back (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," "The Age INT is not null," "ADDRESS CHAR (#)," "SALARY REAL); "; /* Execute SQL statement */ret = sqlite3_exec (db, SQL, callback, 0, &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;}
/* added this function */bool insertrecords () {/* Create SQL statement */sql = "INSERT into Company" ( id,name,age,address,salary) "" VALUES (1, ' Paul ', +, ' California ', 20000.00); "" INSERT into Company (id,name,age,address,salary) "VALUES (2, ' Allen ', +, ' Texas ', 15000.00);" " INSERT into Company (id,name,age,address,salary) "" VALUES (3, ' Teddy ', +, ' Norway ', 20000.00); " "INSERT into Company" VALUES (4, ' Mark ', id,name,age,address,salary, ' Rich-mond ', 65000.00); "; /* Execute SQL statement */ret = sqlite3_exec (db, SQL, callback, 0, &zerrmsg); if (ret! = SQLITE_OK) {fprintf (stderr, "SQL Error:%s\n", zerrmsg); Sqlite3_free (ZERRMSG); return false; } fprintf (stdout, "successfully records created\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 (); Closedb (); return 0;}
SQLite Learning note 9:c inserting data using SQLite in the language