I'm pretty much fed up with ODBC and Qt but I thought I'd make comment on this since no one else has anywhere.
The "names" table has an autoid "id" column and a "name" text column.
Here is code I use when wanting to insert a new item into my database.
//Create a simple model
model.setTable("names");
model.setFilter("id is null"); //I do this so that I don't have to return every single row, or in this case any rows at all.
model.select();
//Make a mapper to hook up my widgets to my data
mapper.setModel(&model);
mapper.addMapping(myIdLineEdit, 0);
mapper.addMapping(myNameLineEdit, 1);
//Since the model has no rows currently (because of the 'is null' filter), insert a default one
rec.setValue(1, "type name here");
model.insertRecord(-1, rec);
mapper.toFirst();
//Create a simple model
QSqlTableModel model;
model.setEditStrategy(QSqlTableModel::OnManualSubmit);
model.setTable("names");
model.setFilter("id is null"); //I do this so that I don't have to return every single row, or in this case any rows at all.
model.select();
//Make a mapper to hook up my widgets to my data
QDataWidgetMapper mapper;
mapper.setModel(&model);
mapper.addMapping(myIdLineEdit, 0);
mapper.addMapping(myNameLineEdit, 1);
//Since the model has no rows currently (because of the 'is null' filter), insert a default one
QSqlRecord rec = model.record();
rec.setValue(1, "type name here");
model.insertRecord(-1, rec);
mapper.toFirst();
To copy to clipboard, switch view to plain text mode
At this point if the user wants to cancel editing I can call model.revertAll() and insert a new default record.
But if the user wants to save i do this:
qDebug() << model.index(0,0).data().toString(); //Try to display the id, this will be blank because the record only exists in Qt's cache and not in the database yet.
qDebug() << model.rowCount(); //displays "1"
qDebug() << mapper.currentIndex(); //displays "0"
model.submitAll();
qDebug() << mapper.currentIndex(); //displays "-1" because submitAll() calls select() and invalidates all model indexes.
qDebug() << model.rowCount(); // *** SEE BELOW ***
qDebug() << model.index(0,0).data().toString(); //*** SEE BELOW ***
qDebug() << model.index(0,0).data().toString(); //Try to display the id, this will be blank because the record only exists in Qt's cache and not in the database yet.
qDebug() << model.rowCount(); //displays "1"
qDebug() << mapper.currentIndex(); //displays "0"
model.submitAll();
qDebug() << mapper.currentIndex(); //displays "-1" because submitAll() calls select() and invalidates all model indexes.
qDebug() << model.rowCount(); // *** SEE BELOW ***
qDebug() << model.index(0,0).data().toString(); //*** SEE BELOW ***
To copy to clipboard, switch view to plain text mode
Ok so if we run the above using a MySql database we now have a model.rowCount() of 1, and the autoid get's passed back from the database and displays correctly.
(using mysql 5, qmysql driver, and qt 4.4.0)
On a MS-SQL server via ODBC (ms-sql server 2005, qodbc driver, qt 4.4.0 compiled with msvc c++ 2008) the model.rowCount() shows 0 and the the autoid line doesn't work because there are no rows in the model.
I kinda get what it's doing behind the scenes and it does kinda make sense to me but it's highly annoying when trying to use QSqlTableModel, QDataWidgetMapper, and the ability to keep data cached locally until the user wants it saved.
- Am I misunderstanding Trolltech's way of doing things?
- Should I turn back to using QSqlQuery and doing everything manually?
- Would using the commercial TDS driver fix these problems? (I'm using the opensource version of Qt right now)
I guess it all comes down to what QSqlDriver->hasFeature() returns, right?. I peeked at the 4.4.0 source for each driver (qodbc, qmysql, qsqlite) and can see how limited qodbc is compared to the others. What I can't do and don't know is what the TDS driver's hasFeature returns. Can anyone tell me what features it supports?
If this is the only way to get MS-SQL to play nicely with Qt apps I am willing to shell out the money for the TDS driver... but I was really hoping for an open source solution.
If anyone can help out with this I would be so very very grateful. Thanks
Bookmarks