5

目前我的应用程序只支持 SQLite 数据库,但我想同时支持 SQLite 和 MySQL 数据库,所以我正在测试SOCI 库,看看它是否能满足我的需要。但是,尽管有示例和文档,但我无法弄清楚 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 库时,我最终放弃了 SOCI 。与 SOCI 不同,它只是对原生 C API 的一个非常薄的包装器,适合我目前的需求。

4

1 回答 1

2

该文档解释了如何使用带参数的准备好的语句

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.

于 2013-03-24T09:56:47.800 回答