|
SQLite++ |
Raf Vermeulen
Member #7,166
April 2006
|
In my current project, I need to manage a small database. It basically needs a way to store, delete or edit pairs of words (one word in one language, and its translation in another language) and retrieve the pair corresponding to one word the user gave in. Best thing for this seems to be using SQL, so I've looked into SQLite3 and SQLite++. Dumb thing's that SQLite3's syntax's not compatible with SQLite++ and I can't find a decent tutorial or reference for that. Anybody here that knows how to work with it and can help me? Thanks in advance |
CGamesPlay
Member #2,559
July 2002
|
SQLite3's SQL parsing should be mostly the same as SQLite++'s, if not for small differences in maybe the PRAGMA command. SQLite++... well, the manual for it is the best reference. I haven't used it myself; I just use SQLite3 from C++. -- Ryan Patterson - <http://cgamesplay.com/> |
Raf Vermeulen
Member #7,166
April 2006
|
Hmm, quick question 'bout SQLite3 then. Do you know how to pass variables into the queries? Like "INSERT INTO table_name VALUES (string1, string2, string3)"? That's mainly what I had a problem with with SQLite3. Doesn't support strings, it seems, and I honestly don't know how to do this with char[]s:-/ |
CGamesPlay
Member #2,559
July 2002
|
You can use sprintf, but since you're using C++, use stringstreams: #include <sstream> std::stringstream sql; sql << "INSERT INTO table VALUES ( '" << my_quoted_string << " ', CURRENT_TIMESTAMP );"; sqlite3_stmt* stmt = sqlite3_prepare(sql.str().c_str());
[append] std::string sql; sql = "SELECT * FROM table;"; sqlite3_stmt* stmt = sqlite3_prepare(sql.c_str());
-- Ryan Patterson - <http://cgamesplay.com/> |
Raf Vermeulen
Member #7,166
April 2006
|
And after that, I just use sqlite3_execute()? So basically, the sqlite3_stmt type is the equivalent of the result of PHP's mysql_query(). Didn't even know that part:-X Thanks for the help |
Thomas Fjellstrom
Member #476
June 2000
|
hmm, sqlite... Passing variables: sqlite3_stmt *stmt = sqlite3_prepare("INSERT INTO table VALUES ( ?, ? );"); sqlite3_bind_int(stmt, 0, 0xDEADBEEF); sqlite3_bind_int(stmt, 1, 0xDECAFBAD); Those bind methods sure can be handy. -- |
Raf Vermeulen
Member #7,166
April 2006
|
Hummm, not as simple as it sounded. std::string query; query = "CREATE TABLE dictionary (word text, translation text);"; sqlite3_stmt *sql_statement = sqlite3_prepare(query.c_str());
That gives this compiling error: |
Thomas Fjellstrom
Member #476
June 2000
|
Since query is a C++ std::string, you have to use the c_str() method on it to pass to functions that take a char* (just as was said by CGames) ie: std::string query = "CREATE TABLE dictionary ('word text', 'translation text');"; sqlite3_stmt *sql_statement = sqlite3_prepare(query.c_str()); Also take a look at my previous post, since using placeholders means you dont have to quote the data yourself. -- |
Raf Vermeulen
Member #7,166
April 2006
|
I edited my post when I noticed the .c_string() function, but it didn't change a thing. I just tried this: Could it be that my query's not correct? I'm not too familiar with SQLite queries, only MySQL queries, so could've put some mistakes in there? |
Thomas Fjellstrom
Member #476
June 2000
|
Sorry, we both (cgames and I) made a small mistake giving you the prepare function, first off, let me say, lookup the sqlite3 docs, they are very usefull, and second, sqlite3_prepare takes the sqlite3* db handle as first arg, as that error is trying to tell you. -- |
Raf Vermeulen
Member #7,166
April 2006
|
Hmm, I've got all parameters right, up til these two now: |
Thomas Fjellstrom
Member #476
June 2000
|
Heres an example using the exec method, I found through google:
A simple exaple using sqlite3_step instead of sqlite3_exec to follow. edit, next example:
edit2, NEITHER of these have been tested, use at your own risk, I'm not even guaranteeing that they'll compile. -- |
Raf Vermeulen
Member #7,166
April 2006
|
Thanks for the help. I don't fully understand those codes (argv[1], argv[2]? Where do they come from?) I managed to fill in my code with those examples, though, but it still doesn't work.
The error it gives, is this: Both point to the sqlite3_prepare line. |
Thomas Fjellstrom
Member #476
June 2000
|
const your zErrMsg and the argv[] comes from the main(int argc, char **argv) function, never used program arguments before? -- |
Raf Vermeulen
Member #7,166
April 2006
|
std::string query; query = "CREATE TABLE dictionary (word TEXT, translation TEXT);"; sqlite3_exec(database, query.c_str(), callback, 0, NULL); That's compiled, and didn't give any runtime errors. I'm curious as to whether it actually created the table or not. Suppose I'll see that when putting in some test values. I've never actually used or heard 'bout program arguments, so the argv[n] and argc're completely new to me. Are they any use? |
Thomas Fjellstrom
Member #476
June 2000
|
if exec didnt give any errors, nor did anything you did in the call back, then yes, it created the table. Quote: I've never actually used or heard 'bout program arguments, so the argv[n] and argc're completely new to me. Are they any use? How and where did you learn to program? Every tutorial for C and C++ that I know of starts with just a main(int argc, char **argv) function and lets you pass stuff to it when running argv is how you pass info, like file names, or config arguments to a program. rather basic stuff. -- |
Raf Vermeulen
Member #7,166
April 2006
|
Ahh, those're parameters you give in the commandline part then. Never worked with those before. Never needed to for what I've been coding. |
|