eva2002
25th January 2010, 05:04
Hi all,
I get tarted using qt sql and tried out a simple example but I could not get it to work.
I got 2 problems here.
1) I can't get the items even if I pass the insert sql statement.
2) I have this database which contain 3 tables
map
--id <PK>
--path
group
--gid <PK>
--name
--map_id <FK>
place
--pid <PK>
--name
--group_id <FK>
I want to link them up. Did I do it correctly (in my code)?
If I want to insert records to each table, do i have to have a QSqlRelationalTableModel for each model. If so, how can I set up the map table? it is the top most table.
QMainWindow.cpp
QSqlRelationalTableModel *placeTable; <-- this is in the header file
placeTable = new QSqlRelationalTableModel(this);
placeTable->setTable("place");
placeTable->setRelation(4, QSqlRelation("group", "gid", "name"));
placeTable->select();
std::cout << placeTable->rowCount() << std::endl;
return;
groupTable = new QSqlRelationalTableModel(this);
groupTable->setTable("group");
groupTable->setRelation(2, QSqlRelation("map", "id", "path"));
database.h
static bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}
QSqlQuery query;
query.exec("create table map (id int primary key, path varchar(20))");
query.exec("insert into map values(0, 'image/ntu.jpg')");
query.exec("create table group (gid int primary key, gname varchar(20), mapid int");
query.exec("insert into group values(0, 'Lecture Halls', 0)");
query.exec("insert into group values(1, 'Tutorial Room', 0)");
query.exec("insert into group values(2, 'Laboratory', 0)");
query.exec("create table place (pid int primary key, pname varchar(20), posX int, posY int, groupid int)");
query.exec("insert into place values(0, 'LT 01', 30, 100, 0)");
query.exec("insert into place values(1, 'LT 02', 40, 110, 0)");
query.exec("insert into place values(2, 'LT 03', 50, 120, 0)");
query.exec("insert into place values(3, 'TR 01', 60, 130, 1)");
query.exec("insert into place values(4, 'TR 02', 70, 140, 1)");
query.exec("insert into place values(5, 'TR 03', 80, 150, 1)");
query.exec("insert into place values(6, 'Chem Lab', 90, 130, 2)");
query.exec("insert into place values(7, 'Digital Lab', 100, 140, 2)");
query.exec("insert into place values(8, 'Computer Lab', 110, 150, 2)");
return true;
}
main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
if (!createConnection())
return 1;
MainWindow w;
w.show();
//w.showFullScreen();
return a.exec();
}
I get tarted using qt sql and tried out a simple example but I could not get it to work.
I got 2 problems here.
1) I can't get the items even if I pass the insert sql statement.
2) I have this database which contain 3 tables
map
--id <PK>
--path
group
--gid <PK>
--name
--map_id <FK>
place
--pid <PK>
--name
--group_id <FK>
I want to link them up. Did I do it correctly (in my code)?
If I want to insert records to each table, do i have to have a QSqlRelationalTableModel for each model. If so, how can I set up the map table? it is the top most table.
QMainWindow.cpp
QSqlRelationalTableModel *placeTable; <-- this is in the header file
placeTable = new QSqlRelationalTableModel(this);
placeTable->setTable("place");
placeTable->setRelation(4, QSqlRelation("group", "gid", "name"));
placeTable->select();
std::cout << placeTable->rowCount() << std::endl;
return;
groupTable = new QSqlRelationalTableModel(this);
groupTable->setTable("group");
groupTable->setRelation(2, QSqlRelation("map", "id", "path"));
database.h
static bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}
QSqlQuery query;
query.exec("create table map (id int primary key, path varchar(20))");
query.exec("insert into map values(0, 'image/ntu.jpg')");
query.exec("create table group (gid int primary key, gname varchar(20), mapid int");
query.exec("insert into group values(0, 'Lecture Halls', 0)");
query.exec("insert into group values(1, 'Tutorial Room', 0)");
query.exec("insert into group values(2, 'Laboratory', 0)");
query.exec("create table place (pid int primary key, pname varchar(20), posX int, posY int, groupid int)");
query.exec("insert into place values(0, 'LT 01', 30, 100, 0)");
query.exec("insert into place values(1, 'LT 02', 40, 110, 0)");
query.exec("insert into place values(2, 'LT 03', 50, 120, 0)");
query.exec("insert into place values(3, 'TR 01', 60, 130, 1)");
query.exec("insert into place values(4, 'TR 02', 70, 140, 1)");
query.exec("insert into place values(5, 'TR 03', 80, 150, 1)");
query.exec("insert into place values(6, 'Chem Lab', 90, 130, 2)");
query.exec("insert into place values(7, 'Digital Lab', 100, 140, 2)");
query.exec("insert into place values(8, 'Computer Lab', 110, 150, 2)");
return true;
}
main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
if (!createConnection())
return 1;
MainWindow w;
w.show();
//w.showFullScreen();
return a.exec();
}