SQLite Forum

Get SQLITE_PROTOCOL error when opening database

Get SQLITE_PROTOCOL error when opening database

(1) By anonymous on 2021-10-26 14:32:36 [source]

Hi, Can anyone help with this error? I am using this code in UWP C++/WinRT

sqlite3** m_dB;
const char* m_szFile = "DataBase/db.sqlite3";
char* sqlite3_temp_directory;

void UI_Database::InitializeDatabase(void)
    int sqlError = SQLITE_OK;
    LPCWSTR zPath = ApplicationData::Current().TemporaryFolder().Path().data();
    char zPathBuf[MAX_PATH + 1];
    memset(zPathBuf, 0, sizeof(zPathBuf));
    WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf), NULL, NULL);
    sqlite3_temp_directory = sqlite3_mprintf("%s", zPathBuf);

    sqlError = sqlite3_open(m_szFile, m_dB);

    if (sqlError == SQLITE_OK)

All I every get for sqlError is 15 (SQLITE_PROTOCOL). It never tries to create the file if missing. I suspect it might be a permissions thing in Win10. Thanks.

(2.1) By Larry Brasfield (larrybr) on 2021-10-26 14:45:26 edited from 2.0 in reply to 1 [link] [source]

The object known as "m_dB" does not have a value which can sensibly be passed to sqlite3_open(). This is a fundamental problem, unrelated to Win10 permissions.

(Added via edit:) Also, your use of sqlite3_temp_directory represents a memory leak.

(3) By anonymous on 2021-10-26 16:37:43 in reply to 2.1 [link] [source]

Update - I dumped all the code above and tried a different scheme. I used a wrapper for SQLite3 from https://github.com/neosmart/CppSQLite and used the same .lib and .dll files. Everything compiles and I get a different error, which is 14 or SQLITE_CANTOPEN. I suspected that was the problem. Why it was one off? Here is the code.

CppSQLite3DB m_dB; const char* m_szFile = "db.sqlite3"; const char* m_szFilesTable = "Files";

void UI_Database::InitializeDatabase(void) { bool fileTableExists = false; const char* ver; int verNumb = 0;

ver = CppSQLite3DB::SQLiteHeaderVersion();
ver = CppSQLite3DB::SQLiteLibraryVersion();
verNumb = CppSQLite3DB::SQLiteLibraryVersionNumber();

    // Open the database.

    // Check if database contains the 'Files' table.
    fileTableExists = m_dB.tableExists(m_szFilesTable);

    if (fileTableExists == false)
catch (CppSQLite3Exception& e)
    // Show the exception.
    ver = e.errorMessage();


Everything works except for getting an exception when open on the database is called. It returns SQLITE_CANTOPEN. I think it is getting blocked from opening by WinRT, but I don't know how to give it access. I just want to confirm my suspicions. Thanks.

(4) By Larry Brasfield (larrybr) on 2021-10-26 17:22:22 in reply to 3 [link] [source]

It seems odd to me that you abandon code with simple, identified errors in favor of courting new ones. Leaving that aside ...

Also leaving aside the wisdom of throwing an exception for failure to .open() ...

What is the absolute path which your "db.sqlite3" filename resolves to? Or, what is the process current directory when the code you now have is run? Is the directory in which you try to create a database file one which you can modify without unusual permissions?

As part of diagnosing your trouble, I suggest setting m_szFile to name a file in a a directory you can create files in. It should contain the whole path, starting from a filesystem root. That way, the process current directory will not matter.

(5) By RandomCoder on 2021-10-26 17:57:57 in reply to 3 [link] [source]

On WinRT, it's fairly unlikely that the processes current directory is writable. You'll need to explicitly specify one of the writable directories.

Also, how are you compiling the SQLite library?

I don't see any mention of setting the SQLITE_OS_WINRT macro that's necessary for SQLite to function on WinRT.

(6) By anonymous on 2021-10-26 18:06:45 in reply to 4 [link] [source]

The file is in my home folder for the project hence the lack of any path on the file. SQLite says the full path is not necessary so I did not use it. The reason I dumped the code that it was not working. I have used the wrapper previously in another project that was written in MFC and it worked flawlessly. I always try to use things that work, but in this case I was following the example on sqlite.org first, and it did not work. I moved on because this has been so frustratingly hard to get to work.

(7) By anonymous on 2021-10-26 18:17:08 in reply to 5 [link] [source]

I am doing nothing special for the compilation. I just added the 3 files that were the wrapper for SQLite to my project as normal. Then I added winsqlite3.dll and .lib files to the project also. Everything compiles with no errors. I did not no about the MACRO, so I will check that out. While waiting for an answer I was messing around with the project and I did something (don't know what) but now the open function works. I can only guess that I fixed the project scope with the database file because that was the only thing I was changing. Strange!!! I am happy now.