c++ - 一个 C++ 应用程序无法在 Linux 上的一个线程中打开 2 个 SQLite 数据库?

标签 c++ database linux sqlite

我实际上似乎对此有问题(仅在 linux 上,用堆栈跟踪描述 here,如

==11682==    at 0x0: ??? // Here goes the error=(
==11682==    by 0x4D49BE: sqlite3_free (sqlite3.c:18155)
==11682==    by 0x102242D5: sqlite3OsInit (sqlite3.c:14162)
==11682==    by 0x1029EB28: sqlite3_initialize (sqlite3.c:107299)
==11682==    by 0x102A159F: openDatabase (sqlite3.c:108909)
==11682==    by 0x102A1B29: sqlite3_open (sqlite3.c:109156)
==11682==    by 0x1021CAB0: sqlite3pp::database::connect(char const*) (sqlite3pp.cpp:89)
==11682==    by 0x1021C6E3: sqlite3pp::database::database(char const*) (sqlite3pp.cpp:74)

) 所以我有一个线程使用 sqlitecpp 调用 2 个不同类的 2 个相似函数

函数看起来像这样

void user_control::start_work_with_db( std::string db_name )
{
    if (!is_db_set) // TODO: find out how to detach from one db and connect to another.
    {
        boost::shared_ptr<sqlite3pp::database> db_( new sqlite3pp::database(db_name.c_str())); //I could not get `db = new sqlite3pp::database(DB_name.c_str());` to compile
        db = db_;
        *lu << "Connected to "<< db_name << " database and created a table with SQLite return code: " << db->execute(command_create_users_table.c_str()) << log_util::endl;
    }
}

void users_files_service::create_files_table( std::string db_name )
{
    if(!is_db_set) // TODO: find out how to detach from one db and connect to another.
    {
        boost::shared_ptr<sqlite3pp::database> db_( new sqlite3pp::database(db_name.c_str())); //I could not get `db = new sqlite3pp::database(DB_name.c_str());` to compile
        db = db_;
        *lu << "Connected to "<< db_name << " database and created a table with SQLite return code: " << db->execute(command_create_files_table.c_str()) << log_util::endl;
        is_db_set = true;
    }
}

和 command_create_files_table 看起来像

command_create_files_table = "CREATE TABLE IF NOT EXISTS files (encoded_url varchar(300) UNIQUE NOT NULL primary key, file_name varchar(150) NOT NULL, user_name varchar(65) NOT NULL, is_public BOOLEAN NOT NULL, modified DATETIME NOT NULL default CURRENT_TIMESTAMP )";

command_create_users_table = "CREATE TABLE IF NOT EXISTS users (email varchar(100) UNIQUE NOT NULL primary key, pass varchar(100))";

db是每个类的私有(private)成员。

我想知道 - 我在提供的代码中是否存在错误,或者 SQLite 不支持从一个线程对 2 个数据库文件进行操作?

最佳答案

基地SQLite库肯定支持从同一个线程打开多个数据库文件——我在 Linux 和 Windows 上都重复这样做过。它既可行又有用,只要您不通过两次打开相同 DB 文件并尝试交错事务来扭曲自己,在这种情况下您的线程可能只是死锁 - 而不是崩溃。 p>

此行表示堆栈损坏:

==11682==    at 0x0: ???

您可能想使用 Valgrind进一步追踪这个问题。在 this past answer of mine 中有一些使用 Valgrind 的技巧。 .

关于c++ - 一个 C++ 应用程序无法在 Linux 上的一个线程中打开 2 个 SQLite 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8019851/

相关文章:

linux - 如何在不知道当前设备的情况下即时更改 ip

c++ - 替换文件中的单词

linux - 我应该长时间打开 sysfs 文件吗?

java - 如何从 NDK(JNI) 调用特定的 Java 方法?

c++ - 如何在 WAF 中构建静态库?

C++套接字设计

c++ - make_unique、工厂方法或客户端 API 的不同设计?

database - 保持密码可配置的最佳方法是什么,又不会让普通读者太容易获得密码?

php - 在我的 mysql 查询中无法正常工作的地方

android - 最安全的移动数据库策略