Results 1 to 11 of 11

Thread: problem creating a mysql database

  1. #1
    Join Date
    Aug 2009
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default problem creating a mysql database

    Using Qt4.5.2 in Suse 11.1

    I am trying to create a mysql database with the following code:

    QSqlDatabase maindb = QSqlDatabase::addDatabase("QMYSQL", "main");
    maindb.setDatabaseName ("mysql");
    maindb.setHostName (url.host());
    maindb.setUserName (url.user());
    maindb.setPassword (url.pass());
    if (!maindb.open()) {
    qFatal("opening maindb");
    }
    QSqlQuery qm(maindb);
    QString qs = QString("CREATE DATABASE %1;").arg(dbName);
    qm.prepare (qs);
    if (!qm.exec()) {
    buildError (<snip>);
    rc = 1;
    }
    maindb.close();
    QSqlDatabase::removeDatabase (maindb.connectionName());


    The exec() call fails with no useful information in the returned QSqlError. The mysql log shows only the following:

    090819 14:34:20 8 Connect tonyb@localhost on mysql
    8 Init DB mysql
    8 Query SET NAMES utf8
    8 Quit

    i.e. the create database statement never appears.

    This code, with the same userid, works okay in Qt3, so it's not a permissions problem. The log there shows

    18 Connect tonyb@localhost on mysql
    18 Init DB mysql
    18 Query CREATE DATABASE KMyMoney
    18 Quit

    Any ideas? Workarounds?

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: problem creating a mysql database

    You should check every statement that could fail (prepare() can fail, too, for example).
    Also, try removing the semi-colon ";" at the end of the query.
    (I remember that was a problem for me once, I was using Oracle, though.)

    HTH

  3. The following user says thank you to caduel for this useful post:

    TonyB (20th August 2009)

  4. #3
    Join Date
    Aug 2009
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: problem creating a mysql database

    Many thanks; it was the prepare that was failing with message
    Query error No 1295: This command is not supported in the prepared statement protocol yet QMYSQL3: Unable to prepare statement

    Now to find a workaround.

  5. #4
    Join Date
    Aug 2009
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: problem creating a mysql database

    For the record, removing the prepare() and executing the statement directly fixed the problem. Thanks again.

  6. The following user says thank you to TonyB for this useful post:

    Raccoon29 (3rd December 2009)

  7. #5
    Join Date
    Jan 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: problem creating a mysql database

    Quote Originally Posted by TonyB View Post
    For the record, removing the prepare() and executing the statement directly fixed the problem. Thanks again.
    Hi,

    I'm using on linux
    Mysql 5.0.77-1
    QT Creator 1.3.1
    QT Libs 4.5.1 (due to Distro compatibility)

    This is my code to create DB Schema:

    Qt Code:
    1. void MainWindow::creadb()
    2. {
    3. QString db_conti = "conti1";
    4.  
    5. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    6. db.setHostName("localhost");
    7. db.setDatabaseName("mysql");
    8. db.setUserName("root");
    9. db.setPassword(ui->password->text());
    10. if (db.open())
    11. {
    12. QSqlQuery query;
    13. query.exec("create database "+db_conti);
    14.  
    15. qWarning()<<query.lastError();
    16.  
    17. ... code for creating tables, views and users...
    18.  
    19. db.close();
    20.  
    21. }
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 

    I'm getting with qWarning() the same your error:

    QSqlError(1295, "QMYSQL3: Unable ro prepare statement", "This command is not supported in the prepared statement protocol yet")

    But DB Schema is created. Also tables views and users are created without problems.

    Do I ignore that error?

    Thanks.

  8. #6
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: problem creating a mysql database

    Quote Originally Posted by repaco View Post
    I'm getting with qWarning() the same your error:

    QSqlError(1295, "QMYSQL3: Unable ro prepare statement", "This command is not supported in the prepared statement protocol yet")

    But DB Schema is created. Also tables views and users are created without problems.

    Do I ignore that error?

    Thanks.
    Can you post your codes where the warning occurs here?

  9. #7
    Join Date
    Jan 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: problem creating a mysql database

    Hi saa7_go,
    sorry delay in answer, I was on holiday.

    My complete code is in my previous post.
    Row 13
    Qt Code:
    1. query.exec("create database "+db_conti);
    To copy to clipboard, switch view to plain text mode 

    db_conti contains DB Schema Name

    and row 15 to get output log about this command.

    DB Schema is created, but query.lastError() gives me error on create database.

    thanks in advance

  10. #8
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: problem creating a mysql database

    Are you sure that this part of your code
    Quote Originally Posted by repaco View Post
    Qt Code:
    1. void MainWindow::creadb()
    2. {
    3. .....
    4. QSqlQuery query;
    5. query.exec("create database "+db_conti);
    6.  
    7. qWarning()<<query.lastError();
    8. .....
    9. }
    To copy to clipboard, switch view to plain text mode 
    prints the warning? Because, you don't do prepare statement like TonyB did.

    Qt Code:
    1. QSqlQuery qm(maindb);
    2. QString qs = QString("CREATE DATABASE %1").arg(dbName);
    3. qm.prepare (qs);
    4. if (!qm.exec()) {
    5. ....
    6. }
    To copy to clipboard, switch view to plain text mode 

    I've tried your code. No error or warning occurs.

  11. #9
    Join Date
    Jan 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: problem creating a mysql database

    Hi again,
    As ToniB wrote, using qm.prepare (qs), no DB Schema is created, with non evidence of error.

    ToniB suggested to remove the prepare() and execute the statement directly.

    Executing directly, DB Schema is created, but I get error.

    It is not a big problem, since DB is created, but I'd like, if it is possible, to fix the problem.

    Tks for answer.

  12. #10
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: problem creating a mysql database

    I think it is a bug . If you want to fix the problem, use Qt 4.6.0 or above.
    Last edited by saa7_go; 21st July 2010 at 02:32. Reason: updated contents

  13. #11
    Join Date
    Jan 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: problem creating a mysql database

    many thanks.

    Ciao.

Similar Threads

  1. Replies: 3
    Last Post: 9th February 2011, 21:30
  2. Replies: 3
    Last Post: 6th May 2009, 12:16
  3. Problem When Creating my own Slot
    By Fatla in forum Qt Programming
    Replies: 12
    Last Post: 6th June 2008, 15:44
  4. Issues regarding QMySql drivers and mysql database
    By bera82 in forum Qt Programming
    Replies: 2
    Last Post: 10th August 2006, 18:50
  5. Replies: 8
    Last Post: 7th March 2006, 14:40

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.