Results 1 to 2 of 2

Thread: Differences between database Objects

  1. #1
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Differences between database Objects

    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:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); //select database type
    2. db.setHostName(_hostName);
    3. db.setDatabaseName(_databaseName);
    4. db.setUserName(_userName);
    5. db.setUserName(_userPassword);
    6.  
    7. QSqlQuery query("USE " + _table);
    8. query.exec("Select * from "+ _table);
    9. //then put the query inside a QByteArray and return it to the class that has called it.
    10. return qbytearray;
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. //open qbytearray and read the QSqlQuery
    2. while (query.next()) {
    3. QSqlRecord record = query.record();
    4. loginName = query.value(0).toString();
    5. loginPassword = query.value(1).toString();
    6. firstName = query.value(2).toString();
    7. surname = query.value(3).toString();
    8. phone = query.value(4).toString();
    9. email = query.value(5).toString();
    10. }
    To copy to clipboard, switch view to plain text mode 

    Does this syntax work for all database types?

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Differences between database Objects

    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
    Qt Code:
    1. QSqlQuery query("USE " + _table);
    2. query.exec("Select * from "+ _table);
    To copy to clipboard, switch view to plain text mode 
    looks odd. It should be
    sql Code:
    1. USE mydatabase;
    2. SELECT `id` FROM mytable;
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Lykurg for this useful post:

    ruben.rodrigues (5th October 2010)

Similar Threads

  1. Replies: 0
    Last Post: 26th August 2010, 17:35
  2. Replies: 3
    Last Post: 9th January 2010, 15:47
  3. Qt\bin and Qt\qt\bin dll differences
    By HyperB in forum Installation and Deployment
    Replies: 2
    Last Post: 6th May 2009, 07:12
  4. Layout differences Qt 4.1/Qt 4.2, why?
    By Arthur in forum Qt Programming
    Replies: 1
    Last Post: 28th August 2008, 16:48
  5. Replies: 7
    Last Post: 18th July 2006, 21:33

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.