如果绑定文本包含短划线

时间:2016-11-08 15:17:39

标签: sqlite prepared-statement bind

我遇到的问题是TABLE的TEXT字段无法绑定到包含短划线的字符串。如果我针对任何字符删除或替换短划线/破折号,则准备好的和绑定语句可以正常工作。

这里有两个代码片段。演示表就像“创建表MyTable(SomeTime text)”

//do not work... runs into commit!! but add nothing to MyTable
int result = sqlite3_exec(validSqlite3, "begin;", nullptr, nullptr, nullptr);
if (result != SQLITE_OK)
    return;
sqlite3_stmt* stmt = nullptr;
bool ok = true;
if (sqlite3_prepare_v2(validSqlite3, "insert into MyTable (SomeTime) values (?);", -1, &stmt, nullptr) != SQLITE_OK)
    ok = false;
if (ok && sqlite3_bind_text(stmt, 1, "2016-11-01 12:00:00", -1, SQLITE_TRANSIENT) != SQLITE_OK)
    ok = false;
if (ok && sqlite3_step(stmt) != SQLITE_DONE)
    ok = false;
if (ok && sqlite3_finalize(stmt) != SQLITE_OK)
    ok = false;
if (ok)
    sqlite3_exec(validSqlite3, "commit;", nullptr, nullptr, nullptr);
else
    sqlite3_exec(validSqlite3, "rollback;", nullptr, nullptr, nullptr);

//same code as above... BUT: no dashes in the text field    
//works fine... runs into commit and inserts a new row in MyTable with column SomeTime content: "2016/11/01 12:00:00"
int result = sqlite3_exec(validSqlite3, "begin;", nullptr, nullptr, nullptr);
if (result != SQLITE_OK)
    return;
sqlite3_stmt* stmt = nullptr;
bool ok = true;
if (sqlite3_prepare_v2(validSqlite3, "insert into MyTable (SomeTime) values (?);", -1, &stmt, nullptr) != SQLITE_OK)
    ok = false;
if (ok && sqlite3_bind_text(stmt, 1, "2016/11/01 12:00:00", -1, SQLITE_TRANSIENT) != SQLITE_OK)
    ok = false;
if (ok && sqlite3_step(stmt) != SQLITE_DONE)
    ok = false;
if (ok && sqlite3_finalize(stmt) != SQLITE_OK)
    ok = false;
if (ok)
    sqlite3_exec(validSqlite3, "commit;", nullptr, nullptr, nullptr);
else
    sqlite3_exec(validSqlite3, "rollback;", nullptr, nullptr, nullptr);

0 个答案:

没有答案