SQLite Forum

QT Framework and SQLite SEE
Login

QT Framework and SQLite SEE

(1) By EagleSparrow on 2021-01-26 21:56:17 [link]

So I am implementing an encrypted database with SEE and QT Framework.

I have been trying to create the database and then open it and apply the key but when I do a query, I get the message "file is not a database" and I also get the error 26 returned.

I have done the following to initialize and create the database.

Create
1) call sqlite3_activate_see
2) call sqlite3_open_v2
3) call dbReturn = sqlite3_rekey(_pdb,key,-1);
on success I close the database then open it again.
I do this to inititialize it with the key method.

4)  call sqlite3_open_v2
5)  dbReturn = sqlite3_key(_pdb,key,-1);
So now my database should be created and have no tables in it. 
It should also be encrypted but usable.

6) Create table and insert one record to read it.
7) query the table for the record
   QString  sql = "SELECT * from MyDataTable where Id=1;"
   sqlite3_stmt *stmt;
   int rc = sqlite3_prepare_v2(_pdb, query.toUtf8().constData(), -1, &stmt,NULL);
   if (rc != SQLITE_OK)
   {
       return false;
   }
   while ((rc = sqlite3_step(stmt)) == SQLITE_ROW)
   {
      switch (sqlite3_column_type(stmt, i))
      {
     case SQLITE_TEXT:
     {
         char *pMyData16  = (char*)  sqlite3_column_text16 (stmt, i);
         int myBytes = sqlite3_column_bytes(stmt, i);
         char myData16[myBytes + 1];
         memcpy(myData16,pMyData16, myBytes );
         result.ColumnText16 = myData16;

         char *pMyData  = (char*)  sqlite3_column_text (stmt, i);
         myBytes = sqlite3_column_bytes(stmt, i);
         char myData[myBytes + 1];
         memcpy(myData,pMyData, myBytes );
         result.ColumnText = myData;
         break;
     }
}
...
My problem is that when I call the sqlite3_column_text function, I get garbage back. 
Not sure how to solve this.

(2) By Richard Hipp (drh) on 2021-01-26 23:01:13 in reply to 1 [link]

Can you provide source code for a test case that I can run and debug here?

(5) By EagleSparrow on 2021-01-27 13:41:58 in reply to 2 [link]

Hi Richard. I have emailed you most of it.

(3) By Sprezzatura on 2021-01-26 23:12:19 in reply to 1

Dumb question: I assume this works for a plain file (minus the encryption stuff)?

Have you tried specifying the actual length of the key instead of -1?

Is step (4) called immediately after (3), or in a different session? Do you need to call 'sqlite3_activate_see' again?

(4.1) By EagleSparrow on 2021-01-27 14:07:49 edited from 4.0 in reply to 3 [link]

I will try calling the 'sqlite3_activate_see again. Maybe that is it. Step 4 is after step 3.

(6) By Marco Bubke (marcob) on 2021-02-10 18:18:59 in reply to 1 [link]

int rc = sqlite3_prepare_v2(_pdb, query.toUtf8().constData(), -1, &stmt,NULL);

Is that not a dangling pointer? You call constData() on a temporary.

You could use sqlite3_prepare16_v2 or stop to use QString?

(7) By Keith Medcalf (kmedcalf) on 2021-02-10 19:24:27 in reply to 1 [link]

You are mixing text encoding lengths.  When you want the length of the UTF-16 data, you need to use sqlite3_column_bytes16.  Since the "character width" is TWO (16-bits) not ONE (8-bits), you need to add TWO bytes for the TWO zero terminator bytes.