我应该在哪里声明数据库对象 [英] where should I declare a database object

查看:97
本文介绍了我应该在哪里声明数据库对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++项目中使用Qt库,但是我有一个设计问题:应在哪里声明数据库?我宁愿不声明全局变量。

I am using the Qt libraries in a C++ project but I have a design question: where should a database be declared? I would prefer not to declare global variables.

当前,我正在以这种方式处理这个问题。我有一个主窗口,并且在那里声明了数据库,因此我在主窗口中执行查询,然后使用不同的信号和插槽将结果传递给对话框。

Currently I am dealing with this problem in this way. I have a mainwindow and I have declared the DB in there so I perform the queries in the main window and pass the results to the dialogs using different signals and slots.

I当主窗口启动时启动DB,并在关闭窗口时关闭它。我不知道这样是否可以。
现在我也需要另一个类中的数据库连接,以便可以将引用传递给数据库或使数据库成为全局数据库。

I start the DB when the main window starts and close it when the window has been closed. I don't know if this is ok Now I need the DB connection in another class as well so I can pass a reference to the DB or make the DB global

我不喜欢这些解决方案。.是否有一种标准的模式来处理这种情况?

I don't like these solutions.. is there a standard pattern to deal with this situation?

编辑:

我的课程现在看起来像:

My class now looks like:

class Database
{
  public:
    bool open(void);
    bool close(void);
    static Database* getDatabase(void);
    // various methods like loadThisTable(), saveThisTable() etc

  private:
    Database();                                // disable constructor
    ~Database();                               // disable destructor
    Database(const Database&);                 // disable copy constructor
    Database& operator=(const Database&);      // disable assignment

    static Database* instance_;                // database instance
    QSqlDatabase qtDB;                         // qt db database
}

如果我愿意,可以添加添加和删除方法但是我只有一个数据库实例。

If I want I can add the add and remove methods but I have a single DB instance.

推荐答案

如果您使用的是 QSqlDatabase ,您实际上并不需要使其成为全局变量。只需在首次启动应用程序时设置连接,然后在不同模块中需要连接时使用静态QSqlDatabase 方法访问该连接。

If you're using QSqlDatabase, you don't really need to make it a global variable. Just set up the connection when you first start your application, then use the static QSqlDatabase methods to access the connection when you need it in different modules.

示例

QSqlDatabase db;  // set up the default connection
// alternative:  set up a named connection
// QSqlDatabase db("conn-name");

// set the connection params and open the connection

// ... later on
QSqlDatabase db = QSqlDatabase::database();  // retrieve the default connection
// alternative:  retrieve the named connection
// QSqlDatabase db = QSqlDatabase::database("conn-name");

来自文档


QSqlDatabase 是一个值类。通过 QSqlDatabase 的一个实例对数据库连接所做的更改将影响 QSqlDatabase 的其他代表相同连接的实例。使用 cloneDatabase()创建基于现有数据库的独立数据库连接。

QSqlDatabase is a value class. Changes made to a database connection via one instance of QSqlDatabase will affect other instances of QSqlDatabase that represent the same connection. Use cloneDatabase() to create an independent database connection based on an existing one.

注意:如果您的应用程序是多线程的,则必须小心,仅在创建它的线程中使用连接。

Note: If you're application is multi-threaded, you have to be careful to only use a connection in the thread in which it was created.

这篇关于我应该在哪里声明数据库对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆