Using sqlite3_bind_XXX inside a loop

会有一股神秘感。 提交于 2020-01-17 01:39:04

问题


I want to do multiple parameterized inserts with SQLite in my code. For this :

I have a single prepare statement outside of my loop as :

error = sqlite3_prepare(connection, insert_sql, strlen(insert_sql), &stmt, NULL);

I want inserts within a loop as:

while ( not reached end of datafile ) {

    // Insert into server table
    sqlite3_bind_int(stmt,    1, id1);
    sqlite3_bind_double(stmt, 2, latitude);
    sqlite3_bind_double(stmt, 3, longitude);

    sqlite3_step(stmt);

}

The API docs for the function : https://www.sqlite.org/c3ref/bind_blob.html mention that :

sqlite3_step() has been called more recently than sqlite3_reset(), then the call will return SQLITE_MISUSE

Bindings are not cleared by the sqlite3_reset() routine

If any sqlite3_bind_() routine is passed a prepared statement that has been finalized, the result is undefined and probably harmful.

I am really confused as to how do I do repeated inserts with parameterized query in SQLite?


回答1:


Just call sqlite3_reset() after sqlite3_step().



来源:https://stackoverflow.com/questions/28973353/using-sqlite3-bind-xxx-inside-a-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!