views:

54

answers:

1

Hi,

I am creating a sqllite database from qt like this :

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", mConnectionName);
db.setDatabaseName("myDattabse.DB");
if (!db.open()) {  
return false;
}

When trying to insert macedonian characters in the sqllite database, the characters are inserted as ???.

According to this link http://www.qtcentre.org/threads/26202-SQLite-connection-and-encoding?highlight=unicode+and+sqllite , I need to create my database with utf-16 encoding to solve my problem. cause sqllite default encoding is utf 8. How can I do that , where to specify this option, above in my code?

A: 

From my understanding you should be fine with UTF8, pls, check if an example below would work for you:

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");

if( !db.open() )
{
    qDebug() << db.lastError();
    qFatal( "Failed to connect." );
}

QSqlQuery query;

query.prepare("CREATE TABLE IF NOT EXISTS names (id INTEGER UNIQUE PRIMARY KEY, test_field VARCHAR(30))");
query.exec();

query.prepare(QObject::trUtf8("INSERT INTO names (id, test_field) VALUES (1, 'тест1')"));
query.exec();
query.prepare(QObject::trUtf8("INSERT INTO names (id, test_field) VALUES (2, 'тест2')"));
query.exec();

QSqlQuery select("SELECT test_field FROM names");
while (select.next())
{
    qDebug() << select.value(0).toString();
}

output should be:

"тест1" 
"тест2" 

now back to your original question, I believe you can have you sqlite database in UTF-16 as default encoding if the database is opened with sqlite3_open16 function. According to QT source code (qsql_sqlite.cpp QSQLiteDriver) they are using sqlite3_open_v2 function to open the connection to database which is UTF-8, but again, there is nothing wrong with it for working correctly with Cyrillic characters.

hope this helps, regards

serge_gubenko