不同数据库的通用数据访问类
创始人
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方法断开与数据库的连接。

相关内容

热门资讯

微乐小程序黑科技!微信微乐小程... 微乐小程序黑科技!微信微乐小程序有窍门吗(开挂)工具-确实分享是有挂1、每一步都需要思考,不同水平的...
微信小程序黑科技免费!微乐小程... 微信小程序黑科技免费!微乐小程序辅助器出租(开挂)方法-原来专业是有挂辅助器是一种具有地方特色的麻将...
微乐小程序黑科技!微乐广西小程... 微乐小程序黑科技!微乐广西小程序(开挂)技巧-一直分享有挂1、这是跨平台的轻量版有透视,在线的操作超...
微乐小程序存在有挂!微乐降序自... 微乐小程序存在有挂!微乐降序自建房辅助app(开挂)技巧-总是开挂真的是有挂1、游戏颠覆性的策略玩法...
微信小程序黑科技免费!微乐家乡... 微信小程序黑科技免费!微乐家乡自建房辅助app(开挂)神器-真是教你存在有挂1、破解器简单,机器人操...
微乐小程序透视辅助!微信小程序... 微乐小程序透视辅助!微信小程序游戏充值破解(开挂)工具-原来必备有挂1、有没有辅助教程、有透视吗等服...
微乐小程序黑科技!微乐自建房脚... 微乐小程序黑科技!微乐自建房脚本免费下载入口(开挂)插件-果然有挂存在有挂1、有没有辅助教程、有透视...
微乐小程序透视挂!微乐河南家乡... 微乐小程序透视挂!微乐河南家乡麻将辅助脚本(开挂)app-其实教你是有挂1、不需要AI权限,帮助你快...
微乐小程序存在有挂!微信小程序... 微乐小程序存在有挂!微信小程序游戏充值破解(开挂)软件-本来推荐是真的挂1、下载好脚本下载之后点击打...
微乐小程序透视辅助!微乐小程序... 微乐小程序透视辅助!微乐小程序自建房插件免费(开挂)插件-切实解密是真的挂;能透视中分为三种模型:模...