SQLite sqlite3_column_text UTF8 to String


#1

Anyone been able to convert the SQLITE_UTF8 encoded string returned by sqlite3_column_text to a String?

I’ve tried FromUTF8() with no success.

SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
  const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
  columnMallocFailure(pStmt);
  return val;
}

calls:

SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
  return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
}

fromUTF8(() doesn’t like the unsigned char type.

Thanks,

Rail


#2

Don’t understand the problem - you can just reinterpret_cast the unsigned char* to a const char*, right?


#3

I had tried that and it didn’t work… Let me see if it’s something else… (It compiled, but the value wasn’t correct).

Thanks,

Rail


#4

Oh my…you’re writing directly for the sqlite3 API? How horrendous! VFLib provides a customized version of soci, a very clean database wrapper, which works exclusively with SQLite. I have provided conversions to and from JUCE types so they can be used quite easily. It is much easier to use than straight sqlite.

Have a look at into_type.cpp, search for x_juceString.


#5

Oh my…you’re writing directly for the sqlite3 API? How horrendous! VFLib provides a customized version of soci, a very clean database wrapper, which works exclusively with SQLite. I have provided conversions to and from JUCE types so they can be used quite easily. It is much easier to use than straight sqlite.

Have a look at into_type.cpp, search for x_juceString.[/quote]

Horrendous :smiley:

It was much faster for me to read a couple of books in the last 2 days on SQLite than to wade through your source code… Or do you have any example code? The SQLite API is ridiculously simple… Everything appears to be working fine except my conversion to a Juce::String

I’ll check through your code though and I’m sure it’ll give me a hint of what my problem is.

Thanks,

Rail


#6
            int bytes = sqlite3_column_bytes (stmt, iCol);

            const CharPointer_UTF8::CharType* c = reinterpret_cast<const CharPointer_UTF8::CharType*>(sqlite3_column_text (stmt, iCol));
            
            String szJuceString = String (CharPointer_UTF8 (c), CharPointer_UTF8 (c + bytes));

Works! I had been trying different variations of the same idea but never got the right result…

Thanks,

Rail


#7

I just cribbed everything from soci so most of their example code works, after a little modification.

There’s more information in this thread. But for comparison purposes, this is an example of performing SELECT:

String filename; statement st = (sql.prepare << "select filename from myfiles", into(filename));

I think this is a lot nicer than the alternative! It uses C++ overloading to make things easy.


#8

Thanks Vin

I’ll check them out… I still run into issues adding your VFlib code to my projects (vflib core for example) which are set up for AAX & RTAS with redefinitions… so haven’t been able to get them going yet. I’m going to have to make some time at some point to delve into that.

Best,

Rail


#9

I have had good luck with this solution: http://www.rawmaterialsoftware.com/viewtopic.php?f=6&t=5005&p=27045&hilit=sqlite#p27045