Results 1 to 3 of 3

Thread: QODBC for Postgresql on Windows

  1. #1
    Join Date
    Apr 2010
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default QODBC for Postgresql on Windows

    Hi all,

    I tried to run an app with QPSQL, but I found that it doesn't support UNICODE:

    qt1.png

    and from psql shell:

    postgresql2.png

    I inserted first row with psql shell therefore you can see unicode is supported inside postgresql.
    As postgresql docs says (link):

    "Programs using libpq directly need to be aware of this, but not much else does."
    So I'm trying to run a test app with QODBC, even if qt docs says that it's better to use specific driver (QPSQL) link:
    Note: You should use native drivers in preference to the ODBC driver where they are available. ODBC support can be used as a fallback for compliant databases if no native drivers are available.
    I installed driver manager too (following this thread instructions):
    posgresql1.png

    then compiled and installed qt odbc wrapper:
    qt0.jpg

    this is my code:


    Qt Code:
    1. //main.cpp:
    2. #include <QCoreApplication>
    3. #include <QSqlRecord>
    4. #include <QSqlQuery>
    5. #include <QSqlError>
    6. #include <QSqlDriver>
    7. #include <QDebug>
    8.  
    9. class Database
    10. {
    11. public:
    12. explicit Database();
    13. ~Database();
    14. bool connection();
    15. private:
    16. QString driverName;
    17. };
    18.  
    19. Database::~Database()
    20. {
    21. db.close();
    22. }
    23.  
    24. Database::Database() :
    25. db(QSqlDatabase::addDatabase("QODBC"))
    26. //db(QSqlDatabase::addDatabase("QODBC3"))//tried even with this
    27. {
    28. driverName = QString("QODBC");
    29. db.setDatabaseName("Driver={PostgreSQL};Server=localhost;Port=5432;Database=prova;Uid=postgres;Pwd=*******;");
    30. //db.setConnectOptions("SQL_ATTR_ODBC_VERSION=SQL_OV_ODBC3");//tried even with this
    31. }
    32.  
    33. bool Database::connection()
    34. {
    35. if (!db.open()) {
    36. qDebug() << QString("Unable to establish a database connection.\n"
    37. "This example needs %1 support. Please read "
    38. "the Qt SQL driver documentation for information how "
    39. "to build it.\n\n"
    40. "Click Cancel to exit.").arg(driverName);
    41. return false;
    42. }
    43. return true;
    44. }
    45.  
    46. bool insertTest()
    47. {
    48. q.prepare("INSERT INTO p(d) VALUES(:d)");
    49. q.bindValue(":d",QString("Questa è un altra prova"));
    50. return q.exec();
    51. }
    52.  
    53. int main(int argc, char **argv)
    54. {
    55. QCoreApplication app(argc,argv);
    56. const QScopedPointer<Database> database(new Database());
    57. database->connection();
    58. if(insertTest())
    59. {
    60. qDebug() << "query executed successfully";
    61. }else{
    62. qDebug() << "insert query error";
    63. }
    64. q.exec("SELECT * FROM p");
    65. while(q.next())
    66. {
    67. QSqlRecord r = q.record();
    68. qDebug() << "id=" << r.value("id").toInt();
    69. qDebug() << "d=" << r.value("d").toString() << '\n';
    70. }
    71. return app.exec();
    72. }
    To copy to clipboard, switch view to plain text mode 

    and this is my .pro file:
    Qt Code:
    1. QT += core gui sql widgets
    2. TARGET = ScopedPointer
    3. CONFIG += console
    4. TEMPLATE = app
    5.  
    6. sql-plugins += odbc
    7.  
    8. SOURCES += main3.cpp
    9.  
    10. OBJECTS_DIR = build/o
    11. MOC_DIR = build/moc
    12. UI_DIR = build/ui
    13. RCC_DIR = build/rcc
    14. DESTDIR = bin
    To copy to clipboard, switch view to plain text mode 
    I added sql-plugins directive following this thread

    Anyway i get this error message:
    Qt Code:
    1. "QODBCResult::exec: No statement handle available" Error: "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified"
    To copy to clipboard, switch view to plain text mode 

    as you can see here:
    error.png

    I'm using Qt 5.2.0. If you need any other information please ask.
    Any help would be appreciated, thanks!
    Alberto

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QODBC for Postgresql on Windows

    Quote Originally Posted by AlbertoN View Post
    Hi all,
    I tried to run an app with QPSQL, but I found that it doesn't support UNICODE:
    Nonsense.
    The problem is that qDebug converts Unicode to the local code page. Often, Windows uses a different code page for character session and another code page for GUI session. For applications always be notified the GUI code page.
    PS.
    psql warns You about this, look at second picture.
    Last edited by Lesiok; 23rd January 2014 at 14:54.

  3. #3
    Join Date
    Apr 2010
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QODBC for Postgresql on Windows

    Quote Originally Posted by Lesiok View Post
    Nonsense.
    The problem is that qDebug converts Unicode to the local code page. Often, Windows uses a different code page for character session and another code page for GUI session. For applications always be notified the GUI code page.
    PS.
    psql warns You about this, look at second picture.
    yes, you're right. After a whole day spent fighting against Windows I found how to fix that.

    I'm going to write it down here for other users with same issue:

    1. change windows' console codepage:
    you can set it up with prompt command: "chcp 1252" but it works just for current session. To make it default codepage, you have to open regedit, locate AutoRun in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor\ subdirectory. If it doesn't exists, as in my case, you'll create an entry as "string" and put "chcp 1252" as value in it.

    2. after previous change you shouldn't be able to see any accents in console due to the fact that windows default font is a "Raster Font". To change it, you have to open a psql shell-> right click -> properties -> Fonts and switch to "Lucida Console". Do the same on a common console (I don't know why. They should be the same... maybe my mistake i'm a bit tired).

    3. after that you have to set up postgresql shell config to WIN1252 encoding. To do that open as admin your runpsql.bat file. In my case there's a psql shell link on desktop -> right click->Shortcut->"open file location" button. It opens a path like this: C:\Program Files (x86)\PostgreSQL\9.3\scripts\runpsql.bat. And change it as follows (to save changes you have to open it as Admin):

    Qt Code:
    1. [....]
    2. for /f "delims=" %%a in ('chcp ^|find /c "932"') do @ SET CLIENTENCODING_JP=%%a
    3. SET PGCLIENTENCODING=WIN1252
    4. if "%CLIENTENCODING_JP%"=="1" SET /P PGCLIENTENCODING="Client Encoding [%PGCLIENTENCODING%]: "
    5. [...]
    To copy to clipboard, switch view to plain text mode 

    4. create a db with right encoding inside psql shell: create database test with encoding 'UTF8';

    That's all.

    I hope this could be useful to someone else.
    Thanks Lesiok for your support.
    Alberto

    P.S.: Anyway I can't run that code with QODBC driver and I can't understand why...
    Last edited by AlbertoN; 23rd January 2014 at 19:07.

Similar Threads

  1. Replies: 2
    Last Post: 11th February 2011, 17:53
  2. How I can activate QODBC in Qt4 for windows
    By vcp in forum Qt Programming
    Replies: 6
    Last Post: 2nd July 2010, 06:45
  3. POSTGRESQL driver for windows
    By drave in forum Newbie
    Replies: 2
    Last Post: 2nd April 2010, 12:44
  4. PostgreSQL plugin under Windows
    By aekilic in forum Newbie
    Replies: 1
    Last Post: 22nd January 2007, 06:43
  5. PostgreSQL and Windows Community Version
    By graeme in forum Installation and Deployment
    Replies: 23
    Last Post: 20th April 2006, 19:29

Tags for this Thread

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.