PDA

View Full Version : Qt SQL help



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();
}

eva2002
26th January 2010, 01:50
I manage to get it to work.. However I need to know whether is it possible to save the database into a file or something so next time I open the application, I can load the database.

I am using SQLite.

tangential
3rd February 2010, 06:24
Try this: http://www.sqlite.org/backup.html
Get the sqlite3 handle via reinterpret_cast<sqlite3*>(db->handle());

If you have an older version of sqlite installed, then you may have to use the sqlite attach command (via sql), and select from one database into the other (table by table).