Relational Table Model Example not persisting data
-
Relational Table Model Example not persisting data, you may not notice it unless you switch the example from memory into a file, then close app and restart it; first thing you will notice is that it will double the number of records; since it recreates the database every time; I think this needs to be addressed in the example; something like:
in static bool createConnection() modify this
@
bool doCreate = true;
//db.setDatabaseName(":memory:");
#ifdef Q_OS_LINUX
// NOTE: We have to store database file into user home folder in Linux
QString path(QDir::home().path());
path.append(QDir::separator()).append("my.db.sqlite");
path = QDir::toNativeSeparators(path);
QFileInfo fi(path);
if (fi.exists())
{
doCreate = false;
}
db.setDatabaseName(path);
#else
// NOTE: File exists in the application private folder, in Symbian Qt implementation
QFileInfo fi("my.db.sqlite");
if (fi.exists())
{
doCreate = false;
}
db.setDatabaseName("my.db.sqlite");
#endifif (!db.open()) return false;
if (doCreate)
{
QSqlQuery query;
// get this from createRelationalTables and delete that function and call to it
query.exec("create table employee(id int primary key, name varchar(20), city int, country int)");
query.exec("insert into employee values(1, 'Espen', 5000, 47)");
...
}@
Now you can run the program without doubling the records each time; not sure if this is the best way to do this; but it at least works.
Now if you edit the records; close the file, by quiting; you will notice on running it again; that none of the changes got persisted; I noticed this with all the Database examples.
I realize this example uses three tables; employee, city and country; and you only want to update the employee; and model->setEditStrategy(QSqlTableModel::OnManualSubmit); has something to do with this behavior; but looking at the API and examples; it wasn't clear how this should be done; one would have guessed that these examples were persisting the data; but unless I'm wrong; you would be wrong to assume that.
I know its just an example; but lets face it; on closer examination; its a bad example; so I'm hoping that we can get the examples updated; not sure where to start; but before I submit this as a bug; I'd like to know how to fix this example so it works; since I didn't find an example that didn't have this problem; I'll have to submit a bug report against all of them: Table Model Example, Books, Drill down, and SQL Widget Mapper Example to name a few.
I like examples that work; it makes learning a new framework so much easier; and since I'm interested in Database examples; its driving me crazy that I couldn't find one that worked right; or maybe its just me, and I don't get the examples; yes in memory and not expecting that you would actually like to persist data to an actual database; saves on hard drive space when running examples; but still; it should work if you switch to a live database; that's all I'm saying.
Update: As I suspected setEditStrategy is why its not being persisted.
This will fix that; but not how I'd do it in a real app.
model->setEditStrategy(QSqlTableModel::OnFieldChange); // OnFieldChange, OnRowChange, OnManualSubmitI have more thoughts about this; but do not like using Edit to do that; every forum is different; so let me know if that is wrong; but lets face it; edits go unnoticed; unless you happen to come back to an old thread.
Thanks
-
So far this is my feeling about all the SQL Examples:
Do not share the the connection.h; unless you want them all to use the same database; in which case; you will have to create all your databases here; or make a call to another function in the app; lets say, createDatabase, such that it only gets called once and will not step on another examples create or modify calls; in other words; write this function so it detects the existence of the database file, then create all tables, indexes, and add data to them; that may work for examples; in real life; its a bad idea, unless both apps are using the same database. In fact: I'd recommend not teaching people bad practices; which as a DBA I would consider this as; these are examples of totally different concepts; and do not have a reason to share a common database; if space is an issue; then making them in memory will solve that problem (default, by the way); just document this feature; and maybe set a directive or variable to switch to a real database or memory model of it. This should just be a function in the main body of code; it should never be hidden in a header file, I over looked the header file at first; many do; its another bad practice in my opinion; keep the code simple; easy to follow; so keep it all in once file will like functions.
It would require a bit more code; but if you just added a function call on exit to test to see if the control is dirty; you could prompt the user for a Save option.
Examples say a lot about a product; you want your examples, samples or demo apps to work great; it shows your medal; I was disappointed with these examples; and they need to be updated, the underlying code and format is fine; so all I'm saying is to just update these examples so they at least work, use good practices; and are easy to understand; I like more comments in the code; they should tell the story, not a book, reading the code is a lost art, I have been a computer programmer since 1979, I do remember all the changes, all the languages, all the millions of lines of code I have seen over those years; and the code that I liked the most; had lots of comments.
This is Open Source, and I have no problem updating these examples; but I have gone there before in other projects, just to be ignored; and it did sore me, so its best to ask if there is any chance that my efforts would even be worth it; otherwise I'll just do what everyone else does; complain about it; boy that is Open Source at work for you; but I don't see that in this project yet, so I have hope, that is why I'm writing this.
On a side note; this is 11-11-11, Friday and Veterans Day, and I just happen to be a Disabled Gulf War Vet, Medically Retired U.S. Air Force, my wife was also in the Air Force; you see how I spend my time; can't go out and celebrate, so I say home, and do what I can do; so to all the Vets out there, thanks.
Is this the right forum to be asking about Example Code?
If not, can I get a moderator to move it please.Thanks