-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMotionMySql.py
169 lines (144 loc) · 5.63 KB
/
MotionMySql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import pymysql
class join:
def join(self,host,user,password,library_name):
self.conn = pymysql.connect(
host=host,
user=user,
password=password,
database=library_name,
cursorclass=pymysql.cursors.DictCursor,
)
self.cursor = self.conn.cursor()
class GetColumn:
def GetColumn(self, cursor, conn, table_name):
query = f"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'abc' AND TABLE_NAME = '{table_name}';"
cursor.execute(query)
results = cursor.fetchall()
print(results)
column_names = [item["COLUMN_NAME"] for item in results]
print(column_names)
# 创建一个字典来存储列名和对应的值
column_values_dict = {}
# 提示用户输入每列的值
for column_name in column_names:
while True:
value = input(f"请输入{column_name}的值:")
if value:
column_values_dict[column_name] = value
break
else:
print("输入不能为空,请重新输入")
# 检查输入值的数量是否与列的数量匹配
if len(column_values_dict) == len(column_names):
# 构建参数化的INSERT查询
placeholders = ", ".join(["%s"] * len(column_names))
insert_query = f"INSERT INTO {table_name} ({', '.join(column_names)}) VALUES ({placeholders})"
print(insert_query)
# 执行参数化查询
# cursor.execute(insert_query, list(column_values_dict.values()))
try:
cursor.execute(insert_query, list(column_values_dict.values()))
except Exception as e:
print(f"输入有误: {e}")
conn.commit()
else:
print("输入的值的数量与列的数量不匹配")
class Inquire:
def run(self, cursor, conn, table_name):
try:
# 获取用户输入
column_name = input("请输入要查询的列名: ")
search_term = input("请输入要搜索的关键词: ")
# 将空格替换为`
column_name = column_name.replace(" ", "`")
search_term = search_term.replace(" ", "`")
# 构建查询语句
query = f"SELECT * FROM {table_name} WHERE `{column_name}` LIKE '%{search_term}%'"
n = input("是否确认操作无误(yes/no)")
# 执行查询
if n == "yes":
print("确认操作")
cursor.execute(query)
results = cursor.fetchall()
# 输出查询结果
if results:
for result in results:
print(result)
else:
print("没有找到相关数据!")
else:
print("取消操作")
except pymysql.Error as e:
print(f"查询错误: {e}")
except Exception as e:
print(f"输入有误: {e}")
class delt:
def deltf(self, cursor, conn, table_name):
# 获取用户输入
column_name = input("请输入要删除的列名: ")
search_term = input("请输入要删除的关键词: ")
query = (
f"SELECT * FROM {table_name} WHERE `{column_name}` LIKE '%{search_term}%'"
)
# 执行查询
cursor.execute(query)
results = cursor.fetchall()
# 输出查询结果
if results:
column_name = column_name.replace(" ", "`")
search_term = search_term.replace(" ", "`")
# 构建查询语句
sql = f"DELETE FROM {table_name} WHERE `{column_name}` LIKE '%{search_term}%';"
n = input("是否确认操作无误(yes/no)")
# 执行查询
if n == "yes":
try:
# 执行SQL语句
cursor.execute(sql)
# 提交到数据库执行
conn.commit()
print("成功")
except Exception as e:
# 发生错误时回滚
conn.rollback()
print(f"执行SQL出错: {e}")
else:
print("取消操作")
else:
print("没有找到相关数据!")
class Renewal:
def Renewalf(self, cursor, conn, table_name):
# 获取用户输入
column_name = input("输入要更改的列名") # 用实际列名替换
search_term = input("输入要查询的关键字") # 用实际搜索词替换
new_value = input("输入要更改的值") # 用实际的新值替换
query = (
f"SELECT * FROM {table_name} WHERE `{column_name}` LIKE '%{search_term}%'"
)
# 执行查询
cursor.execute(query)
results = cursor.fetchall()
# 输出查询结果
if results:
column_name = column_name.replace(" ", "`")
search_term = search_term.replace(" ", "`")
# 构建查询语句
rwl = f"UPDATE {table_name} SET {column_name} = '{new_value}' WHERE {column_name} LIKE '%{search_term}%';"
n = input("是否确认操作无误(yes/no)")
# 执行查询
if n == "yes":
try:
# 执行SQL语句
cursor.execute(rwl)
# 提交到数据库执行
conn.commit()
print("成功")
except Exception as e:
# 发生错误时回滚
self.conn.rollback()
print(f"执行SQL出错: {e}")
else:
print("没有找到相关数据!")
def close(cursor, conn):
cursor.close() # 关闭游标
conn.close() # 关闭连接