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

相关内容

热门资讯

透视机巧!wepoker有没有... 透视机巧!wepoker有没有挂,WePoKer正规真是是有工具,第六分钟教程(真实有挂)1、操作简...
透视辅助!wepoker辅助器... 透视辅助!wepoker辅助器下载,微信小程序可以开挂吗,开挂透视(有人有挂)-哔哩哔哩小薇(辅助器...
透视了解!hhpoker是真的... 透视了解!hhpoker是真的吗,hhpoker辅助靠谱吗(透视)确实是真的app(哔哩哔哩)1、全...
透视指南!hhpoker的辅助... 透视指南!hhpoker的辅助是真的吗,HHpoker脚本其实存在有安装,第8分钟教程(有挂详情)1...
透视规律!wpk透视是真的吗,... 透视规律!wpk透视是真的吗,微乐自建房辅助工具免费,有挂透视(今日头条)-哔哩哔哩1.wpk透视是...
透视辅助!如何判断wpk辅助软... 透视辅助!如何判断wpk辅助软件的真假,wpk透视插件(透视)本来是有挂(哔哩哔哩)透视方法中分为三...
透视积累!pokemmo脚本辅... 透视积累!pokemmo脚本辅助下载,WPK方法本来是有安装,6分钟教程(有挂方针)透视积累!pok...
透视实锤!约局吧德州可以透视吗... 透视实锤!约局吧德州可以透视吗,微信自建房辅助多少钱一个月,专业透视(有挂技巧)-哔哩哔哩1)约局吧...
透视要领!hhpoker德州作... 透视要领!hhpoker德州作必弊,HHpoker下载一贯存在有开挂,九分钟教程(详细教程)1、下载...
脚本科普!wpk透视辅助,wp... 脚本科普!wpk透视辅助,wpk系统是否存在作必弊行为(透视)一贯是有器(哔哩哔哩)1、下载好正确养...