【问题标题】:How do I bind a variable to a prepared statement with the SOCI libary?如何使用 SOCI 库将变量绑定到准备好的语句?
【发布时间】:2013-03-13 17:59:21
【问题描述】:

目前我的应用程序只支持 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。与 SOCI 不同,它只是对原生 C API 的一个非常薄的包装器,适合我目前的需求。

【问题讨论】:

  • > “当我找到 cppdb 库时,我最终放弃了 SOCI”。 - 谢谢!它是 CppDB 生命中的最后一个,从那时起,该项目在过去 8 年中一直停留在 0.3.1 版本。

标签: c++ mysql database sqlite soci


【解决方案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 的生命周期一样长。

【讨论】:

  • 谢谢。我确实在文档中看到了这一点,但我希望可能有一种替代方法允许 use() 和 into() 函数稍后绑定,就在执行准备好的语句之前,所以变量没有必须与声明“随身携带”。我现在假设这不能用 SOCI 库来完成。
猜你喜欢
  • 1970-01-01
  • 2019-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-03
  • 1970-01-01
  • 2014-09-27
相关资源
最近更新 更多