Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: QT QT-Creator
I have used the following code to connect to the mysql database, the connection succeeds, but nothing is happened in the database and also no table is created and the rows are not indeed updated. Kindly help me on this issue..
 
QSqlDatabase db= QSqlDatabase::addDatabase("QODBC");
     db.setDatabaseName("DRIVER={MYSQL ODBC 3.51 Driver};FIL={MYSQL};DBQ=screengrabber");
     db.setHostName("localhost");
     //db.setConnectOptions("CLIENT_ODBC");
     //db.setDatabaseName("screengrabber");
     db.setUserName("root");
     db.setPassword("1");
     ok = db.open();////Here ok is true...

     QSqlQuery query;
       query.exec("create table person (id int primary key, "
                  "firstname varchar(20), lastname varchar(20))");
       query.exec("insert into person values(101, 'Danny', 'Young')");
 

Default Re: MYSQL Qt connectivity?? I have used the following query which succees in inserting into the table. But how to bind the values instead of directly giving the values as below::
Qt Code:
 bool qrylog=querylog.exec("INSERT INTO service_log (ip_address,date_time, service_status, logged_user) " "VALUES ('172.16.0.51','2011-07-28 15:55:09',1,'ramachandran')");
Here in database ip_address is varchar(20), date_time is datetime, service_status is integer and logged_user is varchar(30). Please help me fixing this issue::
Posted 27-Jul-11 20:08pm
Edited 28-Jul-11 1:51am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You have to tell QSqlQuery how to "talk" to the databasse.
 
http://doc.qt.nokia.com/latest/qsqlquery.html#QSqlQuery-3[^]
 
QSqlQuery query = db.exec("create table person (id int primary key, firstname varchar(20), lastname varchar(20))");
query.exec("insert into person values(101, 'Danny', 'Young')");
or
QSqlQuery query(db);
query.exec("create table person (id int primary key, firstname varchar(20), lastname varchar(20))");
query.exec("insert into person values(101, 'Danny', 'Young')");
 
For binding values[^] aka SqlParameter.
  Permalink  
v3
Comments
Gokulnath007 at 28-Jul-11 3:33am
   
Could you please explain with the code what i have used, any modifications to be done with it.. Thank You!!!
Kim Togo at 28-Jul-11 3:39am
   
You can do a:
 
QSqlQuery query = db.exec("create table person (id int primary key, firstname varchar(20), lastname varchar(20))");
query.exec("insert into person values(101, 'Danny', 'Young')");
 
or
 
QSqlQuery query(db);
 
query.exec("create table person (id int primary key, firstname varchar(20), lastname varchar(20))");
query.exec("insert into person values(101, 'Danny', 'Young')");
Gokulnath007 at 28-Jul-11 6:50am
   
Default Re: MYSQL Qt connectivity??
 
I have used the following query which succees in inserting into the table. But how to bind the values instead of directly giving the values as below::
Qt Code:
 

bool qrylog=querylog.exec("INSERT INTO service_log (ip_address,date_time, service_status, logged_user) "
"VALUES ('172.16.0.51','2011-07-28 15:55:09',1,'ramachandran')");
 

 
Here in database ip_address is varchar(20), date_time is datetime, service_status is integer and
logged_user is varchar(30).
 
Please help me fixing this issue::
Kim Togo at 28-Jul-11 6:52am
   
See approaches-to-binding-values
 
There is some good examples on how to :-)
Gokulnath007 at 28-Jul-11 7:07am
   
I have used the following code where nothing is added in two rows, 0 and continuous 0 has been added along the ip_address and datetime column respectively.
 
QString ipname="gok";
QString datetime="2011-07-28 15:55:09";
int serv=1;
 
QSqlQuery querylog(db);
querylog.prepare("INSERT INTO service_log (ip_address,date_time, service_status, logged_user) "
"VALUES (ip_address, date_time, service_status, logged_user)");
querylog.bindValue("ip_address",ipname);
querylog.bindValue("date_time",datetime);
querylog.bindValue("service_status",serv);
querylog.bindValue("logged_user",hostname);
Kim Togo at 28-Jul-11 7:10am
   
Change "VALUES (ip_address, date_time, service_status, logged_user)" to:
 
"VALUES (:ip_address, :date_time, :service_status, :logged_user)" and
 
querylog.bindValue(":ip_address",ipname);
querylog.bindValue(":date_time",datetime);
querylog.bindValue(":service_status",serv);
querylog.bindValue(":logged_user",hostname);
 
Look, I have added a : tag as described in the link to approaches-to-binding-values.
Gokulnath007 at 28-Jul-11 7:13am
   
I have tried with that also, but it fails.
Kim Togo at 28-Jul-11 7:18am
   
Strange.. Have you tried the other versions of binding-values? - Hmm. I think it has something to do with ODBC driver ( and the ODBC driver v 3.51 for MySql is really old one, you can download a version 5.1.8 now ).
 
Try "version 2".
 
"VALUES (?, ?, ?, ?)" and
 
querylog.bindValue(ipname);
querylog.bindValue(datetime);
querylog.bindValue(serv);
querylog.bindValue(hostname);
Gokulnath007 at 28-Jul-11 7:20am
   
Do we need to convert all values into QVariant before calling bindValue() function.
Kim Togo at 28-Jul-11 7:25am
   
For "version 2" example change
querylog.bindValue(ipname);
 
to
 
querylog.addBindValue(ipname);
Gokulnath007 at 28-Jul-11 7:22am
   
How to get that version and install in order to invoke in our coding??
Kim Togo at 28-Jul-11 7:26am
   
The newest version MySql ODBC can be downloaded her:
 
http://dev.mysql.com/downloads/connector/odbc/
Gokulnath007 at 28-Jul-11 7:29am
   
I have already installed odbc 5.1 only...
Kim Togo at 28-Jul-11 7:30am
   
Good. But in the connection you have specified "MYSQL ODBC 3.51 Driver".
Check out http://www.connectionstrings.com/mysql#p31
Gokulnath007 at 28-Jul-11 7:29am
   
I am using this code::
 
QSqlDatabase db= QSqlDatabase::addDatabase("QMYSQL3");
DBQ=screengrabber");
db.setHostName("localhost");
//db.setConnectOptions("CLIENT_ODBC");
db.setDatabaseName("screengrabber");
db.setUserName("root");
db.setPassword("1");
ok = db.open();
Gokulnath007 at 28-Jul-11 7:32am
   
I modified the code and now am using this code only...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 381
1 Sergey Alexandrovich Kryukov 265
2 Praneet Nadkar 237
3 Marcin Kozub 225
4 /\jmot 189
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,614
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 28 Jul 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100