下面是一个使用Python编写存储库类以从3个链接的表中获取数据的示例代码:
import mysql.connector
class Repository:
def __init__(self):
self.db = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="yourdatabase"
)
self.cursor = self.db.cursor()
def get_data_from_table1(self):
self.cursor.execute("SELECT * FROM table1")
data = self.cursor.fetchall()
return data
def get_data_from_table2(self):
self.cursor.execute("SELECT * FROM table2")
data = self.cursor.fetchall()
return data
def get_data_from_table3(self):
self.cursor.execute("SELECT * FROM table3")
data = self.cursor.fetchall()
return data
repo = Repository()
table1_data = repo.get_data_from_table1()
table2_data = repo.get_data_from_table2()
table3_data = repo.get_data_from_table3()
print(table1_data)
print(table2_data)
print(table3_data)
请确保替换yourusername
,yourpassword
和yourdatabase
为实际的数据库凭据和数据库名称。此示例假设您使用的是MySQL数据库。您还可以根据需要修改SQL查询以适应您的表结构和数据需求。