PDA

View Full Version : Embedding binary data into an application



Valheru
1st September 2006, 07:48
I need to embed an sqlite database into my application. It's very small, only 2kb. I tried using a Qt resource file, but rcc chokes on it. Can rcc only embed pictures into an application? I can't see any way of actually CREATING a database through sql commands - Qt seems to need the database to physically exist to connect to :(

munna
1st September 2006, 08:39
I need to embed an sqlite database into my application.

I remember reading a similar post by someone but I am not able to find it now. I don't think you can do that.


I can't see any way of actually CREATING a database through sql commands - Qt seems to need the database to physically exist to connect to

Try this :

setDatabaseName will create a database if the file does not exists. Not sure, but I remember doing something similar.




QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(path);//Path where you want the database
if(db.open()){
if(db.tables().count() != 0){
return true;
}
else{
if(createTables()){
return true;
}
else{
//Error Message
}

Valheru
1st September 2006, 09:31
I remember reading a similar post by someone but I am not able to find it now. I don't think you can do that.



Try this :

setDatabaseName will create a database if the file does not exists. Not sure, but I remember doing something similar.




QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(path);//Path where you want the database
if(db.open()){
if(db.tables().count() != 0){
return true;
}
else{
if(createTables()){
return true;
}
else{
//Error Message
}



Meh, setDatabaseName() isn't creating anything - as I thought it wouldn't, since you are not specifying any layout for the database so the program doesn't know how to create the database.

munna
1st September 2006, 10:23
Below is the code that worked for me. First time the applicaion is launched, the database is created and then in createTables(), the required tables were created. From the second time onwards database is only opened.




bool AddressBookDB::openDatabase()
{
QString homePath = QDir::homePath();
homePath.append("/adressbook");
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(homePath);
if(db.open()){
if(db.tables().count() != 0){
return true;
}
else{
if(createTables()){
return true;
}
}
}
else{
return false;
}
return false;
}

Valheru
1st September 2006, 14:13
Below is the code that worked for me. First time the applicaion is launched, the database is created and then in createTables(), the required tables were created. From the second time onwards database is only opened.




bool AddressBookDB::openDatabase()
{
QString homePath = QDir::homePath();
homePath.append("/adressbook");
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(homePath);
if(db.open()){
if(db.tables().count() != 0){
return true;
}
else{
if(createTables()){
return true;
}
}
}
else{
return false;
}
return false;
}


Sorry, could I trouble you to post the contents of createTables()?

munna
1st September 2006, 14:23
The createTables() function is too big. Here is a part of it




QSqlQuery query;

if(query.exec("create table cgroup(id INTEGER PRIMARY KEY,name TEXT,category INTEGER,operator TEXT)")){
if(query.exec("insert into cgroup (id,name,category) values (NULL,'All',1)")){
if(query.exec("select * from cgroup")){
if(query.first()){
QSqlRecord r = query.record();
gid = r.value("id").toInt();
}
}
if(query.exec("create table group_contacts(id INTEGER PRIMARY KEY,"
"nameid INTEGER, groupid INTEGER, emailid INTEGER, phoneid INTEGER, addressid INTEGER)")){
if(query.exec("insert into group_contacts(id,nameid,groupid) "
"values (NULL, " + QString::number(nid) + ", " + QString::number(gid) +
")")){

}
}
}
}