PDA

View Full Version : MYSQL Qt connectivity??



Gokulnathvc
27th July 2011, 09:57
Could anybody please provide a good source code which successfully connects MYSQL with Qt and the changes should be updated in the database. A very simple code is quite better..

wysota
27th July 2011, 10:17
SQL Examples

Gokulnathvc
28th July 2011, 06:58
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')");

gkarthick5
28th July 2011, 08:18
Check whether query.exec(..) returns true. If it returns false, you can get the error with query.lastError().text()

Gokulnathvc
28th July 2011, 10:19
It gives the following error Message::

[MYSQL][ODBC 3.51 DRIVER][mysqld-5.0.45-community-nt] No Database selected QODBC: Unable to execute the statement.

Gokulnathvc
28th July 2011, 12:23
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::

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::

ChrisW67
29th July 2011, 02:11
Which bit of the excellent documentation are you having difficulty with?

Gokulnathvc
29th July 2011, 06:52
The entries for each field has been hardcoded and it is not storing the values in a variable and binding the values to it. Could you help me with my previous post??

stampede
29th July 2011, 08:54
Just open the link from ChrisW67 and look at first example !

Gokulnathvc
29th July 2011, 10:04
Those are examples with hard coded values, it doesnt explain about the variable storing the values and then assigning the variable to the field value..

stampede
29th July 2011, 10:21
Those values are only examples, bindValue() accepts everything convertible to QVariant, so for example this will work:


int number = ...
QString name = ...
QString surname = ...
...
QSqlQuery query;
query.prepare("INSERT INTO person (id, forename, surname) "
"VALUES (:id, :forename, :surname)");
query.bindValue(":id", number);
query.bindValue(":forename", name);
query.bindValue(":surname", surname);
query.exec();

Gokulnathvc
29th July 2011, 10:24
One row has been created with NULL values and 0 in number field alone is printed where i gave the value for number as 9.

wysota
29th July 2011, 13:46
Apparently your code was incorrect.

Gokulnathvc
29th July 2011, 14:38
I have used the following code also: it says unsupported buffer type.


QString ipname="gok";
QString datetime="2011-07-28 15:55:09";
QString servstr="1";
QString hostname="gokul";
QSqlQuery querylog(db);
querylog.prepare("INSERT INTO service_log (ip_address,date_time, service_status, logged_user)VALUES (?,?,?,?)");
querylog.bindValue(0,ipname);
querylog.bindValue(1,datetime);
querylog.bindValue(2,serv);
querylog.bindValue(3,hostname);
querylog.exec()

Added after 20 minutes:


QSqlQuery querylog(db);
querylog.exec("INSERT INTO service_log (ip_address,date_time, service_status, logged_user)VALUES (?,?,?,?)");
querylog.bindValue(0,ipname);
querylog.bindValue(1,datetime);
querylog.bindValue(2,serv);
querylog.bindValue(3,hostname);
bool qrylog= querylog.exec();

This works fine...........:D:D:D:D