here is spirit code:
void testeMYSQL2::on_pushButton_clicked()
{
db.setHostName("localhost");
db.setDatabaseName("teste");
db.setUserName("root");
db.setPassword("teste1234");
if (!db.open()) {
qDebug() << query.lastError().text();
return;
}
if (!query.exec("select nome_prod from produtos where cod_prod = 1")) {
qDebug() << query.lastError().text();
return;
}
if (!query.next()) {
qDebug() << query.lastError().text();
return;
}
QString name
= query.
value(0).
toString();
nameEdit->setText(name);
}
void testeMYSQL2::on_pushButton_clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("teste");
db.setUserName("root");
db.setPassword("teste1234");
if (!db.open()) {
qDebug() << query.lastError().text();
return;
}
QSqlQuery query;
if (!query.exec("select nome_prod from produtos where cod_prod = 1")) {
qDebug() << query.lastError().text();
return;
}
if (!query.next()) {
qDebug() << query.lastError().text();
return;
}
QString name = query.value(0).toString();
nameEdit->setText(name);
}
To copy to clipboard, switch view to plain text mode
its code didn't compile, than I changed to:
void testeMYSQL2::on_pushButton_clicked()
{
db.setHostName("localhost");
db.setDatabaseName("teste");
db.setUserName("root");
db.setPassword("teste1234");
if (!db.open()) {
qDebug() << db.lastError().text();
return;
}
if (!query.exec("select nome_prod from produtos where cod_prod = 1")) {
qDebug() << query.lastError().text();
return;
}
if (!query.next()) {
qDebug() << query.lastError().text();
return;
}
QString name
= query.
value(0).
toString();
nameEdit->setText(name);
}
void testeMYSQL2::on_pushButton_clicked()
{
QSqlQuery query;
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("teste");
db.setUserName("root");
db.setPassword("teste1234");
if (!db.open()) {
qDebug() << db.lastError().text();
return;
}
if (!query.exec("select nome_prod from produtos where cod_prod = 1")) {
qDebug() << query.lastError().text();
return;
}
if (!query.next()) {
qDebug() << query.lastError().text();
return;
}
QString name = query.value(0).toString();
nameEdit->setText(name);
}
To copy to clipboard, switch view to plain text mode
and then I got this error:
gul@gul-laptop:~/qt/testemysql2$ ./testemysql2
QSqlQuery::exec: database not open
"Driver not loaded Driver not loaded"
Then I saw where is the error:
void testeMYSQL2::on_pushButton_clicked()
{
db.setHostName("localhost");
db.setDatabaseName("teste");
db.setUserName("root");
db.setPassword("teste1234");
if (!db.open()) {
qDebug() << db.lastError().text();
return;
}
if (!query.exec("select nome_prod from produtos where cod_prod = 1")) {
qDebug() << query.lastError().text();
return;
}
if (!query.next()) {
qDebug() << query.lastError().text();
return;
}
QString name
= query.
value(0).
toString();
nameEdit->setText(name);
void testeMYSQL2::on_pushButton_clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("teste");
db.setUserName("root");
db.setPassword("teste1234");
if (!db.open()) {
qDebug() << db.lastError().text();
return;
}
QSqlQuery query;
if (!query.exec("select nome_prod from produtos where cod_prod = 1")) {
qDebug() << query.lastError().text();
return;
}
if (!query.next()) {
qDebug() << query.lastError().text();
return;
}
QString name = query.value(0).toString();
nameEdit->setText(name);
To copy to clipboard, switch view to plain text mode
Then it worked, but still I don't see the result in the QLineEdit.
Now I will try what spirit said above. I will post the result in a few seconds.
Renan
Bookmarks