You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

_j

Unregistered

1

Sunday, May 16th 2004, 6:12am

Mysql connection

hi all,

I have done a project in QT in Linux. There I used the mysql.h avaliable in linux to make a connection. I want the project to be in Windows too but I am having a problem with mysql connection. I downloaded the mysql for windows and installed it. Can any one suggest me a method to make the code run in windows with slight modifications. Plz don't tell to use the inbuild qtmysql connection. Becoz I have to change a lot of code.

Thanking you in anticipation

_j

Christian

Unregistered

2

Sunday, May 16th 2004, 10:48am

Hi,

we need some lines of code to be able to help you or a more detailed description where you problem is.

greets

djanubis

Professional

  • "djanubis" is male

Posts: 1,370

Location: Moulins France

Occupation: Software ingeneering

  • Send private message

3

Sunday, May 16th 2004, 3:53pm

RE: Mysql connection

Quoted

Originally posted by _j
There I used the mysql.h avaliable in linux to make a connection.


Why using mysql.h?

You can simply use <qsqldatabase.h>
and the static method QSqlDatabase::addDatabase( "QMYSQL3", "myconnection" ) to init the connection.
Then you use QSqlCursor, QSqlQuery and QSqlRecord classes to manage your data.

Refer to database section in Assistant.
Never patch not working code. Rewrite it !
Never patch badly designed classes. Recreate them cleanly.
(Excerpts from Computing Bible)

Home of the Lab project

vinod_j

Beginner

  • "vinod_j" is male

Posts: 2

Location: Hyderabad

Occupation: Student

  • Send private message

4

Sunday, May 16th 2004, 8:01pm

Quoted

Originally posted by Christian
Hi,

we need some lines of code to be able to help you or a more detailed description where you problem is.

greets


I am giving the code in the form1.ui.h file.

This is how I made a connection using mysql.h file


#include <mysql.h>
#include <winsock2.h>
#include<windows.h>
#include "form2.h"
#include "form4.h"
#include<qmessagebox.h>
#include<qdatetime.h>
#include<fstream>
using namespace std;
MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;

extern QString gDateVariable;

void connect ()
{
mysql_init (&mysql);
if (!mysql_connect (&mysql, "localhost", "itep", "itep123"))
{
fprintf (stderr, "Failed to connect to database: Error :%s\n",
mysql_error (&mysql));
exit (1);
}

if (mysql_select_db (&mysql, "test"))
{
fprintf (stderr, "Failed to select the database: Error :%s\n",
mysql_error (&mysql));
exit (1);
}
}

This is the sample code of how I execute the query...

/*******************
char query[100];
::connect ();
//Getting Max Val in 24 hrs for that day...
sprintf(query,"select Max(Value) from Profile where Month=%d and Weekday=%d group by Month,Weekday",Month,Weekday);
if (mysql_query (&mysql, query))
{
QMessageBox message("MySql Error","SQL Error program aborts",QMessageBox::NoIcon,QMessageBox::Ok ,QMessageBox::Cancel,QMessageBox::NoButton);
exit (1);
}

if (!(res = mysql_store_result (&mysql)))
{
QMessageBox message("MySql Error","SQL Error program aborts",QMessageBox::NoIcon,QMessageBox::Ok ,QMessageBox::Cancel,QMessageBox::NoButton);
exit (1);
}
row = mysql_fetch_row (res);
if (!row)
{
QMessageBox message("Row Does not exist","Such row does not exist",QMessageBox::NoIcon,QMessageBox::Ok ,QMessageBox::Cancel,QMessageBox::NoButton);
}
else
{
MaxValInProfile=atof(row[0]);
}
_j (:-{)

This post has been edited 1 times, last edit by "vinod_j" (May 16th 2004, 8:04pm)


djanubis

Professional

  • "djanubis" is male

Posts: 1,370

Location: Moulins France

Occupation: Software ingeneering

  • Send private message

5

Monday, May 17th 2004, 7:45am

What about:

In your form:

includes:
<qsqldatabase.h>
<qsqlselectcursor.h>
<qsqlrecord.h>
<qsqlerror.h>

variables (protected or private):
QSqlSelectCursor * cursor ;
QSqlRecord * buffer ;

And in your code:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
extern QString gDateVariable;

void form1::init()
{
    QSqlDatabase * db = QSqlDatabase::addDatabase( "QMYSQL3" ) ;
    db->setDatabaseName( "dbToConnectTo" ) ;
    db->setHostName( "localhost" ) ;
    db->setUserName( "dbUser" ) ;
    db->setPassword( "dbPassword" ) ;
    if ( !db->open() ) {
	qDebug( "Failed opening database dbToConnectTo\nReason:" ) ;
	qDebug( db->lastError().driverText() ) ;
	qDebug( db->lastError().databaseText() ) ;
	exit ( 1 ) ; 
    }
    cursor = new QSqlSelectCursor( QString::null, db ) ;
}

void form1::getMaxValue()
{
	cursor->exec(  QString( "SELECT Max(Value) AS MaxVal FROM Profile "
                                                   "WHERE Month='%1' AND Weekday='%2' "
                                                   "GROUP BY Month,Weekday" )
                                   .arg( Month).arg( Weekday) ) ) ;
        if ( !cursor->next() ) {
              qDebug( cursor->lastQuery() ) ;
             return ;
       } 
       MaxValInProfile = cursor->value( "MaxVal" ).toDouble() ;
}


This is a simplistic example. The main advantage is that you are no more tied to MySQL and you can use the same code for SQLite, PostgreSQL, and many more.
Never patch not working code. Rewrite it !
Never patch badly designed classes. Recreate them cleanly.
(Excerpts from Computing Bible)

Home of the Lab project