Created
April 19, 2012 21:59
-
-
Save enile8/2424514 to your computer and use it in GitHub Desktop.
A small example program using SQLite with C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A small example program using SQLite with C++ | |
#include <iostream> | |
#include <sqlite3.h> | |
using namespace std; | |
static int callback(void *NotUsed, int argc, char **argv, char **azColName) | |
{ | |
int i; | |
for(i=0; i<argc; i++) | |
{ | |
cout<<azColName[i]<<" = " << (argv[i] ? argv[i] : "NULL")<<"\n"; | |
} | |
cout<<"\n"; | |
return 0; | |
} | |
int main() | |
{ | |
const int STATEMENTS = 8; | |
sqlite3 *db; | |
char *zErrMsg = 0; | |
const char *pSQL[STATEMENTS]; | |
int rc; | |
rc = sqlite3_open("familyGuy.db", &db); | |
if( rc ) | |
{ | |
cout<<"Can't open database: "<<sqlite3_errmsg(db)<<"\n"; | |
} | |
else | |
{ | |
cout<<"Open database successfully\n\n"; | |
} | |
pSQL[0] = "create table myTable (FirstName varchar(30), LastName varchar(30), Age smallint, Hometown varchar(30), Job varchar(30))"; | |
pSQL[1] = "insert into myTable (FirstName, LastName, Age, Hometown, Job) values ('Peter', 'Griffin', 41, 'Quahog', 'Brewery')"; | |
pSQL[2] = "insert into myTable (FirstName, LastName, Age, Hometown, Job) values ('Lois', 'Griffin', 40, 'Newport', 'Piano Teacher')"; | |
pSQL[3] = "insert into myTable (FirstName, LastName, Age, Hometown, Job) values ('Joseph', 'Swanson', 39, 'Quahog', 'Police Officer')"; | |
pSQL[4] = "insert into myTable (FirstName, LastName, Age, Hometown, Job) values ('Glenn', 'Quagmire', 41, 'Quahog', 'Pilot')"; | |
pSQL[5] = "select * from myTable"; | |
pSQL[6] = "delete from myTable"; | |
pSQL[7] = "drop table myTable"; | |
for(int i = 0; i < STATEMENTS; i++) | |
{ | |
rc = sqlite3_exec(db, pSQL[i], callback, 0, &zErrMsg); | |
if( rc!=SQLITE_OK ) | |
{ | |
cout<<"SQL error: "<<sqlite3_errmsg(db)<<"\n"; | |
sqlite3_free(zErrMsg); | |
break; | |
} | |
} | |
sqlite3_close(db); | |
return 0; | |
} |
when I run the above code I am getting an error
"Severity Code Description Project File Line Suppression State
Error C2597 illegal reference to non-static member 'Database::db' "
"
https://bryanstamour.com/post/2017-03-12-sqlite-with-cpp/
if u dont mind explaining, what is the callback function for and why is it necessary?
if u dont mind explaining, what is the callback function for and why is it necessary?
The callback provides a way to obtain the results returned from queries like SELECTs. Here is a breakdown of it:
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 */
);
May i ask a question,how can i select data and save it in vector,finally return it?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
when I run the above code I am getting an error
"Severity Code Description Project File Line Suppression State
Error C2597 illegal reference to non-static member 'Database::db' "
"