2012-02-13 88 views
2

我有一个应用程序处理用户编辑的多个数据库,我也希望它从指定的路径打开它们(如我得到的路径使用QFileDialog)。Qt和数据库保存并打开它们

此外,我看到它保存的数据库文件的可执行文件是,但有没有办法,我可以把它们保存在另一个地方?

回答

2

如果你正在使用sqlite,那么你只需要传递给db.setDatabaseName(file_name)(其中db是连接)文件的路径,你可以使用QFileDialog进行选择。

如果您正在使用其他数据库,那么您可能只需连接到数据库服务器。

+0

非常感谢你!你拯救了我的一天,我只是不会想象它那么容易 – Andrew 2012-02-13 13:06:47

1

首先,您正在使用哪个数据库; MySQL,SQLite。如果你正在使用SQLite,这非常简单。您在添加数据库时指定一个文件名。例如:

//get the database file with QFileDialog 
QString fileName = QFileDialog::getOpenFileName(this, 
tr("Open Database"), "/home/yourhome", tr("SQLite Database Files (*.sqlite)")); 

//add the new database 
QSqlDatabase db = QSqlDatabase::addDatabase("SQLITE"); 
db.setHostName("localhost"); 
db.setDatabaseName(fileName); 
//now your database will be stored in fileName 
相关问题