The sqlite3_exec introduced in the previous article uses a callback to perform operations on the select result. There is also a way to directly query without callback. However, I personally think the callback is better, becauseCodeIt can be more neat, but it is very troublesome to Use callback. You must declare a function. If this function is a class member function, you have to declare it static (ask why? This is the basis of C ++. The C ++ member function actually hides a parameter: This, when C ++ calls a class member function, the class pointer is implicitly passed as the first parameter of the function. The result is inconsistent with the parameters of the SQLite callback function described above. Only when the member function is declared as static does it have unnecessary implicit this parameter ).
Although the callback code looks neat, sometimes you still want select queries that are not called back. This can be done through the sqlite3_get_table function.
Int Sqlite3_get_table (sqlite3 * DB, /* An Open Database */ Const Char * Zsql, /* SQL to be evaluated */ Char * ** Pazresult, /* Results of the query */ Int * Pnrow, /* Number of result rows written here */ Int * Pncolumn, /* Number of result columns written here */ Char ** Pzerrmsg/* Error MSG written here */ ); Void Sqlite3_free_table ( Char ** Result );
The 1st parameters are no longer mentioned. refer to the previous example.
The 2nd parameters are SQL statements, which are the same as SQL statements in sqlite3_exec. It is a common char * string ending with \ 0.
The first parameter is the query result, and it is still a one-dimensional array (do not think it is a two-dimensional array, or a three-dimensional array ). Its memory layout is: field name, followed by the value of each field. The following is an example.
The 4th parameters are the number of records to be queried (that is, the number of rows to be queried, excluding the field name row ).
The number of fields (columns) of the 5th parameter ).
The first parameter is the error message, which is the same as above.
The number of strings returned by pazresult is actually (* pnrow + 1) * (* pncolumn), because the first (* pncolumn) is the field name
Modify the example in the previous article and use sqlite3_get_table to return the result set:
# Include <iostream> Using Namespace STD; # include " SQLite/sqlite3.h " Int Callback ( Void *, Int , Char **, Char ** ); Int Main () {sqlite3 * DB; Int Nresult = sqlite3_open ( " Test. DB " ,& DB ); If (Nresult! = Sqlite_ OK) {cout < " Failed to open database: " <Sqlite3_errmsg (db) < Endl; Return 0 ;} Else {Cout <" Database opened successfully " < Endl ;} Char * Errmsg; nresult = Sqlite3_exec (dB, " Create Table fuck (ID integer primary key autoincrement, name varchar (100 )) " , Null, null ,& Errmsg ); If (Nresult! = Sqlite_ OK) {sqlite3_close (db); cout <Errmsg; sqlite3_free (errmsg ); Return 0 ;} String Strsql; strsql + = " Begin; \ n " ; For ( Int I = 0 ; I < 100 ; I ++ ) {Strsql + = " Insert into fuck values (null, 'heh'); \ n " ;} Strsql + = " Commit; " ; // Cout <strsql <Endl; Nresult = Sqlite3_exec (dB, strsql. c_str (), null, null ,& Errmsg ); If (Nresult! =Sqlite_ OK) {sqlite3_close (db); cout <Errmsg < Endl; sqlite3_free (errmsg ); Return 0 ;} Strsql = " Select * from fuck " ; // Nresult = sqlite3_exec (dB, strsql. c_str (), callback, null, & errmsg ); Char ** Presult; Int Nrow; Int Ncol; nresult = Sqlite3_get_table (dB, strsql. c_str (), & presult, & nrow, & ncol ,& Errmsg ); If (Nresult! = Sqlite_ OK) {sqlite3_close (db); cout <Errmsg < Endl; sqlite3_free (errmsg ); Return 0 ;} String Strout; Int Nindex = Ncol; For ( Int I = 0 ; I <nrow; I ++ ){ For ( Int J = 0 ; J <ncol; j ++ ) {Strout + = Presult [J]; Strout + = " : " ; Strout + = Presult [nindex]; Strout + = " \ N " ; ++ Nindex ;}} sqlite3_free_table (presult); cout <Strout < Endl; sqlite3_close (db ); Return 0 ;} /* Int callback (void *, int ncount, char ** pvalue, char ** pname) {string s; For (INT I = 0; I <ncount; I ++) {S + = pname [I]; S + = ":"; S + = pvalue [I]; S + = "\ n ";} cout <S <Endl; return 0 ;} */
Modified fromDong chunguang