#include <iostream> #include <sstream> #include ". /sqlite3_lib/sqlite3.h " /** Demonstrates the process of PHP calling C + + for data manipulation. **/ #pragma comment (lib, "...). /sqlite3_lib/sqlite3.lib ") using namespace Std; static int _sql_callback (void * notused, int argc, char * * argv, char * * szcolname) { int i; for (i=0 i < argc; i++) { printf ("%s =%sn", Szcolname[i], argv[i] = = 0? "NUL": Argv[i]); } return 0; } int main (int argc, char * argv[]) { String Name= ""; int age=0; String birthday= "1985-05-16"; if (argc>3) { cout<< "Number of arguments is" <<argc<<endl; NAME=ARGV[1]; Age=atoi (argv[2]); BIRTHDAY=ARGV[3]; } else{ cout<< "did not get enough command-line arguments, please enter" <<endl; cout<< "Please enter your name, age, Birthday" in turn <<endl; cin>>name; cin>>age; cin>>birthday; } StringStream Strstream; Strstream <<age; string s = Strstream.str (); String isql= "insert into users values (' +name+", "+s+", "+birthday+"); "; cout<<isql<<endl; const char * sSQL1 = "CREATE TABLE users" (userid varchar () PRIMARY KEY, age int, birthday datetime); "; const char * sSQL2 = "SELECT * from users;"; Sqlite3 * db = 0; char * perrmsg = 0; int ret = 0;
Connecting to a database ret = Sqlite3_open ("./db.db", &db); if (ret!= SQLITE_OK) { fprintf (stderr, "Cannot Open database:%s", sqlite3_errmsg (db)); return (1); }
printf ("Database connection succeeded!n"); Execute the Build table SQL Sqlite3_exec (db, sSQL1, 0, 0, &perrmsg); if (ret!= SQLITE_OK) { fprintf (stderr, "SQL Error:%SN", perrmsg); Sqlite3_free (PERRMSG); }
Execute INSERT Record SQL Sqlite3_exec (DB, Isql.c_str (), 0, 0, &perrmsg); Querying data tables Sqlite3_exec (db, sSQL2, _sql_callback, 0, &perrmsg); Close Database Sqlite3_close (DB); db = 0; return 0; } |