Skip to content

Instantly share code, notes, and snippets.

@enile8 enile8/gist:2424514
Created Apr 19, 2012

Embed
What would you like to do?
A small example program using SQLite with C++
// 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;
}
@SaddamBInSyed

This comment has been minimized.

Copy link

commented Nov 7, 2018

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' "
"

@vip3r011

This comment has been minimized.

Copy link

commented Nov 10, 2018

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/

@SatoTheHat

This comment has been minimized.

Copy link

commented Nov 19, 2018

if u dont mind explaining, what is the callback function for and why is it necessary?

@WesThorburn

This comment has been minimized.

Copy link

commented Jan 8, 2019

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 */
);
@Gan-Jason

This comment has been minimized.

Copy link

commented Jul 22, 2019

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
You can’t perform that action at this time.