Connecting to Databases

To access a database with QSqlQuery or QSqlQueryModel, create and open one or more database connections. Database connections are normally identified by connection name, not by database name. You can have multiple connections to the same database. QSqlDatabase also supports the concept of a default connection, which is an unnamed connection. When calling QSqlQuery or QSqlQueryModel member functions that take a connection name argument, if you don't pass a connection name, the default connection will be used. Creating a default connection is convenient when your application only requires one database connection.

Note the difference between creating a connection and opening it. Creating a connection involves creating an instance of class QSqlDatabase. The connection is not usable until it is opened. The following snippet shows how to create adefault connection and then open it:

     QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("bigblue");
db.setDatabaseName("flightdb");
db.setUserName("acarlson");
db.setPassword("1uTbSbAs");
bool ok = db.open();

The first line creates the connection object, and the last line opens it for use. In between, we initialize some connection information, including the database name, the host name, the user name, and the password. In this case, we are connecting to the MySQL database flightdb on the host bigblue. The "QMYSQL" argument to addDatabase() specifies the type of database driver to use for the connection. The set of database drivers included with Qt are shown in the table of supported database drivers.

The connection in the snippet will be the default connection, because we don't pass the second argument toaddDatabase(), which is the connection name. For example, here we establish two MySQL database connections named "first" and "second":

     QSqlDatabase firstDB = QSqlDatabase::addDatabase("QMYSQL", "first");
QSqlDatabase secondDB = QSqlDatabase::addDatabase("QMYSQL", "second");

After these connections have been initialized, open() for each one to establish the live connections. If the open() fails, it returns false. In that case, call QSqlDatabase::lastError() to get error information.

Once a connection is established, we can call the static function QSqlDatabase::database() from anywhere with a connection name to get a pointer to that database connection. If we don't pass a connection name, it will return the default connection. For example:

     QSqlDatabase defaultDB = QSqlDatabase::database();
QSqlDatabase firstDB = QSqlDatabase::database("first");
QSqlDatabase secondDB = QSqlDatabase::database("second");

To remove a database connection, first close the database using QSqlDatabase::close(), then remove it using the static method QSqlDatabase::removeDatabase().

连接数据库

与QSqlQuery或QSqlQueryModel访问数据库,创建和打开一个或多个数据库连接。数据库连接通常被连接的名字,而不是数据库名称。可以有多个相同的数据库连接。QSqlDatabase还支持一个默认连接的概念,这是一个不愿透露姓名的连接。当调用QSqlQuery或QSqlQueryModel成员函数,把一个连接名称的参数,如果你不通过连接名称,将使用默认的连接。创建一个默认的连接方便当您的应用程序只需要一个数据库连接。

注意创建连接的区别和打开它。创建一个连接涉及创建QSqlDatabase类的一个实例。不是可用的,直到它被打开的连接。以下代码片段显示了如何创建adefault连接,然后打开它:

     QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("bigblue");
db.setDatabaseName("flightdb");
db.setUserName("acarlson");
db.setPassword("1uTbSbAs");
bool ok = db.open();

创建连接对象,第一行和最后一行打开使用。在之间,我们初始化一些连接信息,包括数据库名称、主机名、用户名和密码。在这个例子中,我们连接到MySQL数据库主机bigblue flightdb。“QMYSQL”参数addDatabase()指定类型的数据库驱动程序用于连接。数据库驱动程序集包含在Qt支持的数据库驱动程序表所示。 片段的连接将被默认的连接,因为我们不通过第二个参数toaddDatabase(),它是连接的名字。例如,在这里我们建立两个MySQL数据库连接命名“第一”和“第二”:

     QSqlDatabase firstDB = QSqlDatabase::addDatabase("QMYSQL", "first");
QSqlDatabase secondDB = QSqlDatabase::addDatabase("QMYSQL", "second");

这些连接被初始化后,open()为每个人建立现场连接。如果开放()失败,它将返回错误的。在这种情况下,调用QSqlDatabase:lastError()来获取错误信息。 一旦建立连接,我们可以调用静态函数QSqlDatabase::数据库()从任何连接的名字让指针,数据库连接。如果我们不通过连接的名字,它将返回默认的连接。例如:

     QSqlDatabase defaultDB = QSqlDatabase::database();
QSqlDatabase firstDB = QSqlDatabase::database("first");
QSqlDatabase secondDB = QSqlDatabase::database("second");

先删除一个数据库连接,关闭数据库使用QSqlDatabase::关闭(),然后使用静态方法QSqlDatabase删除它:removeDatabase()。

SQL in Qt (一)的更多相关文章

  1. Qt SQL Programming 部分翻译

    简介:      Qt SQL 是 Qt 的重要模块之一,为了方便,Qt 对 SQL 进行了一系列的封装,并将 SQL API 分为如下三层:      (1)驱动层      (2)SQL API ...

  2. Qt入门(20)——Qt模块简介

    当你安装Qt时,这些模块会被构建到库中.在Qt企业版.Qt评估版和Qt自由版中,包含所有的模块.对于Qt专业版,提供基本的模块--工具.核心.窗口部件.对话框.图标视图和工作区模块.画布模块画布模块提 ...

  3. Qt 学习之路 :Qt 模块简介

    Qt 5 与 Qt 4 最大的一个区别之一是底层架构有了修改.Qt 5 引入了模块化的概念,将众多功能细分到几个模块之中.Qt 4 也有模块的概念,但是是一种很粗的划分,而 Qt 5 则更加细化.本节 ...

  4. [QT]QT概述

    QT概述 基于C++的GUI开发框架,跨平台.Qt 是一个用于桌面系统和嵌入式开发的跨平台应用程序框架. QT是挪威TROLLTECH公司开发的跨平台C++工具,在UNIX下非常出名:他的宗旨是“一次 ...

  5. Qt 5简介

    Qt 5简介 Qt 5概要介绍 在Qt 5这个版本中,Qt Quick成为了Qt的核心.但是Qt 5也继续提供了本地C++强大的功能来完成更好的用户体验,也提供了对OpenGL/OpenGL ES图形 ...

  6. 5.Qt模块简介

    Qt 5 与 Qt 4 最大的一个区别之一是底层架构有了修改.Qt 5 引入了模块化的概念,将众多功能细分到几个模块之中.Qt 4 也有模块的概念,但是是一种很粗的划分,而 Qt 5 则更加细化.本节 ...

  7. Qt学习(一)

    1. 简介 跨平台 GUI 通常有三种实现策略 API 映射 相当于将不同平台的 API 提取公共部分.界面库使用同一套 API,将其映射到不同的底层平台上面.相当于取交集 如wxWidgets. 优 ...

  8. Qt 学习之路 2(6):Qt 模块简介

    Home / Qt 学习之路 2 / Qt 学习之路 2(6):Qt 模块简介  豆子  2012年8月26日  Qt 学习之路 2  20条评论 Qt 5 与 Qt 4 最大的一个区别之一是底层架构 ...

  9. 海思3559A QT 5.12移植(带webengine 和 opengl es)

    海思SDK版本:Hi3559AV100_SDK_V2.0.1.0 编译器版本:aarch64-himix100-linux-gcc 6.3.0(这个版本有点小问题,使用前需要先清除本地化设置) $ e ...

随机推荐

  1. ios开发 AFNetworking的基本使用方法

    AFNetworking的基本使用方法 什么是GET请求? 如果只是单纯的下载数据, 使用GET请求 什么是POST请求? 特点:  请求的内容不会出现在URL网址中 向服务器发送用户名和密码, 或者 ...

  2. 二维计算几何基础题目泛做(SYX第一轮)

    题目1: POJ 2318 TOYS 题目大意: 给一个有n个挡板的盒子,从左到右空格编号为0...n.有好多玩具,问每个玩具在哪个空格里面. 算法讨论: 直接叉积判断就可以.注意在盒子的边界上面也算 ...

  3. POJ 1723 SOLDIERS (中位数)

    题目大意: 平面上有N(N<=10000)个点,求这些点变成一条水平线的最小移动步数. 算法讨论: 表示自己太弱弱了,打算从今天开始提高一下智商. 我们考虑,既然是要成一条水平线,那么这条直线的 ...

  4. [hdu5136]Yue Fei's Battle 2014 亚洲区域赛广州赛区J题(dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 现场赛的时候由于有个地方有点小问题,没有成功AC,导致与金牌失之交臂. 由于今天下 ...

  5. mysql中查看字符集的cmd指令

    参看下面链接:http://blog.chinaunix.net/uid-20180960-id-1972668.html

  6. Citrix 服务器虚拟化之十二 Xenserver灾难恢复

    Citrix 服务器虚拟化之十二 Xenserver灾难恢复 (环境有限实验无法测试,配置步骤摘取自官方文档) XenServer 灾难恢复的工作原理在存储库(SR)上还原从主(生产)环境复制到备份环 ...

  7. 极客”一词,来自于美国俚语“geek”的音译,一般理解为性格古怪的人

    起源 “ 极客”一词,来自于美国俚语“ geek”的音译,一般理解为性格古怪的人.数学“极客”大多是指,并不 一定是数学专业但又对数学等技术有狂热的兴趣并投入大量时间钻研的人.又 译作“ 奇客”.以前 ...

  8. Lucene学习总结之六:Lucene打分公式的数学推导

    在进行Lucene的搜索过程解析之前,有必要单独的一张把Lucene score公式的推导,各部分的意义阐述一下.因为Lucene的搜索过程,很重要的一个步骤就是逐步的计算各部分的分数. Lucene ...

  9. TeXLive安装过程

    Linux系统下TeXLive2016安装教程:http://www.linuxidc.com/Linux/2016-08/133913.htm 安装完成后,在当前用户的 ~/.bashrc 中加入如 ...

  10. py2exe 生成带图标的单个文件实例

    随便拉了个学习时用的测试程序来做的实例,原程序如下: #Filename:for.py count=0 for i in range(1,100,2): count+=i else: print 't ...