2010/1/19 Fabrice NA <pazou...@gmail.com>: > Hi all, > > I am trying to understand Sqlite and to make thing worse I am also learning > C++. You will not be surprised that I find it really hard to understand the > C++ example on the web. Can someone guide me to digest this? If you can > explain please do so by extrapolating since I am a total newbie. > > I have managed to compile the code example into a file called testdb.exe and > have created a database named Cars.db containing 7 rows. (notice that I have > removed some part of that code that I don't need help for) > > #include <stdio.h> > #include <sqlite3.h> > > static int callback(void *NotUsed, int argc, char **argv, char **azColName){ > int i; > for(i=0; i<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], &db); > rc = sqlite3_exec(db, argv[2], callback, 0, &zErrMsg); > > sqlite3_close(db); > > return 0; > > } > > Now in command line when I execute testdb.exe cars.db "select * from cars" > (the table is called cars too) everything works fine and I get an output > with all my seven rows. But how come this work? Is this some kind of magic?
No > > Even after reading the doc again and again I don't understand (probably > because I am still learning some basic C++ concepts). Here my questions; Your problems appear to be mostly understanding C/C++. > > 1) What is the purpose of doing "sqlite3 *db" are we just creating a > pointer of type sqlite3 named db here? Yes. It is essentially a handle to the database that is returned by the database open call, which then needs to be passed to other sqlite library routines. > > 2) At first I though that for the main function the first > parameter "cars.db" > was represented by the variable argc and the second "select * from cars" by > argv. Well at the end, it looks like that "cars.db" is argv[1] and that the > select statement is argv[2]. What is argc then? argc is the count of passed in arguments argv is the vector of passed in arguments argv[0] is always the name of the executable that is executing > Seems like it's the number > of rows returned by the query (when looking at function callback) but how > the program find this out? How come we have a line " if( argc!=3 )" and see > this same argc variable in the callback function? argc/argv in main and argc/argv in callback have nothing to do with each other. callback is a routine that needs to be provided for an sqlite_exec() call. In callback argc would be better named numColumns, and argv better named columnData. > > 3) I don't understand the third argument from the query " rc = > sqlite3_exec(db, argv[2], callback, 0, &zErrMsg);" my problem is > understanding the callback function inside the sqlite3_exec function. > Reading this link http://www.sqlite.org/c3ref/exec.html didn't help too much > even though it has been written in plain English. As sqlite_exec() retrieves each row of data, it calls the callback routine that is provided via the third argument (in your case also named 'callback'). You can then do what YOU want with the data by coding the callback appropriately. In the case you have shown, the values are simply being printed out. > > 4) I am using VC++ Express from Microsoft on WinXP and would like to > know if it's possible to pass parameter when debugging i.e. tell the > debugger to use cars.db for file and use "select * from cars" as a statement > (this would allow me to see what's hapening witout replacing variables by > their real values). Don't know about VC++ Express, but in Visual Studio there is a debugging tab on the project properties page that allows command line arguments to be specified. > > 5) It's really hard to find some simple example on internet about C++ > working with sqlite. Can any of you provide with simple sample codes that > shows how you can do and what you can do with sqlite and C++? SQLite is a C library, not C++. You can however link C++ code against C object, and the SQLite library even provides appropriate 'extern "C" {}' wrapping around things so that it can be compiled with a C++ compiler; but that does not make it C++ code. There are C++ wrappers, notably System.Data.SQLite (http://sqlite.phxsoftware.com) (sorry anybody else) further things to take a look at: http://www.sqlite.org/cintro.html The code you show above is based on sqlite3_exec(). This is deprecated; better to use sqlite3_prepare_v2(), sqlite3_step, sqlite3_reset()/sqlite3_finalize(): http://www.sqlite.org/cvstrac/wiki?p=SimpleCode > > I hope I didn't offended anyone with my lack of knowledge and I thank in > advance the courageous ones who managed to read this email until the end and > probably got answers to my questions. > > Fabou Regards, Simon _______________________________________________ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users