Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 28-Jul-11 0:51am
v2

1 solution

You have to tell QSqlQuery how to "talk" to the databasse.

http://doc.qt.nokia.com/latest/qsqlquery.html#QSqlQuery-3[^]

C
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
C
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.
 
Share this answer
 
v3
Comments
Gokulnath007 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 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 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 28-Jul-11 6:52am    
See approaches-to-binding-values

There is some good examples on how to :-)
Gokulnath007 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);

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900