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.
Bookmarks