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..
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..
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..
Qt Code:
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')");To copy to clipboard, switch view to plain text mode
Check whether query.exec(..) returns true. If it returns false, you can get the error with query.lastError().text()
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.
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')");To copy to clipboard, switch view to plain text mode
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::
Last edited by Gokulnathvc; 28th July 2011 at 11:37.
Which bit of the excellent documentation are you having difficulty with?
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??
Just open the link from ChrisW67 and look at first example !
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..
Those values are only examples, bindValue() accepts everything convertible to QVariant, so for example this will work:
Qt Code:To copy to clipboard, switch view to plain text mode
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.
I have used the following code also: it says unsupported buffer type.
Qt Code:
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()To copy to clipboard, switch view to plain text mode
Added after 20 minutes:
Qt Code:
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();To copy to clipboard, switch view to plain text mode
This works fine...........![]()
Last edited by Gokulnathvc; 29th July 2011 at 13:38.
Bookmarks