Archived
添加了支持MySQL数据库的测试代码
This commit is contained in:
6 files changed
+601
-215
No files matched your search
@@ -0,0 +1,128 @@
|
||||
#!/usr/bin/env python3
|
||||
# -- coding:utf-8 --
|
||||
# @Author: markushammered@gmail.com
|
||||
# @Development Tool: PyCharm
|
||||
# @Create Time: 2022/2/26
|
||||
# @File Name: MySQL.py
|
||||
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from tables import * # 导入所有的主题表模型
|
||||
from typing import List, Tuple
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
class MySQL:
|
||||
"""使用SQLAlchemy进行的MySQL数据库操作"""
|
||||
|
||||
@property
|
||||
def session(self):
|
||||
"""
|
||||
创建会话
|
||||
:return:
|
||||
"""
|
||||
return self.Session()
|
||||
|
||||
def __init__(self,
|
||||
host: str,
|
||||
user: str,
|
||||
pwd: str,
|
||||
database: str,
|
||||
charset: str = 'utf8',
|
||||
port: int = 3306
|
||||
) -> None:
|
||||
"""
|
||||
初始化
|
||||
:param host: 数据库地址
|
||||
:param port: 数据库端口默认3306
|
||||
:param user: 用户名
|
||||
:param pwd: 登陆密码
|
||||
:param database: 数据库名
|
||||
:param charset: 字符集 默认utf8
|
||||
"""
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.user = user
|
||||
self.password = pwd
|
||||
self.database = database
|
||||
self.charset = charset
|
||||
self.engine = create_engine(
|
||||
f'mysql+mysqlconnector://{self.user}:{self.password}@{self.host}:{self.port}/{self.database}') # 连接到数据库
|
||||
self.Session = sessionmaker(bind=self.engine) # 创建引擎
|
||||
|
||||
def __del__(self):
|
||||
"""
|
||||
自动提交
|
||||
自动关闭连接
|
||||
:return:
|
||||
"""
|
||||
self.session.commit()
|
||||
self.session.close()
|
||||
|
||||
def insert(self, name: str, times: int = 0) -> bool:
|
||||
"""
|
||||
插入数据
|
||||
:param name: 名称
|
||||
:param times: 次数 默认0
|
||||
:return:
|
||||
"""
|
||||
new_data = ReqCount(name=name, times=times)
|
||||
self.session.add(new_data)
|
||||
self.session.commit()
|
||||
return True
|
||||
|
||||
def update(self, name: str, times: int) -> bool:
|
||||
"""
|
||||
更新数据
|
||||
:param name: 名称
|
||||
:param times: 次数
|
||||
:return:
|
||||
"""
|
||||
self.session.query(ReqCount).filter(ReqCount.name == name).update({'name': name, 'times': times})
|
||||
return True
|
||||
|
||||
def delete(self, name: str) -> bool:
|
||||
"""
|
||||
删除数据
|
||||
:param name: 名称
|
||||
:return:
|
||||
"""
|
||||
self.session.query(ReqCount).filter(ReqCount.name == name)
|
||||
return True
|
||||
|
||||
def fetch(self, name: str) -> int:
|
||||
"""
|
||||
查询数据
|
||||
:param name:
|
||||
:return:
|
||||
"""
|
||||
data = self.session.query(ReqCount).filter(ReqCount.name == name).all()
|
||||
for i in data:
|
||||
print(i.times)
|
||||
|
||||
def fetchall(self) -> List[Tuple]:
|
||||
"""
|
||||
抓取全部数据
|
||||
:return:
|
||||
"""
|
||||
data_list = []
|
||||
data = self.session.query(ReqCount).filter().all()
|
||||
for tup in data:
|
||||
data_list.append((tup.name, tup.times))
|
||||
|
||||
return data_list
|
||||
|
||||
def fetch_table(self, table_name: str) -> List[Tuple] or bool:
|
||||
"""
|
||||
抓取指定名称的表
|
||||
:param table_name:
|
||||
:return:
|
||||
"""
|
||||
table_list = []
|
||||
tables = self.session.query(eval(table_name)).filter().all() # 使用eval函数将字符串转换为Python对象
|
||||
for tup in tables:
|
||||
table_list.append((tup.k, tup.v, tup.w, tup.h))
|
||||
return table_list
|
||||
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env python3
|
||||
# -- coding:utf-8 --
|
||||
# @Author: markushammered@gmail.com
|
||||
# @Development Tool: PyCharm
|
||||
# @Create Time: 2022/2/26
|
||||
# @File Name: __init__.py.py
|
||||
|
||||
|
||||
@@ -0,0 +1,376 @@
|
||||
#!/usr/bin/env python3
|
||||
# -- coding:utf-8 --
|
||||
# @Author: markushammered@gmail.com
|
||||
# @Development Tool: PyCharm
|
||||
# @Create Time: 2022/2/26
|
||||
# @File Name: tables.py
|
||||
|
||||
|
||||
from sqlalchemy import Column
|
||||
from sqlalchemy import Integer
|
||||
from sqlalchemy import String
|
||||
from sqlalchemy import Text
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
"""所有已保存的主题均在此处列出"""
|
||||
|
||||
|
||||
class ReqCount(Base):
|
||||
"""计数器数据库模型"""
|
||||
|
||||
__tablename__ = 'ReqCount'
|
||||
name = Column(String(20), primary_key=True) # 名称
|
||||
times = Column(Integer()) # 次数
|
||||
|
||||
|
||||
class gelbooru(Base):
|
||||
__tablename__ = 'gelbooru'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class moebooru(Base):
|
||||
__tablename__ = 'moebooru'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class g(Base):
|
||||
__tablename__ = 'g'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class cripple(Base):
|
||||
__tablename__ = 'cripple'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class blacked(Base):
|
||||
__tablename__ = 'blacked'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class rule34(Base):
|
||||
__tablename__ = 'rule34'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class steambanner(Base):
|
||||
__tablename__ = 'steambanner'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class lefty(Base):
|
||||
__tablename__ = 'lefty'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class crewbooru(Base):
|
||||
__tablename__ = 'crewbooru'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class sthg(Base):
|
||||
__tablename__ = 'sthg'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class rfck(Base):
|
||||
__tablename__ = 'rfck'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class lisu(Base):
|
||||
__tablename__ = 'lisu'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class tv(Base):
|
||||
__tablename__ = 'tv'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class lewd(Base):
|
||||
__tablename__ = 'lewd'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class amibooru(Base):
|
||||
__tablename__ = 'amibooru'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class blankatlas(Base):
|
||||
__tablename__ = 'blankatlas'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class mmballbusting(Base):
|
||||
__tablename__ = 'mmballbusting'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class sss(Base):
|
||||
__tablename__ = 'sss'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class legolamb(Base):
|
||||
__tablename__ = 'legolamb'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class goldengator(Base):
|
||||
__tablename__ = 'goldengator'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class r6gdrawfriends(Base):
|
||||
__tablename__ = 'r6gdrawfriends'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class vivi(Base):
|
||||
__tablename__ = 'vivi'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class twifanartsfw(Base):
|
||||
__tablename__ = 'twifanartsfw'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class hololive(Base):
|
||||
__tablename__ = 'hololive'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class vglobby(Base):
|
||||
__tablename__ = 'vglobby'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class jaypee(Base):
|
||||
__tablename__ = 'jaypee'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class melanin(Base):
|
||||
__tablename__ = 'melanin'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class orb(Base):
|
||||
__tablename__ = 'orb'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class min(Base):
|
||||
__tablename__ = 'min'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class mjg(Base):
|
||||
__tablename__ = 'mjg'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class cloppers(Base):
|
||||
__tablename__ = 'cloppers'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class townofgravityfalls(Base):
|
||||
__tablename__ = 'townofgravityfalls'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class brown(Base):
|
||||
__tablename__ = 'brown'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class enacdoa(Base):
|
||||
__tablename__ = 'enacdoa'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class daifuku(Base):
|
||||
__tablename__ = 'daifuku'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class osc(Base):
|
||||
__tablename__ = 'osc'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class girlsfeet(Base):
|
||||
__tablename__ = 'girlsfeet'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class hybreedsgeneral(Base):
|
||||
__tablename__ = 'hybreedsgeneral'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class sr(Base):
|
||||
__tablename__ = 'sr'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class mono(Base):
|
||||
__tablename__ = 'mono'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class riskofrain(Base):
|
||||
__tablename__ = 'riskofrain'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class neovb(Base):
|
||||
__tablename__ = 'neovb'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
class ffsr(Base):
|
||||
__tablename__ = 'ffsr'
|
||||
k = Column(Text, primary_key=True)
|
||||
v = Column(Text)
|
||||
w = Column(Integer())
|
||||
h = Column(Integer())
|
||||
|
||||
|
||||
__all__ = ['ReqCount', 'gelbooru', 'moebooru', 'g', 'cripple', 'blacked', 'rule34', 'steambanner', 'lefty', 'crewbooru',
|
||||
'sthg', 'rfck', 'lisu', 'tv', 'lewd', 'amibooru', 'blankatlas', 'mmballbusting', 'sss', 'legolamb',
|
||||
'goldengator', 'r6gdrawfriends', 'vivi', 'twifanartsfw', 'hololive', 'vglobby', 'jaypee', 'melanin', 'orb',
|
||||
'min', 'mjg', 'cloppers', 'townofgravityfalls', 'brown', 'enacdoa', 'daifuku', 'osc', 'girlsfeet',
|
||||
'hybreedsgeneral', 'sr', 'mono', 'riskofrain', 'neovb', 'ffsr']
|
||||
@@ -1,213 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -- coding:utf-8 --
|
||||
# @Author: markushammered@gmail.com
|
||||
# @Development Tool: PyCharm
|
||||
# @Create Time: 2022/2/25
|
||||
# @File Name: MySQL_.py
|
||||
|
||||
|
||||
import pymysql
|
||||
|
||||
|
||||
class MySQL:
|
||||
"""操作MySQL数据库"""
|
||||
|
||||
def __init__(self,
|
||||
host: str,
|
||||
user: str,
|
||||
password: str,
|
||||
database: str,
|
||||
charset: str = 'utf8',
|
||||
port: int = 3306
|
||||
) -> None:
|
||||
"""
|
||||
初始化类
|
||||
:param host: 数据库地址
|
||||
:param user: 数据库用户
|
||||
:param password: 数据库密码
|
||||
:param database: 数据库名称
|
||||
:param charset: 字符集 默认utf8
|
||||
:param port: 端口地址
|
||||
"""
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.user = user
|
||||
self.password = password
|
||||
self.database = database
|
||||
self.charset = charset
|
||||
# self.__create_database() # 自动创建数据库
|
||||
|
||||
def __create_database(self) -> bool:
|
||||
"""
|
||||
创建数据库
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
conn = pymysql.connect(host=self.host,
|
||||
port=self.port,
|
||||
user=self.user,
|
||||
passwd=self.password,
|
||||
local_infile=True,
|
||||
charset=self.charset)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('CREATE DATABASE IF NOT EXISTS %(db)s DEFAULT CHARSET utf8;' % {'db': self.database})
|
||||
conn.commit()
|
||||
return True
|
||||
except pymysql.err.OperationalError:
|
||||
print(f'无法连接到数据库: {self.database}')
|
||||
return False
|
||||
finally:
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
def __import_database(self):
|
||||
"""
|
||||
导入本地数据库
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
conn = pymysql.connect(host=self.host,
|
||||
port=self.port,
|
||||
user=self.user,
|
||||
passwd=self.password,
|
||||
local_infile=True,
|
||||
charset=self.charset)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(f'use {self.database};')
|
||||
cursor.execute(f'source ./static/cache/db.sql;')
|
||||
conn.commit()
|
||||
except pymysql.err.OperationalError:
|
||||
print(f'无法连接到数据库: {self.database}')
|
||||
return False
|
||||
finally:
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
def insert(self, name: str) -> bool:
|
||||
"""
|
||||
新增数据
|
||||
:param name: 名称
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
conn = pymysql.connect(host=self.host,
|
||||
port=self.port,
|
||||
user=self.user,
|
||||
password=self.password,
|
||||
database=self.database,
|
||||
charset=self.charset)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
'insert into reqcount (name, times) values("%(name)s", 0);' % {'name': name})
|
||||
conn.commit()
|
||||
return True
|
||||
except pymysql.err.OperationalError:
|
||||
print(f'无法连接到数据库: {self.database}')
|
||||
return False
|
||||
finally:
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
def update(self, name: str, times: int) -> bool:
|
||||
"""
|
||||
更新数据
|
||||
:param name: 名称
|
||||
:param times: 次数
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
conn = pymysql.connect(host=self.host,
|
||||
port=self.port,
|
||||
user=self.user,
|
||||
password=self.password,
|
||||
database=self.database,
|
||||
charset=self.charset)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
'insert into reqcount (name, times) values("%(name)s", %(times)s);' % {'name': name, 'times': times})
|
||||
conn.commit()
|
||||
return True
|
||||
except pymysql.err.OperationalError:
|
||||
print(f'无法连接到数据库: {self.database}')
|
||||
return False
|
||||
finally:
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
def delete(self, name: str) -> bool:
|
||||
"""
|
||||
删除数据
|
||||
:param name: 名称
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
conn = pymysql.connect(host=self.host,
|
||||
port=self.port,
|
||||
user=self.user,
|
||||
password=self.password,
|
||||
database=self.database,
|
||||
charset=self.charset)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('delete from reqcount where name"=%(name)s"' % {'name': name})
|
||||
conn.commit()
|
||||
return True
|
||||
except pymysql.err.OperationalError:
|
||||
print(f'无法连接到数据库: {self.database}')
|
||||
return False
|
||||
finally:
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
def fetch(self, name: str, fetchall: bool = False):
|
||||
"""
|
||||
抓取数据
|
||||
:param fetchall: 是否抓取全部
|
||||
:param name: 名称
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
conn = pymysql.connect(host=self.host,
|
||||
port=self.port,
|
||||
user=self.user,
|
||||
password=self.password,
|
||||
database=self.database,
|
||||
charset=self.charset)
|
||||
cursor = conn.cursor()
|
||||
if not fetchall:
|
||||
cursor.execute('select * from reqcount where name="%(name)s"' % {'name': name})
|
||||
return cursor.fetchall()
|
||||
else:
|
||||
cursor.execute('select * from reqcount')
|
||||
except pymysql.err.OperationalError:
|
||||
print(f'无法连接到数据库: {self.database}')
|
||||
return False
|
||||
finally:
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
def fetch_table(self, table_name: str, fetchall: bool = False):
|
||||
"""
|
||||
抓取已有表名称
|
||||
:param table_name:
|
||||
:param fetchall: 是否抓取全部
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
conn = pymysql.connect(host=self.host,
|
||||
port=self.port,
|
||||
user=self.user,
|
||||
password=self.password,
|
||||
database=self.database,
|
||||
charset=self.charset)
|
||||
cursor = conn.cursor()
|
||||
if not fetchall:
|
||||
cursor.execute('select * from reqcount where name="%(name)s"' % {'name': table_name})
|
||||
return cursor.fetchall()
|
||||
else:
|
||||
cursor.execute('select * from reqcount')
|
||||
except pymysql.err.OperationalError:
|
||||
print(f'无法连接到数据库: {self.database}')
|
||||
return False
|
||||
finally:
|
||||
cursor.close()
|
||||
conn.close()
|
||||
+71
-2
@@ -7,5 +7,74 @@
|
||||
|
||||
import sys
|
||||
|
||||
sys.stdout.write('\033[1;31m请不要调用此目录下的任何文件! 此目录文件仅作测试用!\033[0m')
|
||||
sys.exit(-1)
|
||||
sys.stdout.write('\033[0;34mMySQL数据库支持在./bin/tests/MySQLSET/MySQL.py\n\033[0m'
|
||||
'\033[0;34m由原本pymysql库更换为了SQLAlchemy库, 已经将增删查改四个功能写好\n\033[0m'
|
||||
'\033[0;34m由于一些原因无法使用\033[0m'
|
||||
'\033[0;34m数据库导入文件请前往 https://themedatabase.vercel.app/source/sql 下载\n\033[0m'
|
||||
'\033[0;34msql文件来源: 使用SQLiteStudio直接导出为.sql文件\n\033[0m'
|
||||
'\033[0;34m使用 source db.sql 导入数据库时出现了一些错误:\n\033[0m'
|
||||
'\033[0;34m有几个base64编码的值无法插入到表内\n\033[0m'
|
||||
'\033[0;34m如果你有能力贡献代码请毫不犹豫地提交 pull request 吧!\n\n\033[0m')
|
||||
sys.stdout.write('\033[1;31m----------以下为报错信息----------\033[0m')
|
||||
sys.stdout.write(
|
||||
r"""
|
||||
Traceback (most recent call last):
|
||||
File "C:\Users\Tapso\AppData\Local\Programs\Python\Python310\lib\site-packages\mysql\connector\connection_cext.py", line 523, in cmd_query
|
||||
self._cmysql.query(query,
|
||||
_mysql_connector.MySQLInterfaceError: Table 'data.reqcount' doesn't exist
|
||||
|
||||
During handling of the above exception, another exception occurred:
|
||||
|
||||
Traceback (most recent call last):
|
||||
File "C:\Users\Tapso\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\engine\base.py", line 1802, in _execute_context
|
||||
self.dialect.do_execute(
|
||||
File "C:\Users\Tapso\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\engine\default.py", line 732, in do_execute
|
||||
cursor.execute(statement, parameters)
|
||||
File "C:\Users\Tapso\AppData\Local\Programs\Python\Python310\lib\site-packages\mysql\connector\cursor_cext.py", line 269, in execute
|
||||
result = self._cnx.cmd_query(stmt, raw=self._raw,
|
||||
File "C:\Users\Tapso\AppData\Local\Programs\Python\Python310\lib\site-packages\mysql\connector\connection_cext.py", line 528, in cmd_query
|
||||
raise errors.get_mysql_exception(exc.errno, msg=exc.msg,
|
||||
mysql.connector.errors.ProgrammingError: 1146 (42S02): Table 'data.reqcount' doesn't exist
|
||||
|
||||
The above exception was the direct cause of the following exception:
|
||||
|
||||
Traceback (most recent call last):
|
||||
File "C:\Users\Tapso\PycharmProjects\RequestCounter\bin\tests\MySQL.py", line 133, in <module>
|
||||
print(mysql.fetch('AAA'))
|
||||
File "C:\Users\Tapso\PycharmProjects\RequestCounter\bin\tests\MySQL.py", line 102, in fetch
|
||||
data = self.session.query(ReqCount).filter(ReqCount.name == name).all()
|
||||
File "C:\Users\Tapso\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\orm\query.py", line 2759, in all
|
||||
return self._iter().all()
|
||||
File "C:\Users\Tapso\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\orm\query.py", line 2894, in _iter
|
||||
result = self.session.execute(
|
||||
File "C:\Users\Tapso\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\orm\session.py", line 1692, in execute
|
||||
result = conn._execute_20(statement, params or {}, execution_options)
|
||||
File "C:\Users\Tapso\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\engine\base.py", line 1614, in _execute_20
|
||||
return meth(self, args_10style, kwargs_10style, execution_options)
|
||||
File "C:\Users\Tapso\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\sql\elements.py", line 325, in _execute_on_connection
|
||||
return connection._execute_clauseelement(
|
||||
File "C:\Users\Tapso\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\engine\base.py", line 1481, in _execute_clauseelement
|
||||
ret = self._execute_context(
|
||||
File "C:\Users\Tapso\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\engine\base.py", line 1845, in _execute_context
|
||||
self._handle_dbapi_exception(
|
||||
File "C:\Users\Tapso\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\engine\base.py", line 2026, in _handle_dbapi_exception
|
||||
util.raise_(
|
||||
File "C:\Users\Tapso\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\util\compat.py", line 207, in raise_
|
||||
raise exception
|
||||
File "C:\Users\Tapso\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\engine\base.py", line 1802, in _execute_context
|
||||
self.dialect.do_execute(
|
||||
File "C:\Users\Tapso\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\engine\default.py", line 732, in do_execute
|
||||
cursor.execute(statement, parameters)
|
||||
File "C:\Users\Tapso\AppData\Local\Programs\Python\Python310\lib\site-packages\mysql\connector\cursor_cext.py", line 269, in execute
|
||||
result = self._cnx.cmd_query(stmt, raw=self._raw,
|
||||
File "C:\Users\Tapso\AppData\Local\Programs\Python\Python310\lib\site-packages\mysql\connector\connection_cext.py", line 528, in cmd_query
|
||||
raise errors.get_mysql_exception(exc.errno, msg=exc.msg,
|
||||
sqlalchemy.exc.ProgrammingError: (mysql.connector.errors.ProgrammingError) 1146 (42S02): Table 'data.reqcount' doesn't exist
|
||||
[SQL: SELECT reqcount.name AS reqcount_name, reqcount.times AS reqcount_times
|
||||
FROM reqcount
|
||||
WHERE reqcount.name = %(name_1)s]
|
||||
[parameters: {'name_1': 'AAA'}]
|
||||
(Background on this error at: https://sqlalche.me/e/14/f405)"""
|
||||
)
|
||||
|
||||
input()
|
||||
@@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env python3
|
||||
# -- coding:utf-8 --
|
||||
# @Author: markushammered@gmail.com
|
||||
# @Development Tool: PyCharm
|
||||
# @Create Time: 2022/2/26
|
||||
# @File Name: fetchtable.py
|
||||
|
||||
|
||||
import sqlite3
|
||||
|
||||
conn = sqlite3.connect('../db/data.db')
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('select * from sqlite_master where type="table"')
|
||||
tables = []
|
||||
for i in cursor.fetchall():
|
||||
tables.append(i[1])
|
||||
|
||||
print(tables)
|
||||
Reference in New Issue
Block a user