Results 1 to 3 of 3

Thread: How to create mysql database table in Qt?

  1. #1
    Join Date
    Nov 2015
    Posts
    3
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows Android

    Default How to create mysql database table in Qt?

    mydb = QSqlDatabase::addDatabase("QMYSQL");
    mydb.setDatabaseName("mylogin");

    mydb.setUserName("root");
    mydb.setPort(3306);
    mydb.setHostName("localhost");
    mydb.setPassword("");

    if(!mydb.open())
    {
    qDebug() << "failed" ;
    qDebug() << mydb.lastError().text();
    }
    QSqlQuery qry;
    qry.prepare("CREATE TABLE RSA(name varchar(20);");
    if(qry.exec())
    {
    QMessageBox msg;
    msg.setText("Data saved");
    msg.exec();
    mydb.close();
    mydb.removeDatabase(QSqlDatabase::defaultConnectio n);
    }
    else
    {
    QMessageBox msg2;
    msg2.setText("Failed to save data");
    msg2.exec();
    qDebug() << mydb.lastError().text();
    }
    }

  2. #2
    Join Date
    Jun 2014
    Posts
    98
    Thanks
    43
    Thanked 4 Times in 4 Posts
    Platforms
    Windows

    Default Re: How to create mysql database table in Qt?

    What do you want to happen, and what has happened?

  3. #3
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How to create mysql database table in Qt?

    You are missing a right parenthesis on your create statement. Should be the following:

    Qt Code:
    1. qry.prepare("CREATE TABLE RSA(name varchar(20));");
    To copy to clipboard, switch view to plain text mode 

    Edit: I should add that for a DDL statement like this, you could just execute the query directly. The prepare should be used whenever you have parameter substitution needs or will be executing the same query multiple times, etc.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. Replies: 9
    Last Post: 14th February 2013, 12:14
  2. Replies: 5
    Last Post: 3rd April 2010, 04:07
  3. Check if a table exists in a mysql database
    By graciano in forum Qt Programming
    Replies: 8
    Last Post: 5th November 2009, 02:44
  4. get mysql table into QComboBox
    By eleanor in forum Qt Programming
    Replies: 17
    Last Post: 10th October 2007, 15:35
  5. MYSQL 5 Table qt model as small Mysql admin
    By patrik08 in forum Qt-based Software
    Replies: 0
    Last Post: 1st May 2007, 09:43

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.