# 连接数据库 db = pymysql.connect( host='localhost', user='root', password='240034', charset='utf8', database='class' ) # 创建游标 cursor = db.cursor() # 查询sql sql = 'show tables' # 把sql语句交给游标执行 cursor.execute(sql) for list,contentss in zip(content,contents): if i == 0: # 创建数据表 sql = f'create table use({list} varchar(800))' cursor.execute(sql) for content in contentss: # 插入数据 # print(content) sql = 'insert ignore into use values(%s)' cursor.execute(sql,(content)) db.commit() i = i + 1 else: sql = 'alter table use add `%s` varchar(800)'%list cursor.execute(sql) for content in contentss: # 插入数据 # print(content) sql = f'insert ignore into use({list}) values(%s)' cursor.execute(sql,(content)) db.commit()
时间: 2024-02-10 10:15:03 浏览: 98
根据你提供的代码,我发现在执行创建数据表的语句时出现了问题。你在创建表时表名前添加了 use 关键字,这是不正确的,因为 use 是MySQL的关键字,不能作为表名使用。正确的做法应该是将 use 放在CREATE TABLE后面的括号中,作为表的名称。例如,你的创建表的语句应该是这样的:
```
sql = f'create table `use` ({list} varchar(800))'
```
另外,你在插入数据时也需要注意语法错误,插入数据时需要使用 INSERT INTO 语句,而不是 INSERT IGNORE INTO 语句。正确的插入数据的语句应该是这样的:
```
sql = f'insert into `use` (`{list}`) values (%s)'
```
带有参数的语句中表名和字段名需要用反引号 `` 包围起来,而不是用括号 () 包围。
相关问题
import pymysql class MysqlDBUtil(): # 字典输出 def __init__(self, host, user, password, database, charset): # host = "localhost" # 指定访问的服务器,本地服务器指定“localhost”,远程服务器指定服务器ip地址 # port = 3306 # 指定端口号,范围在0-65535 # user = "root" # 用户名 # password = "123456" # 密码 # database = "sys" # 数据库的名字 # charset = "utf8" # 数据库的编码方式 # 建立连接 self.connect = pymysql.Connect(host=host, port=3306, user=user, passwd=password, db=database, charset=charset) # 数据库的查询 def query(self , sql): cursor = None res = None try: cursor = self.connect.cursor() cursor.execute(sql) res = cursor.fetchall() except Exception as e: print(str(e)) finally: cursor.close() return res # 数据库的增删改 def uid(self , sql): cursor = None try: cursor = self.connect.cursor() cursor.execute(sql) self.connect.commit() except Exception as e: print(str(e)) self.connect.rollback() finally: cursor.close() #数据库的关闭 def sql_close(self): self.connect.close() import pytest import allure import logging from utils.mysql_database_connection_utils import MysqlDBUtil import utils.mysql_database_connection_utils class TestChaxun(): def mysql_connect(self): self.conn = MysqlDBUtil("localhost", "root", "123456", "sys", "utf8") def teardown_class(self): self.conn.sql_close() def test1(self): data = self.conn.query("SELECT * from stu WHERE name = '张三' ") print(data)
这段代码中的问题可能是在TestChaxun类中的mysql_connect方法中,没有使用self关键字来定义conn属性。应该将mysql_connect方法中的 "self.conn" 更改为 "self.db",因为在类的初始化方法中已经定义了 "self.connect"。修改后的代码如下:
```
class TestChaxun():
def mysql_connect(self):
self.db = MysqlDBUtil("localhost", "root", "123456", "sys", "utf8")
def teardown_class(self):
self.db.sql_close()
def test1(self):
data = self.db.query("SELECT * from stu WHERE name = '张三' ")
print(data)
```
另外,你需要确保在运行测试之前已经安装了pytest和allure模块,并且将utils.mysql_database_connection_utils模块添加到PYTHONPATH中,以便测试代码能够正确地导入MysqlDBUtil类。
请你逐行解释一下以下代码class StudentView: def __init__(self, parent_window, student_id): parent_window.destroy() # 销毁主界面 self.window = tk.Tk() # 初始框的声明 self.window.title('学生信息查看') self.window.geometry('300x450') # 这里的乘是小x label = tk.Label(self.window, text='学生信息查看', bg='pink', font=('Verdana', 20), width=30, height=2) label.pack(pady=20) self.id = '学号:' + '' self.name = '姓名:' + '' self.gender = '性别:' + '' self.age = '年龄:' + '' # 打开数据库连接 db = pymysql.connect(host="localhost", port=3306, user="root", password="123456", database="student", charset="utf8") # db = pymysql.connect("localhost", "root", "123456", "student") # 打开数据库连接 cursor = db.cursor() # 使用cursor()方法获取操作游标 sql = "SELECT * FROM student_k WHERE id = '%s'" % (student_id) # SQL 查询语句 try: # 执行SQL语句 cursor.execute(sql) # 获取所有记录列表 results = cursor.fetchall() for row in results: self.id = '学号:' + row[0] self.name = '姓名:' + row[1] self.gender = '性别:' + row[2] self.age = '年龄:' + row[3] except: print("Error: unable to fetch data") db.close() # 关闭数据库连接 Label(self.window, text=self.id, font=('Verdana', 18)).pack(pady=5) Label(self.window, text=self.name, font=('Verdana', 18)).pack(pady=5) Label(self.window, text=self.gender, font=('Verdana', 18)).pack(pady=5) Label(self.window, text=self.age, font=('Verdana', 18)).pack(pady=5) Button(self.window, text="返回首页", width=8, font=tkFont.Font(size=16), command=self.back).pack(pady=25) self.window.protocol("WM_DELETE_WINDOW", self.back) # 捕捉右上角关闭点击 self.window.mainloop() # 进入消息循环 def back(self): StartPage(self.window) # 显示主窗口 销毁本窗口
这段代码定义了一个名为 StudentView 的类,该类用于展示学生信息界面。在该类的初始化方法 __init__ 中,首先销毁了父窗口,然后创建了一个名为 window 的 Tkinter 窗口。接着设置了窗口的标题和大小,并创建了一个 Label 用于显示标题。之后定义了四个变量 id、name、gender、age,并打开了一个 MySQL 数据库的连接。接着使用 cursor() 方法获取操作游标,并定义了一个 SQL 查询语句。然后执行 SQL 语句,获取查询结果,并将查询结果赋值给 id、name、gender、age 四个变量。最后创建了四个 Label 用于显示学生信息,并创建了一个“返回首页”的 Button,设置了该 Button 的字体大小和点击事件。最后设置了窗口右上角关闭按钮的点击事件,并进入了 Tkinter 的消息循环。back 方法用于返回到主窗口并销毁当前窗口。
阅读全文
相关推荐

















