PDA

View Full Version : QT MySQL



sabeeshcs
10th January 2007, 09:04
Hello,
I am a new commer in QT. I am using QT 3.3.4, MYSQL 4.1.11 and OS is Linux (Fedora Core 4 ). I wand to create a simple programe in QT ( Linux -Fedora Core 4 Version ). I designed a form and wite the code. The programe is working. After that I had try to connect this programe with MYSQL, then it is not working, How can I connect my programme with MYSQL?
I give some lines in my programme, that the following,


#include <qapplication.h>
#include "FarkData.h"
#include <qsqldatabase.h>

int main( int argc, char ** argv )
{
QApplication a( argc, argv );
QsqlDatabase *defaultDB=QsqlDatabase::addDatabase(“QMYSQL 3”);
defaultDB->setDatabaseName(“test”);
defaultDB->setPassword(" “);
defaultDB->setHostName(“localhost”);

if(defaultDB->open())
{
qWarning(“Database connection successful”);
}
else
{
qWarning(“Database connection failed”);
return a.exec();
}

MarkList w;
w.show();
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return a.exec();
}

after this code i give a commend,
qmake -o Makefile mark.pro
Then no error displayed, then i give the command,
make
Then a list of error is display, the error is



/usr/lib/qt-3.3/bin/uic FarkData.ui -o .ui/FarkData.h
g++ -c -pipe -Wall -W -O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -m32 -march=i386 -mtune=pentium4 -fasynchronous-unwind-tables -DQT_NO_DEBUG -DQT_SHARED -DQT_THREAD_SUPPORT -I/usr/lib/qt-3.3/mkspecs/default -I. -I/usr/lib/qt-3.3/include -I.ui/ -I. -I.moc/ -o .obj/main.o main.cpp
main.cpp:9: error: stray ‘\226’ in program
main.cpp:9: error: stray ‘\128’ in program
main.cpp:9: error: stray ‘\156’ in program
main.cpp:9: error: stray ‘\226’ in program
main.cpp:9: error: stray ‘\128’ in program
main.cpp:9: error: stray ‘\157’ in program
main.cpp:11: error: stray ‘\226’ in program
main.cpp:11: error: stray ‘\128’ in program
main.cpp:11: error: stray ‘\156’ in program
main.cpp:11: error: stray ‘\226’ in program
main.cpp:11: error: stray ‘\128’ in program
main.cpp:11: error: stray ‘\157’ in program
main.cpp:12: error: stray ‘\226’ in program
main.cpp:12: error: stray ‘\128’ in program
main.cpp:12: error: stray ‘\156’ in program
main.cpp:12: error: stray ‘\226’ in program
main.cpp:12: error: stray ‘\128’ in program
main.cpp:12: error: stray ‘\156’ in program
main.cpp:13: error: stray ‘\226’ in program
main.cpp:13: error: stray ‘\128’ in program
main.cpp:13: error: stray ‘\156’ in program
main.cpp:13: error: stray ‘\226’ in program
main.cpp:13: error: stray ‘\128’ in program
main.cpp:13: error: stray ‘\157’ in program
main.cpp:17: error: stray ‘\226’ in program
main.cpp:17: error: stray ‘\128’ in program
main.cpp:17: error: stray ‘\156’ in program
main.cpp:17: error: stray ‘\226’ in program
main.cpp:17: error: stray ‘\128’ in program
main.cpp:17: error: stray ‘\157’ in program
main.cpp:21: error: stray ‘\226’ in program
main.cpp:21: error: stray ‘\128’ in program
main.cpp:21: error: stray ‘\156’ in program
main.cpp:21: error: stray ‘\226’ in program
main.cpp:21: error: stray ‘\128’ in program
main.cpp:21: error: stray ‘\157’ in program
.ui/FarkData.h:47: error: ISO C++ forbids declaration of ‘QSqlDatabase’ with no type
/usr/lib/qt-3.3/include/qsqldatabase.h:63: warning: ‘class QSqlDriverCreatorBase’ has virtual functions but non-virtual destructor
main.cpp: In function ‘int main(int, char**)’:
main.cpp:9: error: ‘QsqlDatabase’ was not declared in this scope
main.cpp:9: error: ‘defaultDB’ was not declared in this scope
main.cpp:9: error: ‘QsqlDatabase’ is not a class or namespace
main.cpp:9: error: ‘QMYSQL3’ was not declared in this scope
main.cpp:9: error: ‘addDatabase’ was not declared in this scope
main.cpp:11: error: ‘test’ was not declared in this scope
main.cpp:13: error: ‘localhost’ was not declared in this scope
main.cpp:17: error: ‘Database’ was not declared in this scope
main.cpp:21: error: ‘Database’ was not declared in this scope
make: *** [.obj/main.o] Error 1
What to do ?
please help me...........

Sabeesh

wysota
10th January 2007, 10:47
First thing to do would probably be to get rid of the stray characters - did you copy and paste the code into your program from some other source (the docs, Internet)? This often causes stray characters to appear. Check the quotes (they are certainly not regular ' " ' characters in the code above), etc.

sabeeshcs
10th January 2007, 11:45
Hi,
Thakyou for your reply, I had take a new project, after that i had add three LineEdit control and three pushbuttons. After that i write the code for working that. then it is worked. After that i wish to insert that values into a table ( MYSQL ). Then i get into the net and get the code. I paste that code in main.cpp and give the command for make.
I had replace the special character ( ie, ->, , et ). After this i give the command " qmake -o Makefile mark.pro " then there is no error display.
But after that i give the command "make" then the error message is display
The code is
=================================
QsqlDatabase *defaultDB=QsqlDatabase::addDatabase("QMYSQL3");
defaultDB->setDatabaseName("test");
defaultDB->setPassword("");
defaultDB->setHostName("localhost");
if(defaultDB->open())
{
qWarning("Database connection successful");
}
else
{
qWarning("Database connection failed");
return a.exec();
}

=====================

The error is
==============================
g++ -c -pipe -Wall -W -O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -m32 -march=i386 -mtune=pentium4 -fasynchronous-unwind-tables -DQT_NO_DEBUG -DQT_SHARED -DQT_THREAD_SUPPORT -I/usr/lib/qt-3.3/mkspecs/default -I. -I/usr/lib/qt-3.3/include -I.ui/ -I. -I.moc/ -o .obj/main.o main.cpp
main.cpp:6:28: error: connection.h: No such file or directory
.ui/FarkData.h:47: error: ISO C++ forbids declaration of ‘QSqlDatabase’ with no type
/usr/lib/qt-3.3/include/qsqldatabase.h:63: warning: ‘class QSqlDriverCreatorBase’ has virtual functions but non-virtual destructor
main.cpp: In function ‘int main(int, char**)’:
main.cpp:12: error: ‘QsqlDatabase’ was not declared in this scope
main.cpp:12: error: ‘defaultDB’ was not declared in this scope
main.cpp:12: error: ‘QsqlDatabase’ is not a class or namespace
main.cpp:12: error: ‘addDatabase’ was not declared in this scope
make: *** [.obj/main.o] Error 1
================================================== =====


After this I try for another code and get a new one, that is

==================================

QSqlDatabase *defaultDB = QSqlDatabase::addDatabase( "QMYSQL" );
defaultDB->setDatabaseName( "test" );
defaultDB->setUserName( "root" );
defaultDB->setPassword( "" );
defaultDB->setHostName( "localhost");

if ( defaultDB->open() ) {
printf("Sucessfully Connected \n");
// Database successfully opened; we can now issue SQL commands.
}

===================================

then i give the command
qmake -o Makefile mark.pro
no error display... When i give the command
make

then the following error display...
====================================
/usr/lib/qt-3.3/bin/uic FarkData.ui -o .ui/FarkData.h
g++ -c -pipe -Wall -W -O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -m32 -march=i386 -mtune=pentium4 -fasynchronous-unwind-tables -DQT_NO_DEBUG -DQT_SHARED -DQT_THREAD_SUPPORT -I/usr/lib/qt-3.3/mkspecs/default -I. -I/usr/lib/qt-3.3/include -I.ui/ -I. -I.moc/ -o .obj/main.o main.cpp
main.cpp:6:28: error: connection.h: No such file or directory
.ui/FarkData.h:47: error: ISO C++ forbids declaration of ‘QSqlDatabase’ with no type
/usr/lib/qt-3.3/include/qsqldatabase.h:63: warning: ‘class QSqlDriverCreatorBase’ has virtual functions but non-virtual destructor
make: *** [.obj/main.o] Error 1
==================================

please help me sir....
thankyou...
Sabeesh

wysota
10th January 2007, 12:59
Did you #include <qsqldatabase.h>?

sabeeshcs
11th January 2007, 03:32
Yes, I include these header files,

#include <qapplication.h>
#include "/usr/include/mysql/mysql.h"
#include <qsqldatabase.h>
#include <qsqlquery.h>

ok i solve the problem.


But now I had occure another one

I wand to execute a query in QT.
The following is the code...

================================================== ==
double id, mark1, mark2, mark3, total;
const char *name;
name = TxtName->text();
mark1 = TxtMark1->text().toDouble();
mark2 = TxtMark2->text().toDouble();
mark3 = TxtMark3->text().toDouble();
id = TxtMark4->text().toDouble();

total = mark1 +mark2+mark3;

QSqlQuery target;

QString sql = "insert into student ( s_id, s_name, s_mark1, s_mark2, s_mark3, s_total ) values( " ;

==================================================

After this i am stuck!!!!!!!!!!!
how can i add the values of the variables to that string ' sql ' ?

please help me......

sabeesh




================================================== ===






Sabeesh

zlatko
11th January 2007, 07:59
Just example from docs...


QSqlQuery query;
query.prepare( "INSERT INTO atable (id, forename, surname) "
"VALUES (:id, :forename, :surname)" );
query.bindValue( ":id", 1001 );
query.bindValue( ":forename", "Bart" );
query.bindValue( ":surname", "Simpson" );
query.exec();


p.s. Read this all ! (http://doc.trolltech.com/3.3/qsqlquery.html)

sabeeshcs
12th January 2007, 04:19
Thankyou for your help...

I wish to consentrate developing graphical application in QT. ie, I wish to create an object with the help of some messure. Can you give some documentation about that.