【问题标题】:Sqlite for windows phone 8.1Windows Phone 8.1 的 Sqlite
【发布时间】:2014-09-21 00:23:58
【问题描述】:

我已经在 Visual Studio 2013 上安装了用于 windows phone 8.1 扩展的 sqlite (http://visualstudiogallery.msdn.microsoft.com/5d97faf6-39e3-4048-a0bc-adde2af75d1b)

现在我正在尝试弄清楚如何在 windows phone C++ XAML 应用程序中使用它。这个怎么做?我似乎找到的所有示例都是针对 C#。他们要求在安装扩展后安装 sqlite3-net 包。

我想可能可以使用本机 C API,但最好有一个 C++/CX 包装器。

【问题讨论】:

    标签: c++ windows-phone-8.1 c++-cx


    【解决方案1】:

    编写自己的包装器。

    老实说,您不需要包装器,只需打开 Db,对其进行查询,处理您的结果,然后关闭 Db。就是这样。

    这是我的代码sn-p作为例子

    void QuestionManager::LoadQuestion(int questionId)
    {
        string dbPath = "my/path/question.sqlite";
        if(sqlite3_open(dbPath.c_str(),&pDB) == SQLITE_OK)
        {
            LOG("Open data base success");
    
            ostringstream ss;
            ss << "Select * from question1 where id=" << questionId;
    
            string sqlCmd = ss.str();
            sqlite3_stmt *stmt;
            if(sqlite3_prepare_v2(pDB, sqlCmd.c_str(), -1, &stmt, NULL) == SQLITE_OK)
            {
                while (sqlite3_step(stmt) == SQLITE_ROW)
                {
                    Question q = Question();
                    q.question = (const char*)sqlite3_column_text(stmt, 1);
                    q.caseA = (const char*)sqlite3_column_text(stmt, 2);
                    q.caseB = (const char*)sqlite3_column_text(stmt, 3);
                    q.caseC = (const char*)sqlite3_column_text(stmt, 4);
                    q.caseD = (const char*)sqlite3_column_text(stmt, 5);
                    q.result = (const char*)sqlite3_column_text(stmt, 6);
    
                    arrQuestion[i] = q;
                }
            }
    
            sqlite3_close(pDB);
        }
        else
        {
            LOG("cannot open DB %s", dbPath.c_str());
        }
    }
    

    【讨论】:

    • 包装器并不像您建议的那样无用。 SQLite 本机使用 UTF8,而 Platform::String^ 使用宽字符。也就是说,主要问题不是编写查询数据库的代码,而是让它工作。将 SQLite 源代码与项目一起编译会产生很多错误。无论如何,我已经设法解决了这个问题。 WP8.1 SQLite 扩展附带一个标头和 .dll,可以作为对项目的引用添加。现在的另一个问题:如何在编译时将现有数据库添加到项目中?
    • 您可以搜索转换方法来将 Platform::String^ 转换为 UTF8 字符序列,反之亦然 (stackoverflow.com/questions/11746146/…)。我使用扩展来稳定和舒适。对于另一个问题:将其添加为其他资产。 :)
    • “将其添加为其他资产”再次只是部分答案,因为数据库文件不可写。看起来你实际上并没有尝试过你建议的东西!无论如何,如果将数据库文件添加到“资产”文件夹中,则可以找到该数据库文件。然后必须将其复制到其他地方。这些位置中的任何一个都可以。 Windows::Storage::ApplicationData::Current->LocalFolder Windows::Storage::ApplicationData::Current->LocalCacheFolder
    猜你喜欢
    • 2014-08-27
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多