pythonmysql连接器查询返回none
我的mysql连接器有问题。我确实找了好一阵子,但什么也没找到 如果我执行第一个刚包含查询的文件,它将按预期工作。但是,如果我尝试创建一个db类并将查询放入其中,它将不返回任何结果。我已经取出了位置变量,它是一样的。无论我尝试查询什么,它都不会返回任何结果。我甚至试着做了一个“展示桌子”的游戏,结果一无所获 此外,我已经在调试器中运行了它,并查看了cursor对象以及mysql常规日志。这个查询是正确的,在我看来一切都是正确的 这是我第一次尝试python,我希望解决方案是一些简单的新手错误 有效的查询: test.pypythonmysql连接器查询返回none,python,mysql-python,Python,Mysql Python,我的mysql连接器有问题。我确实找了好一阵子,但什么也没找到 如果我执行第一个刚包含查询的文件,它将按预期工作。但是,如果我尝试创建一个db类并将查询放入其中,它将不返回任何结果。我已经取出了位置变量,它是一样的。无论我尝试查询什么,它都不会返回任何结果。我甚至试着做了一个“展示桌子”的游戏,结果一无所获 此外,我已经在调试器中运行了它,并查看了cursor对象以及mysql常规日志。这个查询是正确的,在我看来一切都是正确的 这是我第一次尝试python,我希望解决方案是一些简单的新手错误 有
import mysql.connector
_config = {
'user': 'user',
'password': 'password',
'host': '127.0.0.1',
'database': 'testdb',
'raise_on_warnings': True,
}
cnx = mysql.connector.connect(**_config)
cursor = cnx.cursor()
query = ("SELECT * FROM testtbl WHERE location=%s")
location='HERE'
cursor.execute(query, (location, ))
print("--- " + str(cursor) + " ----")
for (stuff) in cursor:
print("stuff: '" + stuff[0] + "', more stuff: '" + stuff[1] + "'")
cursor.close()
cnx.close()
import mysql.connector
class SomeDB(object):
def __init__(self):
_config = {
'user': 'user',
'password': 'password',
'host': '127.0.0.1',
'database': 'testdb',
'raise_on_warnings': True,
}
self.conn = mysql.connector.connect(**_config)
self.cur = self.conn.cursor()
def get_stuff(self):
query = ("SELECT * FROM testtbl WHERE location=%s")
location="HERE"
result = self.cur.execute(query, (location, ))
return result
def __del__(self):
self.conn.close()
import somedb
db = somedb.SomeDB()
cursor = db.get_stuff()
print("--- " + str(cursor) + " ----")
for (stuff) in cursor:
print("stuff: '" + stuff[0] + "', more stuff: '" + stuff[1] + "'")
那些不起作用的:somedb.py
import mysql.connector
_config = {
'user': 'user',
'password': 'password',
'host': '127.0.0.1',
'database': 'testdb',
'raise_on_warnings': True,
}
cnx = mysql.connector.connect(**_config)
cursor = cnx.cursor()
query = ("SELECT * FROM testtbl WHERE location=%s")
location='HERE'
cursor.execute(query, (location, ))
print("--- " + str(cursor) + " ----")
for (stuff) in cursor:
print("stuff: '" + stuff[0] + "', more stuff: '" + stuff[1] + "'")
cursor.close()
cnx.close()
import mysql.connector
class SomeDB(object):
def __init__(self):
_config = {
'user': 'user',
'password': 'password',
'host': '127.0.0.1',
'database': 'testdb',
'raise_on_warnings': True,
}
self.conn = mysql.connector.connect(**_config)
self.cur = self.conn.cursor()
def get_stuff(self):
query = ("SELECT * FROM testtbl WHERE location=%s")
location="HERE"
result = self.cur.execute(query, (location, ))
return result
def __del__(self):
self.conn.close()
import somedb
db = somedb.SomeDB()
cursor = db.get_stuff()
print("--- " + str(cursor) + " ----")
for (stuff) in cursor:
print("stuff: '" + stuff[0] + "', more stuff: '" + stuff[1] + "'")
根据Alu的建议,我将get_stuff方法更改为:
def get_nodes(self):
query = ("SELECT * FROM testtbl WHERE location=%s")
location="HERE"
cursor = self.cur.execute(query, (location, ))
list = []
for (thing) in cursor:
list.append(([thing[0],thing[1]]))
return list
test2.py
import mysql.connector
_config = {
'user': 'user',
'password': 'password',
'host': '127.0.0.1',
'database': 'testdb',
'raise_on_warnings': True,
}
cnx = mysql.connector.connect(**_config)
cursor = cnx.cursor()
query = ("SELECT * FROM testtbl WHERE location=%s")
location='HERE'
cursor.execute(query, (location, ))
print("--- " + str(cursor) + " ----")
for (stuff) in cursor:
print("stuff: '" + stuff[0] + "', more stuff: '" + stuff[1] + "'")
cursor.close()
cnx.close()
import mysql.connector
class SomeDB(object):
def __init__(self):
_config = {
'user': 'user',
'password': 'password',
'host': '127.0.0.1',
'database': 'testdb',
'raise_on_warnings': True,
}
self.conn = mysql.connector.connect(**_config)
self.cur = self.conn.cursor()
def get_stuff(self):
query = ("SELECT * FROM testtbl WHERE location=%s")
location="HERE"
result = self.cur.execute(query, (location, ))
return result
def __del__(self):
self.conn.close()
import somedb
db = somedb.SomeDB()
cursor = db.get_stuff()
print("--- " + str(cursor) + " ----")
for (stuff) in cursor:
print("stuff: '" + stuff[0] + "', more stuff: '" + stuff[1] + "'")
更新
好的,我不能让它工作。我已经用一个调试器完成了这段代码,撇开类抽象不谈,其他一切似乎都是一样的。因此,让我进一步完善我的问题:这是否适用于任何mysql驱动程序?这就是我想做的:
更新2
我找到了
result = self.cur.execute(query, (location, ))
return result
需要,简单地说:
self.cur.execute(query, (location, ))
return self.cur
据我所知,您必须在每次交易后提交连接 你可以试着用这个作为例子。这是我的数据库课。
我试过了,但它抛出了一个错误,光标有未读数据。文档中说commit只适用于更改数据的操作:该方法向MySQL服务器发送commit语句,提交当前事务。由于默认情况下Connector/Python不会自动提交,因此在每次修改使用transactional storage engines.Jap的表的数据的事务之后调用此方法非常重要,您必须读取光标。行结果=光标。。。不工作。所以我不能简单地将光标分配给变量并返回它?我怎么能做到呢?这也没用。当它到达循环时,它会抱怨您不能在非类型上迭代。因此它仍然不返回任何数据在您进行提交之前,从游标读取所有结果(只需运行
cur.featchall()
,您甚至不必分配它),然后才进行提交。