Hello,
I was trying to build a simple connection to my database that i created recently. But it seems my code doesn't work or sth else wrong. Actually I want to show the values from database file.
Here's the code :
#include <QtCore/QCoreApplication>
#include <QtSql>
#include <QtDebug>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
db.setConnectOptions();
db.setDatabaseName("deneme");
db.setHostName("localhost");
db.setUserName("root");
db.setPassword("2245009");
if(db.open())
{
cout<<"opened"<<endl;
if(qry.exec("SELECT *FROM `tablo` 1"))
{
while(qry.next())
{
//qDebug() << qry.value(1).toString();
qDebug() << qry.value(1);
}
}
else
{
qDebug() << "error = " << db.lastError().text();
}
cout<<"closing"<<endl;
db.close();
}
else
{
qDebug() << "error = " << db.lastError();
}
return a.exec();
}
#include <QtCore/QCoreApplication>
#include <QtSql>
#include <QtDebug>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setConnectOptions();
db.setDatabaseName("deneme");
db.setHostName("localhost");
db.setUserName("root");
db.setPassword("2245009");
if(db.open())
{
cout<<"opened"<<endl;
QSqlQuery qry;
if(qry.exec("SELECT *FROM `tablo` 1"))
{
while(qry.next())
{
//qDebug() << qry.value(1).toString();
qDebug() << qry.value(1);
}
}
else
{
qDebug() << "error = " << db.lastError().text();
}
cout<<"closing"<<endl;
db.close();
}
else
{
qDebug() << "error = " << db.lastError();
}
return a.exec();
}
To copy to clipboard, switch view to plain text mode
The output is like that :
Starting /home/onur/dbapp-build-desktop/dbapp...
opened
closing
error = " "
It shows an empty error. I didn't understand anything because of being a newbie. Any advices?
Thanks in Advance!
Bookmarks