Embedding binary data into an application
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 :(
Re: Embedding binary data into an application
Quote:
Originally Posted by Valheru
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.
Quote:
Originally Posted by Valheru
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.
Code:
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
}
Re: Embedding binary data into an application
Quote:
Originally Posted by munna
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.
Code:
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.
Re: Embedding binary data into an application
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.
Code:
bool AddressBookDB::openDatabase()
{
homePath.append("/adressbook");
db.setDatabaseName(homePath);
if(db.open()){
if(db.tables().count() != 0){
return true;
}
else{
if(createTables()){
return true;
}
}
}
else{
return false;
}
return false;
}
Re: Embedding binary data into an application
Quote:
Originally Posted by munna
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.
Code:
bool AddressBookDB::openDatabase()
{
homePath.append("/adressbook");
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()?
Re: Embedding binary data into an application
The createTables() function is too big. Here is a part of it
Code:
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()){
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
) + ")")){
}
}
}
}