Skip to content

Commit b6ae141

Browse files
committed
SQLite: Reset prepared statement that failed to execute due to an error
1 parent c0eb2dd commit b6ae141

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/shared/Database/DatabaseSqlite.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -288,38 +288,40 @@ void SqlitePreparedStatement::addParam(unsigned int nIndex, const SqlStmtFieldDa
288288

289289
switch (data.type()) {
290290
case FIELD_BOOL:
291+
result = sqlite3_bind_int(*m_stmt, nIndex + 1, data.toBool());
292+
break;
291293
case FIELD_UI8:
292-
result = sqlite3_bind_int(*m_stmt, nIndex + 1, *static_cast<const uint8*>(data.buff()));
294+
result = sqlite3_bind_int(*m_stmt, nIndex + 1, data.toUint8());
293295
break;
294296
case FIELD_UI16:
295-
result = sqlite3_bind_int(*m_stmt, nIndex + 1, *static_cast<const uint16*>(data.buff()));
297+
result = sqlite3_bind_int(*m_stmt, nIndex + 1, data.toUint16());
296298
break;
297299
case FIELD_UI32:
298-
result = sqlite3_bind_int(*m_stmt, nIndex + 1, *static_cast<const uint32*>(data.buff()));
300+
result = sqlite3_bind_int(*m_stmt, nIndex + 1, data.toUint32());
299301
break;
300302
case FIELD_UI64:
301-
result = sqlite3_bind_int64(*m_stmt, nIndex + 1, *static_cast<const uint64*>(data.buff()));
303+
result = sqlite3_bind_int64(*m_stmt, nIndex + 1, data.toUint64());
302304
break;
303305
case FIELD_I8:
304-
result = sqlite3_bind_int(*m_stmt, nIndex + 1, *static_cast<const int8*>(data.buff()));
306+
result = sqlite3_bind_int(*m_stmt, nIndex + 1, data.toInt8());
305307
break;
306308
case FIELD_I16:
307-
result = sqlite3_bind_int(*m_stmt, nIndex + 1, *static_cast<const int16*>(data.buff()));
309+
result = sqlite3_bind_int(*m_stmt, nIndex + 1, data.toInt16());
308310
break;
309311
case FIELD_I32:
310-
result = sqlite3_bind_int(*m_stmt, nIndex + 1, *static_cast<const int32*>(data.buff()));
312+
result = sqlite3_bind_int(*m_stmt, nIndex + 1, data.toInt32());
311313
break;
312314
case FIELD_I64:
313-
result = sqlite3_bind_int64(*m_stmt, nIndex + 1, *static_cast<const int64*>(data.buff()));
315+
result = sqlite3_bind_int64(*m_stmt, nIndex + 1, data.toInt64());
314316
break;
315317
case FIELD_FLOAT:
316-
result = sqlite3_bind_double(*m_stmt, nIndex + 1, *static_cast<const float*>(data.buff()));
318+
result = sqlite3_bind_double(*m_stmt, nIndex + 1, data.toFloat());
317319
break;
318320
case FIELD_DOUBLE:
319-
result = sqlite3_bind_double(*m_stmt, nIndex + 1, *static_cast<const double*>(data.buff()));
321+
result = sqlite3_bind_double(*m_stmt, nIndex + 1, data.toDouble());
320322
break;
321323
case FIELD_STRING:
322-
result = sqlite3_bind_text(*m_stmt, nIndex + 1, static_cast<const char*>(data.buff()), -1, SQLITE_STATIC);
324+
result = sqlite3_bind_text(*m_stmt, nIndex + 1, data.toStr(), -1, SQLITE_STATIC);
323325
break;
324326
case FIELD_NONE:
325327
result = sqlite3_bind_null(*m_stmt, nIndex + 1);
@@ -359,11 +361,14 @@ bool SqlitePreparedStatement::execute()
359361
{
360362
sLog.outErrorDb("SQL: cannot execute '%s'", m_szFmt.c_str());
361363
sLog.outErrorDb("SQL ERROR: %s", sqlite3_errmsg(m_pSqliteConn));
364+
sqlite3_reset(*m_stmt);
365+
sqlite3_clear_bindings(*m_stmt);
362366
return false;
363367
}
364368

365369
// Reset the prepared statement to be executed again if needed
366370
sqlite3_reset(*m_stmt);
371+
sqlite3_clear_bindings(*m_stmt);
367372

368373
return true;
369374
}

0 commit comments

Comments
 (0)