PDA

View Full Version : Insert unicode in SQlite



Kastagne
11th August 2008, 16:47
Hi all.

I have a little problem when it's come to insert a japanese caracter into my SQlite database :

With the following code :


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

if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit."), QMessageBox::Cancel);
}

QSqlQuery query;


query.exec("create table kanji (id int primary key, "
"kanji nvarchar, "
"signification nvarchar)");

query.exec("insert into kanji values(0, '<all>', '0')");
query.exec("insert into kanji values(1, '日', 'Soleil')");
query.exec("insert into kanji values(2, '火', 'Feu')");
query.exec("insert into kanji values(3, '水', 'Eau')");

QSqlTableModel *model = new QSqlTableModel;
model->setTable("kanji");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
model->removeColumn(0);
model->setHeaderData(0, Qt::Horizontal, tr("Kanji"));
model->setHeaderData(1, Qt::Horizontal, tr("Signification"));

QTableView *view = new QTableView;
view->setModel(model);
view->show();

In result i have this:

http://img87.imageshack.us/img87/3359/qtresultab1.jpg

I'm running under Qt Open source. Thanks you in advance :D

maverick_pol
11th August 2008, 17:25
Hi,

I suppose you have to set model/view locate to accept the characters. Have you done this? In the designer it can be chosen from a combo. That's interesting, I will check it today in the evening.

Kacper

Kastagne
11th August 2008, 20:55
Thanks for the answer.

I'm not sure it will work, because I tried to fill my SQLite data file with an other gui program then i tried to show the caracter with my model/view from and it work.

I think the problem occure with the "insert into" Qt . However, i'm absolutely not certain, it's why i'm asking ! :D

israelins
11th October 2011, 14:07
1- Exec this before QApplication instantiation (or declaration)
QTextCodec::setCodecForCStrings(QTextCodec::codecF orName("UTF-8"));
QTextCodec::setCodecForTr(QTextCodec::codecForName ("UTF-8"));
QTextCodec::setCodecForLocale(QTextCodec::codecFor Name("UTF-8"));

2- Certify your cpp file are UTF-8 encoded
3- Try to change your code:
query.exec("insert into kanji values(0, '<all>', '0')");
query.exec("insert into kanji values(1, 'æ—¥', 'Soleil')");
query.exec("insert into kanji values(2, '火', 'Feu')");
query.exec("insert into kanji values(3, 'æ°´', 'Eau')");

to:
query.prepare("insert into kanji values(:id, :kanji, :desc)");
query.bind(1, 0); query.bind(2, '<all>'); query.bind(3, '0'); query.exec();
query.bind(1, 1); query.bind(2, 'æ—¥'); query.bind(3, 'Soleil'); query.exec();
query.bind(1, 2); query.bind(2, '火'); query.bind(3, 'Feu'); query.exec();
query.bind(1, 3); query.bind(2, 'æ°´'); query.bind(3, 'Eau'); query.exec();

Try this and tell me the result.

If you use linux you can use sqliteman, or on windows SqliteExpert Personal Edition to open the database.