qt 哪位大侠有封装好的 *** 作数据库的类

qt 哪位大侠有封装好的 *** 作数据库的类,第1张

qt中有qtsql模块支持该 *** 作,底层一般都是用驱动插件实现的,利用的是sqlite数据库,相关的类有 qsqldatabase, qsqlquery,qsqlrecord等,谢谢采纳

<?php  

//数据库连接类,不建议直接使用DB,而是对DB封装一层  

//这个类不会被污染,不会被直接调用  

class DB {  

    //pdo对象  

    private $_pdo = null  

    //用于存放实例化的对象  

    static private $_instance = null  

      

    //公共静态方法获取实例化的对象  

    static protected function getInstance() {  

        if (!(self::$_instance instanceof self)) {  

            self::$_instance = new self()  

        }  

        return self::$_instance  

    }  

      

    //私有克隆  

    private function __clone() {}  

      

    //私有构造  

    private function __construct() {  

        try {  

            $this->_pdo = new PDO(DB_DNS, DB_USER, DB_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES '.DB_CHARSET))  

            $this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION)  

        } catch (PDOException $e) {  

            exit($e->getMessage())  

        }  

    }  

      

    //新增  

    protected function add($_tables, Array $_addData) {  

        $_addFields = array()  

        $_addValues = array()  

        foreach ($_addData as $_key=>$_value) {  

            $_addFields[] = $_key  

            $_addValues[] = $_value  

        }  

        $_addFields = implode(',', $_addFields)  

        $_addValues = implode("','", $_addValues)  

        $_sql = "INSERT INTO $_tables[0] ($_addFields) VALUES ('$_addValues')"  

        return $this->execute($_sql)->rowCount()  

    }  

      

    //修改  

    protected function update($_tables, Array $_param, Array $_updateData) {  

        $_where = $_setData = ''  

        foreach ($_param as $_key=>$_value) {  

            $_where .= $_value.' AND '  

        }  

        $_where = 'WHERE '.substr($_where, 0, -4)  

        foreach ($_updateData as $_key=>$_value) {  

            if (Validate::isArray($_value)) {  

                $_setData .= "$_key=$_value[0],"  

            } else {  

                $_setData .= "$_key='$_value',"  

            }  

        }  

        $_setData = substr($_setData, 0, -1)  

        $_sql = "UPDATE $_tables[0] SET $_setData $_where"  

        return $this->execute($_sql)->rowCount()  

    }  

      

    //验证一条数据  

    protected function isOne($_tables, Array $_param) {  

        $_where = ''  

        foreach ($_param as $_key=>$_value) {  

            $_where .=$_value.' AND '  

        }  

        $_where = 'WHERE '.substr($_where, 0, -4)  

        $_sql = "SELECT id FROM $_tables[0] $_where LIMIT 1"  

        return $this->execute($_sql)->rowCount()  

    }  

      

    //删除  

    protected function delete($_tables, Array $_param) {  

        $_where = ''  

        foreach ($_param as $_key=>$_value) {  

            $_where .= $_value.' AND '  

        }  

        $_where = 'WHERE '.substr($_where, 0, -4)  

        $_sql = "DELETE FROM $_tables[0] $_where LIMIT 1"  

        return $this->execute($_sql)->rowCount()  

    }  

      

    //查询  

    protected function select($_tables, Array $_fileld, Array $_param = array()) {  

        $_limit = $_order = $_where = $_like = ''  

        if (Validate::isArray($_param) && !Validate::isNullArray($_param)) {  

            $_limit = isset($_param['limit']) ? 'LIMIT '.$_param['limit'] : ''  

            $_order = isset($_param['order']) ? 'ORDER BY '.$_param['order'] : ''  

            if (isset($_param['where'])) {  

                foreach ($_param['where'] as $_key=>$_value) {  

                    $_where .= $_value.' AND '  

                }  

                $_where = 'WHERE '.substr($_where, 0, -4)  

            }  

            if (isset($_param['like'])) {  

                foreach ($_param['like'] as $_key=>$_value) {  

                    $_like = "WHERE $_key LIKE '%$_value%'"  

                }  

            }  

        }  

        $_selectFields = implode(',', $_fileld)  

        $_table = isset($_tables[1]) ? $_tables[0].','.$_tables[1] : $_tables[0]  

        $_sql = "SELECT $_selectFields FROM $_table $_where $_like $_order $_limit"  

        $_stmt = $this->execute($_sql)  

        $_result = array()  

        while (!!$_objs = $_stmt->fetchObject()) {  

            $_result[] = $_objs  

        }  

        return Tool::setHtmlString($_result)  

    }  

      

    //总记录  

    protected function total($_tables, Array $_param = array()) {  

        $_where = ''  

        if (isset($_param['where'])) {  

            foreach ($_param['where'] as $_key=>$_value) {  

                $_where .= $_value.' AND '  

            }  

            $_where = 'WHERE '.substr($_where, 0, -4)  

        }  

        $_sql = "SELECT COUNT(*) as count FROM $_tables[0] $_where"  

        $_stmt = $this->execute($_sql)  

        return $_stmt->fetchObject()->count  

    }  

      

    //得到下一个ID  

    protected function nextId($_tables) {  

        $_sql = "SHOW TABLE STATUS LIKE '$_tables[0]'"  

        $_stmt = $this->execute($_sql)  

        return $_stmt->fetchObject()->Auto_increment  

    }  

  

  

    //执行SQL  

    private function execute($_sql) {  

        try {  

            $_stmt = $this->_pdo->prepare($_sql)  

            $_stmt->execute()  

        } catch (PDOException  $e) {  

            exit('SQL语句:'.$_sql.'<br />错误信息:'.$e->getMessage())  

        }  

        return $_stmt  

    }  

}  

?>

一、链接数据库

(1)找到模块文件夹中的Conf文件夹,然后进行编写config.php文件

我这里是这样的文件路径

(2)打开这个config.php文件,然后找到父类配置文件convention.php文件,将关于"数据库"的部分复制粘贴到config.php配置文件中

1

2

3

4

5

6

7

8

9

/* 数据库设置 */

'DB_TYPE' => '', // 数据库类型

'DB_HOST' => '', // 服务器地址

'DB_NAME' => '', // 数据库名

'DB_USER' => '', // 用户名

'DB_PWD'=> '', // 密码

'DB_PORT' => '',// 端口

'DB_PREFIX' => '',// 数据库表前缀

'DB_FIELDS_CACHE' => true,// 启用字段缓存(开发时这个要写成false)

下面是我的数据库连接内容

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<?php

return array(

//'配置项'=>'配置值'

/* 数据库设置 */

'DB_TYPE' => 'mysql', // 数据库类型

'DB_HOST' => 'localhost', // 服务器地址

'DB_NAME' => 'test3', // 数据库名

'DB_USER' => 'root', // 用户名

'DB_PWD'=> '123', // 密码

'DB_PORT' => '3306',// 端口

'DB_PREFIX' => '', // 数据库表前缀

'DB_FIELDS_CACHE' => false,// 启用字段缓存(开发时这个要是false)

)

连接成功后,然后就是新建模型文件了


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9924711.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-03
下一篇 2023-05-03

发表评论

登录后才能评论

评论列表(0条)

保存