PDA

View Full Version : how to save database



khalid_se
4th January 2009, 18:16
hi
how are you?
i wan`t to ask how i can to save a database that i enter data in it and i use it in onther time like i make a database and i give it a name and i create tables in it but how i can save to use it data every time i enter to it .
i look in alot of books but i just see how to make query but no body talk how to save it
and if any body can give me a small example like address bokk or any small project.

wysota
4th January 2009, 22:17
Take a look at database examples distributed with Qt. They mostly work on sqlite databases which are plain files on your disk so you don't need a database server.

khalid_se
4th January 2009, 22:33
---------------(iam using sqlite)
i did not found any document or any subjects about ( database examples distributed with Qt.).
so i will be grateful if you give me a link or give any link about this subject.
and if you have a little program that connect with adatabase i will be so grateful to you

wysota
4th January 2009, 23:21
Open up Qt Assistant and click on "Examples" and then SQL in Qt Reference Manual.

schall_l
11th March 2009, 08:26
The example within QT Assistent are creating the same database content using the createConnection() function each time the program is launched.

I suppose the initial post was to ask how the content of the SQLITE database can be stored when the application ends and how this content can be reloaded again when the application is started the next time so that changes made to the database are persistent across reboots.

Do you have any example on this ?

schall_l
11th March 2009, 08:48
I think the answer would be something like this:

To store the database in memory only you can use something like:


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 the database connection.\n"
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}

QSqlQuery query;
query.exec("create table table1 (someIdentifier int, someNameAssociated varchar(30))");
query.exec("create table table2 (someIdentifier int, someDateAssociated datetime)");

return true;
}


While if you would like to make the database persistent accross reboots it should be saved in a file on the hard drive so you could use something only like:


static bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("myDatabase");
if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish the database connection.\n"
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}

QSqlQuery query;
query.exec("create table table1 (someIdentifier int, someNameAssociated varchar(30))");
query.exec("create table table2 (someIdentifier int, someDateAssociated datetime)");

return true;
}

Note the difference between the 2 versions of createConnection:

The first one is using db.setDatabaseName(":memory:");
The second one is using db.setDatabaseName("myDatabase");

Laurent

akon
11th March 2009, 08:48
I am not sure this would help or not, a code snipp to connect to database



database = QSqlDatabase::addDatabase("QSQLITE");
if(database.isValid()) {
QDir databasePath("path here");
QString databaseFile(full path and file name here);

if(database.open()) {
qDebug() << ;
} else {
qDebug() << ";
}
}

tomkonikkara
10th July 2011, 18:47
i did somewhat like scahall_i described above and it works. thanks.:cool: