我正在尝试使用Python(3.4)MySQL模块在本地MySQL数据库上查询以下代码:
class databases():
def externaldatabase(self):
try:
c = mysql.connector.connect(host="127.0.0.1", user="user",
password="password", database="database")
if c.is_connected():
c.autocommit = True
return(c)
except:
return(None)
d = databases().externaldatabase()
c = d.cursor()
r = c.execute('''select * from tbl_wiki''')
print(r)
> Returns: None
据我所知,连接成功,数据库由多行组成,但查询总是返回none类型。
MySQL执行函数的哪些实例返回None?
答案 0 :(得分:10)
查询执行没有返回值。
您需要遵循的模式是:
cursor creation;
cursor, execute query;
cursor, *fetch rows*;
或者在python中:
c = d.cursor()
c.execute(query) # selected rows stored in cursor memory
rows = c.fetchall() # get all selected rows, as Barmar mentioned
for r in rows:
print(r)
此外,一些数据库模块允许您使用for ... in模式迭代游标,但是对于mysql进行三重检查。