Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » SQLite and AGUP behaving oddly

This thread is locked; no one can reply to it. rss feed Print
SQLite and AGUP behaving oddly
Raf Vermeulen
Member #7,166
April 2006

The program I'm making's almost done, but it's got two more weird bugs.

1) When there's no database, and the user presses the Clear Data button, it should create the table needed. When the table already exists, it should flush it (or destroy it and make it again.

My code for that, is:

1 open_dbase = sqlite3_open(file, &database);
2 if(open_dbase) {
3 alert("Problem opening database.", 0, 0, "&Ok", NULL, 'o', NULL);
4 sqlite3_close(database);
5 }
6 else {
7 std::stringstream query;
8 query << "DROP TABLE dictionary;";
9 result = sqlite3_exec(database, query.str().c_str(), callback, 0, NULL);
10 
11 if (result == SQLITE_OK) alert("The database is empty now.", 0, 0, "&Ok", NULL, 'o', NULL);
12 else {
13 const char* error = sqlite3_errmsg(database);
14 alert(error, 0, 0, "&Ok", 0, 'o', 0);
15 }
16 
17 query << "CREATE TABLE dictionary (word TEXT, translation TEXT);";
18 result = sqlite3_exec(database, query.str().c_str(), callback, 0, NULL);
19 
20 if (result == SQLITE_OK) alert("The database is empty now.", 0, 0, "&Ok", NULL, 'o', NULL);
21 else {
22 const char* error = sqlite3_errmsg(database);
23 alert(error, 0, 0, "&Ok", 0, 'o', 0);
24 }
25 
26 sqlite3_close(database);

It gives this error message twice:
No such table: dictionary

When I take away the Drop part and create without the table already existing, it works fine. If I do this, but when the table already exists, it says that the table already exists.

2) I've got a Quit-button at the main part of my application. Pressing Esc works as well. It first comes up and asks if you really want to quit. It works fine when pressing Esc, but when using the Quit-button, pressing Cancel still quits the program.

This's the code:

1void closebutton (void)
2{
3 simulate_keypress ((KEY_ESC << 8) | scancode_to_ascii (KEY_ESC));
4}
5 
6 
7int quit_proc(int msg, DIALOG *d, int c) {
8 (void)d;
9 (void)c;
10 
11 return (((msg == MSG_KEY) || (msg == MSG_XCHAR && c == KEY_F10 << 8))
12 && alert(0, "Quit?", 0, "Ok", "Cancel", 0, 0) == 1 ? D_CLOSE : D_O_K);
13}
14 
15 
16(...)
17 
18 DIALOG main_dlg[] =
19 {
20 /* (dialog proc) (x) (y) (w) (h) (fg)(bg) (key) (flags) (d1) (d2) (dp) (dp2) (dp3) */
21
22 /* this element just clears the screen, therefore it should come before the others */
23 { d_agup_clear_proc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL },
24
25 // Text
26 { d_agup_ctext_proc, 134, 50, 10, 0, 0, 0, 0, 0, 0, 0, (void *)"Dictionary", 0, 0 },
27 
28 // Buttons
29 { d_agup_push_proc, 90, 75, 100, 18, 0, 0, 0, 0, 0, 0, (void*)"Translate", 0, (void*)translation_form },
30 { d_agup_push_proc, 90, 95, 100, 18, 0, 0, 0, 0, 0, 0, (void*)"Add", 0, (void*)add },
31 { d_agup_push_proc, 90, 115, 100, 18, 0, 0, 0, 0, 0, 0, (void*)"Edit", 0, (void*)edit },
32 { d_agup_push_proc, 90, 135, 100, 18, 0, 0, 0, 0, 0, 0, (void*)"Delete", 0, (void*)delete_record },
33 { d_agup_push_proc, 90, 155, 100, 18, 0, 0, 0, 0, 0, 0, (void*)"Clear Data", 0, (void*)new_dbase },
34 { d_agup_push_proc, 90, 180, 100, 18, 0, 0, 0, 0, 0, 0, (void*)"Quit", 0, (void*)closebutton },
35 
36 { quit_proc, 0, 0, 0, 0, 0, 0, 27, 0, 0, 0, 0, 0, 0 },
37
38 /* the next two elements don't draw anything */
39 { d_yield_proc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL },
40 { NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL }
41 };
42 set_gui(main_dlg);

To me, it all seems pretty logical, and I can't think of why it would act the way it does.

CGamesPlay
Member #2,559
July 2002
avatar

To your first question, call query.str("") to remove the existing query from the stringstream before continuing. Right now you are trying to execute this SQL:
DROP TABLE dictionary;CREATE TABLE dictionary (word TEXT, translation TEXT);And from the SQLite docs, it will only execute the first statement in a batch.

Your quit proc looks like it will ask the user to quit everytime he presses a key, and that KEY_F10 checking seems wrong, because you ignroe the low bits in c.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Raf Vermeulen
Member #7,166
April 2006

Thanks :) The dropping and recreating of the database works now. The F10 key to quit seems to've worked all the time, too. I'm really quite stumped at this one, as it all uses the same function. It must be something like it keeps sending a 1 through when using the button, while pressing a key sends a 1 through, immediately followed by a 0. Just dunno why it'd do that, or if it's actually that, let alone how to fix it.

Go to: