不同数据库的通用数据访问类
创始人
2025-01-09 18:00:49
0

解决这个问题的方法是创建一个通用的数据访问类,它可以适用于不同的数据库。下面是一个示例代码,展示了如何实现这个通用的数据访问类:

import pymysql
import psycopg2
import pyodbc

class Database:
    def __init__(self, db_type, host, username, password, database):
        self.db_type = db_type
        self.host = host
        self.username = username
        self.password = password
        self.database = database
        self.connection = None
        self.cursor = None

    def connect(self):
        if self.db_type == 'mysql':
            self.connection = pymysql.connect(host=self.host,
                                              user=self.username,
                                              password=self.password,
                                              database=self.database)
            self.cursor = self.connection.cursor()
        elif self.db_type == 'postgresql':
            self.connection = psycopg2.connect(host=self.host,
                                               user=self.username,
                                               password=self.password,
                                               database=self.database)
            self.cursor = self.connection.cursor()
        elif self.db_type == 'mssql':
            conn_str = f'DRIVER=ODBC Driver 17 for SQL Server;SERVER={self.host};DATABASE={self.database};UID={self.username};PWD={self.password}'
            self.connection = pyodbc.connect(conn_str)
            self.cursor = self.connection.cursor()
        else:
            raise Exception('Unsupported database type')

    def execute_query(self, query):
        self.cursor.execute(query)
        result = self.cursor.fetchall()
        return result

    def execute_update(self, query):
        self.cursor.execute(query)
        self.connection.commit()

    def disconnect(self):
        self.cursor.close()
        self.connection.close()

# 使用示例
db = Database('mysql', 'localhost', 'root', 'password', 'mydatabase')
db.connect()

# 执行查询
result = db.execute_query('SELECT * FROM mytable')
print(result)

# 执行更新
db.execute_update('UPDATE mytable SET column1 = 1 WHERE column2 = 2')

db.disconnect()

这个通用的数据访问类使用了不同的数据库连接库(pymysql、psycopg2和pyodbc)来连接不同类型的数据库(MySQL、PostgreSQL和SQL Server)。可以根据具体的需求,使用适当的数据库连接库和连接参数来初始化Database类的实例。然后,可以使用execute_query方法执行查询语句并返回结果,使用execute_update方法执行更新语句,最后使用disconnect方法断开与数据库的连接。

相关内容

热门资讯

玩家必备教程!cloudpok... 自定义we-poker靠谱吗系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器...
一分钟了解!德普之星私人局透视... 一分钟了解!德普之星私人局透视,wepoker辅助器如何使用,新2025教程(有挂方法);wepok...
一起来讨论!安装不了wepok... 一起来讨论!安装不了wepoker,wepoker插件功能辅助器,2025新版(有挂透明)这是由厦门...
来一盘!哈糖大菠萝万能挂,we... 来一盘!哈糖大菠萝万能挂,wepoker辅助软件价格,教你攻略(有挂辅助)科技教程也叫必备教程,这是...
推荐十款!哈糖大菠萝有挂吗5个... 推荐十款!哈糖大菠萝有挂吗5个常用方法,agpoker辅助,详细教程(有挂技巧),支持语音通讯、好友...
一分钟秒懂!wejoker辅助... 一分钟秒懂!wejoker辅助软件价格,wejoker辅助机器人,必备教程(有挂透视);致您一封信;...
查到实测!智星菠萝有挂吗,we... 《查到实测!智星菠萝有挂吗,wepoker透视辅助下载,wpk教程(有挂透明)》 wepoker透视...
重磅来袭!aapoker透视方... 自定义wepoker到底有没有透视系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用...
一分钟教会你!wepoker永... 一分钟教会你!wepoker永久免费脚本,hhpoker免费透视脚本,微扑克教程(有挂透视)1、点击...
分享一款!wepoker有没有... 分享一款!wepoker有没有挂,hhpoker是正品吗,科技教程(有挂技巧);小薇(透视辅助)致您...