almost have sqlite storage working (in memory, not file).

but test_teach_magic crashes.
This commit is contained in:
Enno Rehling 2017-11-09 21:33:42 +01:00
parent 0cd9651690
commit 89bf17cdca
2 changed files with 29 additions and 3 deletions

View File

@ -7,6 +7,7 @@
#include <sqlite3.h>
#include <assert.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
@ -20,9 +21,26 @@ order_data *db_load_order(int id)
{
if (id > 0) {
order_data * od = NULL;
odata_create(&od, 0, NULL);
int err;
err = sqlite3_reset(g_stmt_select);
assert(err == SQLITE_OK);
err = sqlite3_bind_int(g_stmt_select, 1, id);
assert(err == SQLITE_OK);
do {
err = sqlite3_step(g_stmt_select);
if (err == SQLITE_ROW) {
const unsigned char *text;
int bytes;
bytes = sqlite3_column_bytes(g_stmt_select, 0);
assert(bytes > 0);
text = sqlite3_column_text(g_stmt_select, 0);
odata_create(&od, (size_t)bytes, (const char *)text);
return od;
}
} while (err == SQLITE_ROW);
assert(err == SQLITE_DONE);
}
return NULL;
}
@ -30,12 +48,16 @@ int db_save_order(order_data *od)
{
if (od->_str) {
int err;
sqlite3_int64 id;
err = sqlite3_reset(g_stmt_insert);
assert(err == SQLITE_OK);
err = sqlite3_bind_text(g_stmt_insert, 1, od->_str, -1, SQLITE_STATIC);
assert(err == SQLITE_OK);
err = sqlite3_step(g_stmt_insert);
assert(err == SQLITE_DONE);
id = sqlite3_last_insert_rowid(g_db);
assert(id <= INT_MAX);
return (int)id;
}
return 0;
}
@ -50,12 +72,16 @@ void db_open(void)
assert(err == SQLITE_OK);
err = sqlite3_prepare_v2(g_db, "INSERT INTO orders (data) VALUES (?)", -1, &g_stmt_insert, NULL);
assert(err == SQLITE_OK);
err = sqlite3_prepare_v2(g_db, "SELECT data FROM orders WHERE id = ?", -1, &g_stmt_select, NULL);
assert(err == SQLITE_OK);
}
void db_close(void)
{
int err;
err = sqlite3_finalize(g_stmt_select);
assert(err == SQLITE_OK);
err = sqlite3_finalize(g_stmt_insert);
assert(err == SQLITE_OK);
err = sqlite3_close(g_db);

View File

@ -371,7 +371,7 @@ int teach_cmd(unit * teacher, struct order *ord)
token = getstrtoken();
/* Beginne die Fehlermeldung */
if (isparam(token, teacher->faction->locale, P_TEMP)) {
if (token && isparam(token, teacher->faction->locale, P_TEMP)) {
token = getstrtoken();
sprintf(tbuf, "%s %s", LOC(teacher->faction->locale,
parameters[P_TEMP]), token);