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

相关内容

热门资讯

记者揭秘!智星菠萝辅助(透视辅... 记者揭秘!智星菠萝辅助(透视辅助)拱趴大菠萝辅助神器,扑克教程(有挂细节);模式供您选择,了解更新找...
一分钟揭秘!约局吧能能开挂(透... 一分钟揭秘!约局吧能能开挂(透视辅助)hhpoker辅助靠谱,2024新版教程(有挂教学);约局吧能...
透视辅助!wepoker模拟器... 透视辅助!wepoker模拟器哪个好用(脚本)hhpoker辅助挂是真的,科技教程(有挂技巧);囊括...
透视代打!hhpkoer辅助器... 透视代打!hhpkoer辅助器视频(辅助挂)pokemmo脚本辅助,2024新版教程(有挂教程);风...
透视了解!约局吧德州真的有透视... 透视了解!约局吧德州真的有透视挂(透视脚本)德州局HHpoker透视脚本,必胜教程(有挂分析);亲,...
六分钟了解!wepoker挂底... 六分钟了解!wepoker挂底牌(透视)德普之星开辅助,详细教程(有挂解密);德普之星开辅助是一种具...
9分钟了解!wpk私人辅助(透... 9分钟了解!wpk私人辅助(透视)hhpoker德州透视,插件教程(有挂教学);风靡全球的特色经典游...
推荐一款!wepoker究竟有... 推荐一款!wepoker究竟有透视(脚本)哈糖大菠萝开挂,介绍教程(有挂技术);囊括全国各种wepo...
每日必备!wepoker有人用... 每日必备!wepoker有人用过(脚本)wpk有那种辅助,线上教程(有挂规律);wepoker有人用...
玩家必备教程!wejoker私... 玩家必备教程!wejoker私人辅助软件(脚本)哈糖大菠萝可以开挂,可靠技巧(有挂神器)申哈糖大菠萝...