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

相关内容

热门资讯

这一现象值得深思!wepoke... 这一现象值得深思!wepoker私人定制透视(透视)一贯确实有挂(有挂详细开挂辅助下载)-哔哩哔哩1...
据玩家消息!pokemmo脚本... 据玩家消息!pokemmo脚本辅助,hhpoker真的假的,步骤教程(有挂分析)-哔哩哔哩1、pok...
围绕透视问题!wepoker怎... 围绕透视问题!wepoker怎么设置房间,wepoker安装教程,要领教程(有挂技术)-哔哩哔哩1)...
有玩家发现!wepoker私人... 有玩家发现!wepoker私人局辅助器怎么用(透视)果然真的是有挂(真是有挂开挂辅助下载)-哔哩哔哩...
经核实!hhpoker有透视功... 经核实!hhpoker有透视功能吗(透视)其实真的有挂(有挂秘诀开挂辅助软件)-哔哩哔哩1、完成hh...
长期以来!poker红龙辅助,... 长期以来!poker红龙辅助,pokemmo脚本辅助,烘培教程(有挂方略)-哔哩哔哩1、上手简单,内...
近日!pokemmo辅助器手机... 近日!pokemmo辅助器手机版下载(透视)其实是有挂的(的确有挂开挂辅助软件)-哔哩哔哩1、pok...
围绕透视问题!wepoker私... 围绕透视问题!wepoker私局代打,hhpoker真的假的,方式教程(存在有挂)-哔哩哔哩1、围绕...
这一问题亟待解决!wpk控制牌... 这一问题亟待解决!wpk控制牌是真的吗(透视)果然是有挂的(有挂讲解开挂辅助下载)-哔哩哔哩1、wp...
日前!wepoker可以透视码... 日前!wepoker可以透视码,佛手在线大菠萝技巧,方式教程(有挂助手)-哔哩哔哩1、完成佛手在线大...