Hi ChrisW67, Hi Lesiok,
I think I understood the definitions of the classes. I wrote the following code.
Just for info, I'm running an Win XP 32 bit, MySQL Workbench 5.2.47, Qt 5.0.2
on 1.cpp:
void 1::on_addUser_triggered()
{
addUser.setHostName("localhost");
addUser.setDatabaseName("test");
addUser.setUserName("root");
addUser.setPassword("pass");
usernew *nU = new usernew;
nU ->show();
void 1::on_addUser_triggered()
{
QSqlDatabase addUser = QSqlDatabase::addDatabase("QMYSQL", "adConn");
addUser.setHostName("localhost");
addUser.setDatabaseName("test");
addUser.setUserName("root");
addUser.setPassword("pass");
usernew *nU = new usernew;
nU ->show();
To copy to clipboard, switch view to plain text mode
on 2.cpp
void 2::on_buttonBox_accepted()
{
if (addUser.open())
{
qDebug () << "opened"; // only for testing
qDebug () << addUser.connectionName(); // only for testing
q1.prepare("INSERT INTO test.users (username, pass, name, email)VALUES (:username, :pass, :name, :email)");
q1.bindValue(":username", ui->lnUsername->text());
q1.bindValue(":pass", ui->lnPassword->text());
q1.bindValue(":name", ui->lnName->text());
q1.bindValue(":email", ui->lnEmail->text());
q1.exec();
if (q1.exec())
{
qDebug() << "Query executed with success"; // only for testing
}
else
{
qDebug() << "Query Error: " << q1.lastError().text(); // only for testing
qDebug () << addUser.drivers(); // only for testing
}
}else
{
qDebug() << "Database Error:" << addUser.lastError(); // only for testing
}
addUser.close();
addUser.removeDatabase("adConn");
}
void 2::on_buttonBox_accepted()
{
QSqlDatabase addUser = QSqlDatabase::database("adConn");
if (addUser.open())
{
qDebug () << "opened"; // only for testing
qDebug () << addUser.connectionName(); // only for testing
QSqlQuery q1;
q1.prepare("INSERT INTO test.users (username, pass, name, email)VALUES (:username, :pass, :name, :email)");
q1.bindValue(":username", ui->lnUsername->text());
q1.bindValue(":pass", ui->lnPassword->text());
q1.bindValue(":name", ui->lnName->text());
q1.bindValue(":email", ui->lnEmail->text());
q1.exec();
if (q1.exec())
{
qDebug() << "Query executed with success"; // only for testing
}
else
{
qDebug() << "Query Error: " << q1.lastError().text(); // only for testing
qDebug () << addUser.drivers(); // only for testing
}
}else
{
qDebug() << "Database Error:" << addUser.lastError(); // only for testing
}
addUser.close();
addUser.removeDatabase("adConn");
}
To copy to clipboard, switch view to plain text mode
when I run the app, I receive these messeges:
opened - this means the connection is opened.
"adConn" - the connection name is confirmed
QSqlQuery:repare: database not open - Why I get this error?
Query Error: "Driver not loaded Driver not loaded" - Why I get this error?
("QSQLITE", "QMYSQL", "QMYSQL3", "QODBC", "QODBC3") - due to the above error I checked the drivers. MySql drivers are loaded. I run the tutorial basicQuery from here and the database is opened and I can insert new values
QSqlDatabasePrivate::removeDatabase: connection 'adUserConn' is still in use, all queries will cease to work. - Why I get this error? is the ::removeDatabase declaration not used correctly?
I would appreciate if I could receive some guidelines on the below questions:
1. QSqlQuery:repare: database not open - Why I get this error?
2. Query Error: "Driver not loaded Driver not loaded" - Why I get this error?
3. QSqlDatabasePrivate::removeDatabase: connection 'adUserConn' is still in use, all queries will cease to work. - Why I get this error?
thanks for your support.
Bookmarks