PDA

View Full Version : Differences between database Objects



ruben.rodrigues
5th October 2010, 07:06
Hi all!

I am doing an application that for now connects to a mysql database but in the future should connect to oracle and msql (odbc). We want to have the database classes as plugins, that meaning 1 plugin per database capability.

My question is if after I have the QSqlQuery object the syntax is the same for all kinds of databases.

For example, if I do the implementation to the mysql class, after doing the query I could return it to the main class and it will handle the data from there.

MySql:


QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); //select database type
db.setHostName(_hostName);
db.setDatabaseName(_databaseName);
db.setUserName(_userName);
db.setUserName(_userPassword);

QSqlQuery query("USE " + _table);
query.exec("Select * from "+ _table);
//then put the query inside a QByteArray and return it to the class that has called it.
return qbytearray;


then the class that has called it opens the QSqlQuery object and get the information.



//open qbytearray and read the QSqlQuery
while (query.next()) {
QSqlRecord record = query.record();
loginName = query.value(0).toString();
loginPassword = query.value(1).toString();
firstName = query.value(2).toString();
surname = query.value(3).toString();
phone = query.value(4).toString();
email = query.value(5).toString();
}


Does this syntax work for all database types?

Thanks

Lykurg
5th October 2010, 08:24
Try it, and it will work for all databases if they understand SQL. Also it is better to name the fields instead of using '*'.

And
QSqlQuery query("USE " + _table);
query.exec("Select * from "+ _table);looks odd. It should be USE mydatabase;
SELECT `id` FROM mytable;