PDA

View Full Version : How to create mysql database table in Qt?



vivek31
3rd December 2015, 06:35
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();
}
}

neuronet
3rd December 2015, 13:32
What do you want to happen, and what has happened?

jefftee
4th December 2015, 00:50
You are missing a right parenthesis on your create statement. Should be the following:



qry.prepare("CREATE TABLE RSA(name varchar(20));");


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.