digitalmars.D - I could use some help with SQLite3
I hope someone can help me with sqlite3. Maybe someone has been doing it succesfully and can show me how they did it, or maybe someone can explain what i am doing wrong in my own test. Because i can't figure out how i should use the callback function of sqlite3. The code does compile without errors. But when i start the program it gives the error message: "Error: Access Violation" Also the numCols always returns 1 no matter how many columns there are. And if i try to writef the results then i get a wrong set of characters and the error message: "Error: invalid UTF-8 sequence" And if i try to do the same with columnNames then i get the error message: "Error: ArrayBoundsError test.d(9)" Here is my code: import std.stdio; import sqlite3_imp; int DoTheCallback (void* NotUsed, int numCols , char[][] results, char[][] columnNames) { writef("number of colomns: ", numCols, "\n"); return 0; }; int main() { sqlite3* db; sqlite3_open("test.db", &db); sqlite3_exec(db, "SELECT * FROM account" ,cast(sqlite_callback) &DoTheCallback, null, null); sqlite3_close(db); return 0; }
Jul 29 2004
"Mista" <stoop hotpop.com> wrote in message news:ceb8pu$1i9n$1 digitaldaemon.com...I hope someone can help me with sqlite3. Maybe someone has been doing it succesfully and can show me how they did it, or maybe someone can explain what i am doing wrong in my own test. Because i can't figure out how i should use the callback function of sqlite3. The code does compile without errors. But when i start the program it gives the error message: "Error: Access Violation" Also the numCols always returns 1 no matter how many columns there are. And if i try to writef the results then i get a wrong set of characters and the error message: "Error: invalid UTF-8 sequence" And if i try to do the same with columnNames then i get the error message: "Error: ArrayBoundsError test.d(9)" Here is my code: import std.stdio; import sqlite3_imp; int DoTheCallback (void* NotUsed, int numCols , char[][] results, char[][] columnNames) { writef("number of colomns: ", numCols, "\n"); return 0; }; int main() { sqlite3* db; sqlite3_open("test.db", &db); sqlite3_exec(db, "SELECT * FROM account" ,cast(sqlite_callback) &DoTheCallback, null, null); sqlite3_close(db); return 0; }Your callback function is incorrect and your cast is telling the compiler to ignore it. Use this and don't cast it: extern(C) int DoTheCallback(void* pArg, int numCols, char** results, char** columnNames) { writef("number of colomns: ", numCols, "\n"); for(int i = 0; i != numCols; i++) { writef("Column '%s' result is: %s\n", toString(columnNames[i]), toString(results[i])); } return 0; }
Jul 29 2004
Thank you, that did the trick. It would be great if the callback part could be included in the example on http://www.dprogramming.com/sqlite.php Maybe there will be more people who will find this info usefull. Or maybe i am the only one who can't figure out that i should use extern(C) :D Vathix wrote:"Mista" <stoop hotpop.com> wrote in message news:ceb8pu$1i9n$1 digitaldaemon.com...I hope someone can help me with sqlite3. Maybe someone has been doing it succesfully and can show me how they did it, or maybe someone can explain what i am doing wrong in my own test. Because i can't figure out how i should use the callback function of sqlite3. The code does compile without errors. But when i start the program it gives the error message: "Error: Access Violation" Also the numCols always returns 1 no matter how many columns there are. And if i try to writef the results then i get a wrong set of characters and the error message: "Error: invalid UTF-8 sequence" And if i try to do the same with columnNames then i get the error message: "Error: ArrayBoundsError test.d(9)" Here is my code: import std.stdio; import sqlite3_imp; int DoTheCallback (void* NotUsed, int numCols , char[][] results, char[][] columnNames) { writef("number of colomns: ", numCols, "\n"); return 0; }; int main() { sqlite3* db; sqlite3_open("test.db", &db); sqlite3_exec(db, "SELECT * FROM account" ,cast(sqlite_callback) &DoTheCallback, null, null); sqlite3_close(db); return 0; }Your callback function is incorrect and your cast is telling the compiler to ignore it. Use this and don't cast it: extern(C) int DoTheCallback(void* pArg, int numCols, char** results, char** columnNames) { writef("number of colomns: ", numCols, "\n"); for(int i = 0; i != numCols; i++) { writef("Column '%s' result is: %s\n", toString(columnNames[i]), toString(results[i])); } return 0; }
Jul 29 2004