PDA

View Full Version : Data file and QSql



OverTheOCean
29th April 2010, 03:14
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

tbscope
29th April 2010, 11:46
Here's what I would do.

Create a struct to hold the fields.

Example:

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


Create a list based on the above struct.

Like this: QList<fields *> fieldList;


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);
}



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);


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.

OverTheOCean
30th April 2010, 02:32
thanks a lot ... you gave me more than l was hoping, l will look into it.

thanks again,

Michael

waynew
30th April 2010, 23:04
I would use the model / record architecture. Much less programming. See docs QSqlQueryModel and QSqlRecord classes.