Qt-jambi-interest Archive, October 2007
TableView w/ H2 database
Message 1 in thread
Hi, I am trying to populate a TableView widget with data from an H2
database. I have managed to make a connection to the DB and execute a
query, but for some reason my TableView shows no data. It does load the
column headers into the grid so I know it is seeing the table structure,
however the data is not filling as if there is no data in my table.
I have a table TEST with 2 columns ID and NAME. There are 2 rows of data in
the db. Here is my code, which is mostly copied from the QJdbcSqlDriver
sample code. Can anyone see what I may be doing wrong? Much thanks, Jason
try
{
QJdbcSqlDriver.initialize();
try {
Class.forName("org.h2.Driver").newInstance();
} catch (Exception ex) {
System.err.println(ex); return;
}
QSqlDatabase db = QSqlDatabase.addDatabase("QJDBC");
db.setDatabaseName("jdbc:h2:~/testdb");
db.setUserName( "sa" );
db.setPassword( "" );
if (db.open()) {
System.out.println("Connected!");
} else {
System.out.println("Connection Failed!");
System.out.println(db.lastError().text());
return;
}
QSqlTableModel model = new QSqlTableModel(null, db);
model.setTable("TEST");
if (!model.select()) {
System.out.println("SELECT ERROR: " + model.lastError().text());
}
// ui.tblContacts is a TableView widget dragged into the designer
(MainWin.jui)
ui.tblContacts.setModel(model);
ui.tblContacts.show(); // <- COL HEADERS SHOW IN THE TABLE BUT NO DATA.
ARGH!!!
}
catch (Exception ex)
{
System.out.println("ERROR: " + ex.getMessage());
}
[end of code]
Message 2 in thread
Bump - anybody? QSqlTableModel doesn't seem to have any data.
I can get my TableView showing H2 data using a regular old
java.sql.Connection and implementing a QAbstractTableModel class. But, I
think this is a poor hack - it only proves that my TableView seems ok.
Obviously it would be preferable to use the QSqlTableModel.
Thanks for any help,
Jason
-----Original Message-----
From: Jason [mailto:jason@xxxxxxxxxxxxxx]
Sent: Tuesday, October 30, 2007 5:23 PM
To: qt-jambi-interest@xxxxxxxxxxxxx
Subject: TableView w/ H2 database
Hi, I am trying to populate a TableView widget with data from an H2
database. I have managed to make a connection to the DB and execute a
query, but for some reason my TableView shows no data. It does load the
column headers into the grid so I know it is seeing the table structure,
however the data is not filling as if there is no data in my table.
I have a table TEST with 2 columns ID and NAME. There are 2 rows of data in
the db. Here is my code, which is mostly copied from the QJdbcSqlDriver
sample code. Can anyone see what I may be doing wrong? Much thanks, Jason
try
{
QJdbcSqlDriver.initialize();
try {
Class.forName("org.h2.Driver").newInstance();
} catch (Exception ex) {
System.err.println(ex); return;
}
QSqlDatabase db = QSqlDatabase.addDatabase("QJDBC");
db.setDatabaseName("jdbc:h2:~/testdb");
db.setUserName( "sa" );
db.setPassword( "" );
if (db.open()) {
System.out.println("Connected!");
} else {
System.out.println("Connection Failed!");
System.out.println(db.lastError().text());
return;
}
QSqlTableModel model = new QSqlTableModel(null, db);
model.setTable("TEST");
if (!model.select()) {
System.out.println("SELECT ERROR: " + model.lastError().text());
}
// ui.tblContacts is a TableView widget dragged into the designer
(MainWin.jui)
ui.tblContacts.setModel(model);
ui.tblContacts.show(); // <- COL HEADERS SHOW IN THE TABLE BUT NO DATA.
ARGH!!!
}
catch (Exception ex)
{
System.out.println("ERROR: " + ex.getMessage());
}
[end of code]