为什么MySQL执行时返回None? [英] Why would MySQL execute return None?

查看:1044
本文介绍了为什么MySQL执行时返回None?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python的(3.4)MySQL模块通过以下代码在本地MySQL数据库上进行查询:

I am trying to query on a local MySQL database using Python's (3.4) MySQL module with the following code:

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类型.

As far as I can tell, the connection is successful, the database is composed of several rows but the query always returns the none type.

MySQL执行函数在哪些实例中返回None?

What instances does the MySQL execute function return None?

推荐答案

查询执行没有返回值.

您需要遵循的模式是:

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进行三重检查.

Also some db modules allow you to iterate over the cursor using the for...in pattern, but triple-check that regarding mysql.

这篇关于为什么MySQL执行时返回None?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆