问题遇到的现象和发生背景
问题
在使用Python操作MySQL进行查询时,使用fetchall提取数据时报错了,这个是什么问题呢,我sql指定的是自由输入,可以进行这样的操作吗
问题相关代码,请勿粘贴截图
# -- encoding:utf-8 --
import pymysql
import pymssql
def SQLServer():
pass
def MySQL():
host_input = input('请输入主机名')
user_input = input('请输入用户名')
pswd_input = input('请输入密码')
dbs_input = input('选择连接的数据库')
conn = pymysql.connect(host=host_input, user=user_input, password=pswd_input, database=dbs_input)
cur = conn.cursor()
while True:
try:
sql = input('请输入需要执行的操作')
return_sql = cur.execute(sql)
return_sql.fetchall()
print('影响的行数', return_sql)
except Exception as e:
print(e)
if __name__ == '__main__':
choice = input('请选择连接的数据库类型:1.SQLserver 2.MySQL')
if choice == '2':
MySQL()
elif choice == '1':
SQLServer()
else:
print('请输入正确的方法')
运行结果及报错内容
有问题的代码
while True:
try:
sql = input('请输入需要执行的操作')
return_sql = cur.execute(sql)
return_sql.fetchall()
print('影响的行数', return_sql)
except Exception as e:
print(e)
我想要达到的结果
能使用正常的fetchall()遍历执行后的数据