Results 1 to 4 of 4

Thread: Insert unicode in SQlite

  1. #1
    Join Date
    Aug 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Insert unicode in SQlite

    Hi all.

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

    With the following code :

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName("test.db");
    3.  
    4. if (!db.open()) {
    5. QMessageBox::critical(0, qApp->tr("Cannot open database"),
    6. qApp->tr("Unable to establish a database connection.\n"
    7. "This example needs SQLite support. Please read "
    8. "the Qt SQL driver documentation for information how "
    9. "to build it.\n\n"
    10. "Click Cancel to exit."), QMessageBox::Cancel);
    11. }
    12.  
    13. QSqlQuery query;
    14.  
    15.  
    16. query.exec("create table kanji (id int primary key, "
    17. "kanji nvarchar, "
    18. "signification nvarchar)");
    19.  
    20. query.exec("insert into kanji values(0, '<all>', '0')");
    21. query.exec("insert into kanji values(1, '日', 'Soleil')");
    22. query.exec("insert into kanji values(2, '火', 'Feu')");
    23. query.exec("insert into kanji values(3, '水', 'Eau')");
    24.  
    25. model->setTable("kanji");
    26. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    27. model->select();
    28. model->removeColumn(0);
    29. model->setHeaderData(0, Qt::Horizontal, tr("Kanji"));
    30. model->setHeaderData(1, Qt::Horizontal, tr("Signification"));
    31.  
    32. QTableView *view = new QTableView;
    33. view->setModel(model);
    34. view->show();
    To copy to clipboard, switch view to plain text mode 

    In result i have this:



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

  2. #2
    Join Date
    May 2007
    Location
    Lublin, Poland
    Posts
    345
    Thanks
    40
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Insert unicode in SQlite

    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
    Qt allows you to use everything you want
    wysota
    --------------------------------------------------------------------------------
    #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
    abort(); // trap; generates core dump
    #else
    exit(1); // goodbye cruel world
    #endif

  3. #3
    Join Date
    Aug 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Insert unicode in SQlite

    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 !

  4. #4
    Join Date
    Jul 2010
    Location
    Fortaleza, Brazil
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Insert unicode in SQlite

    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.

Similar Threads

  1. Bulk insert into SQLite
    By munna in forum Qt Programming
    Replies: 6
    Last Post: 19th November 2007, 03:56

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.