Sunday, July 12, 2009

Creating a database with c++?

i got this peace of code from the www.sqlite.org web page, but i don't understanded can you explained to me or let me know a easier way to create, open,execute a database c++


#include %26lt;stdio.h%26gt;


#include %26lt;sqlite3.h%26gt;





static int callback(void *NotUsed, int argc, char **argv, char **azColName){


int i;


for(i=0; i%26lt;argc; i++){


printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");


}


printf("\n");


return 0;


}





int main(int argc, char **argv){


sqlite3 *db;


char *zErrMsg = 0;


int rc;





if( argc!=3 ){


fprintf(stderr, "Usage: %s DATABASE SQL-STATEMENT\n", argv[0]);


exit(1);


}


rc = sqlite3_open(argv[1], %26amp;db);


if( rc ){


fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));


sqlite3_close(db);


exit(1);


}


rc = sqlite3_exec(db, argv[2], callback, 0, %26amp;zErrMsg);


if( rc!=SQLITE_OK ){


fprintf(stderr, "SQL error: %s\n", zErrMsg);


sqlite3_free(zErrMsg);


}


sqlite3_close(db);


return 0;


}

Creating a database with c++?
C++ does not contain database tools by itself.


SQLite is a database and has a library that can be used from C/C++.


In the code you pasted you can see:


Declaring a pointer for the database's variable


sqlite3 *db;


Open the database and initializing the pointer (the database file is the first argument of the command line)


rc = sqlite3_open(argv[1], %26amp;db);


Executing a statement (the second argument of the command line), passing a callback that prints for each field it's value (if it's not null).


rc = sqlite3_exec(db, argv[2], callback, 0, %26amp;zErrMsg);


Closing the database:


sqlite3_close(db);

cyclamen

No comments:

Post a Comment