如何使用SOCI库将变量绑定到预准备语句?

时间:2013-03-24 03:08:38

标签: c++ mysql database sqlite soci

目前我的应用程序仅支持SQLite数据库,但我想支持SQLite和MySQL数据库,因此我正在测试SOCI library以查看它是否能满足我的需求。然而,尽管examples and documentation,我还是无法弄清楚SOCI如何处理准备好的陈述。

使用SQLite C API时,您准备语句:

sqlite3_stmt* statement;
sqlite3_prepare_v2( database_handle_pointer,
                    "SELECT * FROM table WHERE user_id=:id;",
                    -1,
                    &statement,
                    NULL );

稍后您值绑定到:ID占位符,执行语句并逐步执行结果:

const sqlite3_int64 user_id = some_function_that_returns_a_user_id();
const int index = sqlite3_bind_parameter_index( statement, ":id" );
sqlite3_bind_int64( statement, index, user_id );

while ( sqlite3_step( statement ) == SQLITE_ROW )
{
     // Do something with the row
}

我如何使用SOCI进行此操作?看起来准备和绑定概念不像原生SQLite API那样分开。在使用soci :: use()做准备期间是否必须发生绑定?

更新1 :如果我没有充分解释这个问题:这是一个使用SQLite C API的小型,有效的C ++示例。如果我能看到这个使用SOCI重新实现,它就会回答这个问题。

#include <sqlite3.h>
#include <iostream>

// Tables and data
const char* table = "CREATE TABLE test ( user_id INTEGER, name CHAR );";
const char* hank = "INSERT INTO test (user_id,name) VALUES(1,'Hank');";
const char* bill = "INSERT INTO test (user_id,name) VALUES(2,'Bill');";
const char* fred = "INSERT INTO test (user_id,name) VALUES(3,'Fred');";

// Create a SQLite prepared statement to select a user from the test table.
sqlite3_stmt* make_statement( sqlite3* database )
{
    sqlite3_stmt* statement;
    sqlite3_prepare_v2( database,
                        "SELECT name FROM test WHERE user_id=:id;",
                        -1, &statement, NULL );
    return statement;
}

// Bind the requested user_id to the prepared statement.
void bind_statement( sqlite3_stmt* statement, const sqlite3_int64 user_id )
{
    const int index = sqlite3_bind_parameter_index( statement, ":id" );
    sqlite3_bind_int64( statement, index, user_id );
}

// Execute the statement and print the name of the selected user.
void execute_statement( sqlite3_stmt* statement )
{
    while ( sqlite3_step( statement ) == SQLITE_ROW )
    {
        std::cout << sqlite3_column_text( statement, 0 ) << "\n";
    }
}

int main()
{
    // Create an in-memory database.
    sqlite3* database;
    if ( sqlite3_open( ":memory:", &database ) != SQLITE_OK )
    {
        std::cerr << "Error creating database" << std::endl;
        return -1;
    }

    // Create a table and some rows.
    sqlite3_exec( database, table, NULL, NULL, NULL );
    sqlite3_exec( database, hank, NULL, NULL, NULL );
    sqlite3_exec( database, bill, NULL, NULL, NULL );
    sqlite3_exec( database, fred, NULL, NULL, NULL );

    sqlite3_stmt* statement = make_statement( database );

    bind_statement( statement, 2 );

    execute_statement( statement );

    // Cleanup
    sqlite3_finalize( statement );
    sqlite3_close( database );

    return 1;
}

使用SOCI部分实施的相同程序(注意标记为 HELPME 的两个存根函数)

#include <soci/soci.h>
#include <iostream>

const char* table = "CREATE TABLE test ( user_id INTEGER, name CHAR );";
const char* hank = "INSERT INTO test (user_id,name) VALUES(1,'Hank');";
const char* bill = "INSERT INTO test (user_id,name) VALUES(2,'Bill');";
const char* fred = "INSERT INTO test (user_id,name) VALUES(3,'Fred');";

soci::statement make_statement( soci::session& database )
{
    soci::statement statement =
        database.prepare << "SELECT name FROM test WHERE user_id=:id";
    return statement;
}

void bind_statement( soci::statement& statement, const int user_id )
{
    // HELPME: What goes here?
}

void execute_statement( soci::statement& statement )
{
    // HELPME: What goes here?
}

int main()
{
    soci::session database( "sqlite3", ":memory:" );

    database << table;
    database << hank;
    database << bill;
    database << fred;

    soci::statement statement = make_statement( database );
    bind_statement( statement, 2 );
    execute_statement( statement );
}

更新2 :当我找到cppdb library时,我最终放弃了社交。与SOCI不同,它只是本地C API的一个非常薄的包装器,适合我目前的需求。

1 个答案:

答案 0 :(得分:1)

该文档说明了如何使用prepared statements with parameters

int user_id;
string name;
statement st = (database.prepare << "SELECT name FROM test WHERE user_id = :id",
                use(user_id),
                into(name));

user_id = 1;
st.execute(true);

请注意,user_idname变量的生命周期必须至少与st一样长。