Connect to a remote MySql database
-
I have a remote MySql database and I'd like to build an application as an interface for that to eventually replace the PHP/HTML/javascript/CSS-based interface that we use now.
I'm just getting started on this and I know next to nothing about QT/C++. So, that's where I am.Now, I'm trying to test the database connection I've tried to set up and I have this:
@ QMessageBox mbox;
QSqlDatabase cdb = QSqlDatabase::addDatabase ("QSQLITE"); cdb.setHostName ("xxx.xxx.xxx.xxx"); cdb.setDatabaseName ("my_databwwase"); cdb.setUserName ("my_username"); cdb.setPassword ("my_password"); bool ok; ok = cdb.open (); QVariant tmp(ok); QString isitok = tmp.toString(); mbox.setText (isitok); mbox.exec();
@
I put in the correct details and I got a 'true' message, so I thought I was connected to the database. Then I put the wrong database name in and tried again, and I still got 'true'. Why would that be?
I was wondering about the first line. I'm guessing that the bit that says QSQLITE should be QMYSQL, but I can't seem to get it to accept that.
I'm doing this in QT Creator by the way.
Any pointers would be appreciated.
Thanks a lot.
-
Can you please do a debug build using the QMYSQL database type, and paste here all the errors you get? (See also QSqlError, QSqlDatabase::lastError). That snippet does not make any sense as-is.
-
You will most probably have to ether build the mysql database driver for Qt or install it from your favorite package manager (if you're on linux).
Please use the search for further instructions on how to do this, it was discussed before:
http://developer.qt.nokia.com/search/tag/mysql
PS:
As you're new to C++ and Qt, I strongly recommend to make yourself familiar with those beforehand and leave that database stuff for the moment. We have a big bunch of introductory pages on DevNet:- "tag search for 'beginner'":http://developer.qt.nokia.com/search/tag/beginner
- "Books and Links for learning C++":http://developer.qt.nokia.com/wiki/Books_and_Links_for_learning_C_and_advanced_topics in the wiki
- "Getting started":http://developer.qt.nokia.com/doc/qt-4.7/gettingstarted-develop.html of the official docs
-
peppe,
I did what you said, and I didn't get any errors in the build. The application output gave me this though:QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITEVolker,
I'll try to build the mysql database driver. I was searching around, but everything I found seemed to be for setting a MySql database to work with Qt - installing MySql. I'm not trying to install MySql at the moment as I have a database to connect to.As for reading about the basics first. Yes, thanks for the advice. I have done quite a bit of that. I read through and used a little C++ before coming to Qt; a lot of it seems quite familiar because I know javascript (of course they're not the same, but many bits seem similar). I've also read through lots of things on this site about what I might be able to do with Qt. Now, I'm trying to - slowly - build something. Establishing a database connection seems to be the first step for what I want to do. After that, I'll slowly build the interface and go from there.
Here's another quick question related to this: I thought I should put the database connection in either main.cpp or in this bit of my db.cpp (the project's called 'db'):
@db::db(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::db)
{
ui->setupUi(this);}@
Then I thought I would check the database connection with a message box on the click of a button. I added a button to the GUI and added a slot to that. Then, the variable that I wanted to check - 'ok' - was not defined in that scope. How do I make variables global? I'm sure it seems like a very basic question, but I haven't managed to find an answer.
Thanks for your patience. I'm sure I'll have lots more questions in the future, so thanks in advance. :)
-
[quote author="BonRouge" date="1313538930"]peppe,
I did what you said, and I didn't get any errors in the build. The application output gave me this though:QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE[/quote]This is the problem. You need to build the Qt/MySQL plugin and probably put it in the right place. The actual procedure is OS dependant, but check http://doc.qt.nokia.com/latest/sql-driver.html#qmysql (and "read between the lines". Don't follow the instructions blindly. Understand what you're supposed to do)
-
After lots of searching, I found this great page: http://www.pikopong.com/blog/2011/07/11/how-to-enable-mysql-support-in-qt-sdk-for-windows-part-2/#comment-170
As I read in another post here - why do we need to go through so many hoops to do this? Qt seems great, but would MySql integration as standard really hurt?
-
Because Qt cannot ship the MySQL plugin due to its license.
-
I see. Maybe they could find a way to make it all a bit less trouble though. No worries though. I seem to have it working OK now and I can get on with my project.
Thanks for the help.