我正在尝试打开一个游标。尽管与mysql数据库连接,但仍无法正常工作

时间:2019-07-19 12:28:50

标签: mysql python-3.x

我想使用游标执行mysql查询,但是无法执行。错误显示: AttributeError:'NoneType'对象没有属性'execute'

我的代码中总共有3个游标(我无法仅使用1个游标)并且它们都已连接(我通过调试对其进行了检查)

我已经通过了此线程,它无法正常工作Python: MySQL connection is open, but can't create cursor

从fpdf导入FPDF,HTMLMixin 导入mysql.connector

#from mysql.connector import error
class HTML2PDF(FPDF, HTMLMixin):
    pass


conn = mysql.connector.connect(host='localhost',
                                       database='swd',
                                       user='root',
                                       password='param2000')
conn1 = mysql.connector.connect(host='localhost',
                                       database='swd',
                                       user='root',
                                       password='param2000')
conn2 = mysql.connector.connect(host='localhost',
                                       database='swd',
                                       user='root',
                                       password='param2000')
#all 3 are successfully connected
cursor = conn.cursor()
cursor.execute("SELECT * from 14batch")
row=cursor.fetchone()
#cursor works perfectly

html='''blah blah blah'''
cursor1=conn1.connect()
cursor1.execute("SELECT * from med_ins_14batch where uid='row[0]'")
#it throws me an error in the above line
row1 = cursor1.fetchone()

AttributeError:“ NoneType”对象没有属性“ execute”

1 个答案:

答案 0 :(得分:0)

查看代码中的差异。

首先,正在使用光标创建cursor。 第二,cursor1的创建方式不同。

将失败的行集更改为:

html='''blah blah blah'''
cursor1=conn1.cursor()
cursor1.execute("SELECT * from med_ins_14batch where uid='row[0]'")
#this should work now
row1 = cursor1.fetchone()