0

I have installed the sqlite for windows phone 8.1 extension on visual studio 2013 (http://visualstudiogallery.msdn.microsoft.com/5d97faf6-39e3-4048-a0bc-adde2af75d1b)

Now I'm trying to figure out how to use it in a windows phone C++ XAML application. How to do this? All the examples I seem to find are for C#.They ask to install the sqlite3-net package following installation of the extension.

I guess it may be possible to use the native C APIs but it would be good to have a C++/CX wrapper.

seggaeman
  • 171
  • 1
  • 10

1 Answers1

0

Write your own wrapper.

Honestly you don't need a wrapper, just open the Db, query on it, handle your result, close the Db. That's it.

This is my code snippet as an example

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());
    }
}
Hai Nguyen
  • 81
  • 10
  • A wrapper isn't so useless as you are suggesting. SQLite uses UTF8 natively whereas Platform::String^ uses wide chars. That said the major problem is not writing the code for querying the database, but rather getting it working. Compiling the SQLite source code along with the project gives lots of errors. Anyway I've managed to solve that. The WP8.1 SQLite extension ships with a header and .dll that can be added as reference to the project. Another question now: how to add an existing database to the project at compile time? – seggaeman Jul 29 '14 at 16:50
  • You can search for a conversion method to convert Platform::String^ to UTF8 char sequence and vice versa (http://stackoverflow.com/questions/11746146/how-to-convert-platformstring-to-char). I used the extension for stable and comfortable. For the other question: add it as other assets. :) – Hai Nguyen Jul 30 '14 at 11:16
  • "add it as other assets" is again only a part answer, since the database file won't be writeable. It doesn't look like you have actually tried the stuff you are suggesting! Anyway the database file can be located if added to the "Assets" folder. It must then be copied somewhere else. Either of these locations will do. Windows::Storage::ApplicationData::Current->LocalFolder Windows::Storage::ApplicationData::Current->LocalCacheFolder – seggaeman Aug 20 '14 at 13:04