PDA

View Full Version : Weird Sql connection issue, db.open() tells me connection is there even it really isn



msimurin
2nd April 2010, 05:42
So i am trying to connect to database from the website i own at some hosting company, here is my usage.cpp with database login info changed for obvious reasons.

And the qDebug() output i get is bellow ...

I am trying to connect to Database but it seems that wp_users table doesnt exist by qDebug() even i am sure its there. What is even weirder is that no matter what username, pass, dbname etc.. i use to connect(i can write any gibberish there) it will always tell me that connection is opened by db.open() or db.isValid()...

Please explain me why Qt doesnt give me right info that my connection succeeded and why the table that exists is reported not to be there?

This is a database i use for my wordpress website btw


#include "widget.h"
#include "ui_widget.h"
#include <QtSql>

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setHostName("tm4.justhost.com");
db.setDatabaseName("database_name");
db.setUserName("username");
db.setPassword("pass");

if (!db.open()) {
qDebug() << db.lastError();
}

qDebug() << db.isValid();

QSqlQuery query;

query.exec("SELECT user_login, user_pass FROM wp_users");

while (query.next()) {
QString user = query.value(0).toString();
QString pass = query.value(1).toString();
qDebug() << user << pass;
}

qDebug() << query.lastError();


}

Widget::~Widget()
{
delete ui;
}

void Widget::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}

output:

true
QSqlError(1, "Unable to execute statement", "no such table: wp_users")

Lykurg
2nd April 2010, 06:42
You are sure it is a SQLIte database? Isn't it a MySQL?

Why it return the is because qt creates a new database file in the directory of your executable named "database_name". This is the default behavior for SQLite databases. They also don't accept host, username and password.