Results 1 to 4 of 4

Thread: Data file and QSql

  1. #1
    Join Date
    Sep 2009
    Location
    phoenix, AZ
    Posts
    41
    Qt products
    Qt4
    Platforms
    Windows

    Default Data file and QSql

    Guys,

    l would like to use a QSQL database to load and process large data files...but l'm very new to QSQL( but know pretty well Qt) and before to spend my nights on it l would like to get a few advises.

    my files have the following format

    Field1 Field2 Field3 Field4 Field5 ....
    1 12 25 251 1255
    1 14 21 432 1254
    1 12 22 435 1252
    2 18 23 134 1258
    2 17 25 251 1259
    2 19 24 250 1252
    3 11 26 251 1254
    3 19 25 251 1255
    3 17 21 251 1256
    ...

    l would to store this data in a database, and access it with some sort of filter to select the rows with specific value of Field2,3, 4...

    well, just a basic database l guess... is anyone can give me the inspiration ?

    regards,

    Michael

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Data file and QSql

    Here's what I would do.

    1. Create a struct to hold the fields.

      Example:

      struct fields {
      int field1;
      int field2;
      int field3;
      int field4;
      int field5;
      };


    1. Create a list based on the above struct.

      Like this: QList<fields *> fieldList;


    1. Parse the datafile. Read each line and split the line based on the space character.
      Create a new fields element and add the data.

      Example:

      QFile file("yourfilename");
      file.open(...);

      while (!file.atEnd()) {
      QString line = file.readLine();
      QStringList fields = line.split(" ");
      fields *newFields = new fields;
      foreach(QString field, fields) {
      newFields->field1 = fields.at(0).toInt();
      ...
      }
      fieldList.append(newFields);
      }


    1. When done parsing the file, use the fieldList to add the data to the database, but first create the database.

      Here's how you create a simple SQLite database:

      QSqlDatabase database = QSqlDatabase::addDatabase("QSQLite", "fieldsdb");
      database.setHostname("localhost");
      database.setDatabaseName("./fields.db");
      database.open();

      QString queryString;
      queryString = "CREATE TABLE fields (";
      queryString += " fieldId INTEGER PRIMARY KEY,";
      queryString += " field1 INTEGER,";
      ...
      queryString += "field 5 INTEGER)";

      QSqlQuery createQuery(QSqlDatabase::database("fieldsdb"));
      createQuery.exec(queryString);


    1. Add the data to the database, like this:

      database.transaction();

      QSqlQuery queryAddField(QSqlDatabase::database("fieldsdb"));
      queryAddField.prepare("INSERT INTO fields (field1, field2, field3, field4, field5) VALUES (?,?,?,?,?)");

      foreach(Fields *field, fieldList) {
      queryAddField.addBindValue(field->field1);
      ...
      queryAddField.addBindValue(field->field5);
      queryAddField.exec();
      }

      database.commit();


    That should do it.

    When you want to read from the database, create a new QSqlQuery with the query SELECT * FROM fields WHERE field5 = 5
    This will give all rows and columns where field5 equals 5.
    For more information see: http://www.w3schools.com/sql/default.asp
    And the Qt documentation.

  3. #3
    Join Date
    Sep 2009
    Location
    phoenix, AZ
    Posts
    41
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Data file and QSql

    thanks a lot ... you gave me more than l was hoping, l will look into it.

    thanks again,

    Michael

  4. #4
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Data file and QSql

    I would use the model / record architecture. Much less programming. See docs QSqlQueryModel and QSqlRecord classes.
    Last edited by waynew; 1st May 2010 at 00:06. Reason: Class name correction

Similar Threads

  1. Reading data from xls file
    By addu in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2010, 10:33
  2. reading data from file
    By offline in forum Qt Programming
    Replies: 1
    Last Post: 9th April 2010, 11:31
  3. how can I get picture data from file ?
    By yleesun in forum Qt Programming
    Replies: 5
    Last Post: 21st October 2009, 12:12
  4. How to write data into a file
    By grsandeep85 in forum Qt Programming
    Replies: 9
    Last Post: 21st July 2009, 09:51
  5. data is not appending to the file
    By sudheer in forum Qt Tools
    Replies: 2
    Last Post: 3rd April 2008, 13:39

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.