cursor.execute(sqlString)
当我直接在mysql工作台上运行它时,我正在调用运行良好的sql。但是,当我运行代码时,没有得到结果集。
我有与链接中所述完全相同的问题症状,并且尝试了链接的解决方案,但事实证明我没有相同的问题。
返回时,我的_stored_results []为空。
我在try / except块中使用该代码,我还有另一个python程序,该程序使用相同的代码将csv加载到mymySQL数据库中,并且很好用。
如果有任何区别,我遇到问题的代码位于@ app.route中。
我的代码如下所示:
def functionName() :
try:
import mysql.connector
from mysql.connector import errorcode
cnx = mysql.connector.connect(user=init["dbDetails"][0], password=init["dbDetails"][1], host=init["dbDetails"][2], database=init["dbDetails"][3])
cur = cnx.cursor()
cur.close() #I deffo don't need the two lines below but they were added for a sanity check, just to make sure the cur was not being read from any other code.
cur = cnx.cursor() # and this one obviously
sqlString = 'CALL `schemaName`.`getProcedureName_sp`(1, 1, 0)'
cur.execute(sqlString, multi=True) # tried it here without the multi=True and got the msg telling me to use it.
getSomeDetails = cur.fetchall()
cnx.commit() # probably don't need to commit here I am just reading from the dB but I am trying anything as I have no idea what my issue might be.
return render_template('success.html')
except Exception as e:
return render_template('error.html', error = str(e))
finally:
cur.close()
cnx.close()
我很困惑,因为我在多个地方都使用相同的代码。
因此,我以此为头撞墙,当我无法到达任何地方时,我决定离开它继续前进,然后重新思考。好吧...行得通,有点。
因此,我没有找到解决方案,但是找到了解决该问题的方法,甚至可能对代码中实际发生的情况有所了解。
我认为,由于fetchall()方法是导致我麻烦的原因,因此我应该尝试绕过它。
我在调用fetchall()方法之前探测了cursor(cur),发现cur._rows包含SQL调用的结果。
所以我换了线
getSomeDetails = cur.fetchall()
至
if len(cur._rows) > 0 :
getSomeDetails = list(cur._rows[0]) #I only ever expect one result in this query
#getSomeDetails should now have the row I am looking for
getSomeDetails[0] #gets me the field I am looking for
现在我的变量getSomeDetails具有过程调用的返回值
但是,它们的格式不是我应该从fetchall()函数中获取的格式,因此,我必须进行一些处理,必须确保已取回一些值,并且我注意到这些值是在元组
我在运行两个不同操作系统和两个不同版本的python的两个不同的机器上遇到了这个问题(带有Python 2.7的Windows 7和带有Python 3的Windows 10)这两个代码都是不同的,因此事实上我正在使用两个不同的MySQL库,因此在这两种情况下,该修复程序的实际代码略有不同,但是现在我两种情况下都是将数据库中的数据放入Python中的变量中,所以这很酷。
但是,这是一个hack,我知道,我宁愿使用适当的函数cur.fetchall(),所以我仍然对这里可能出问题的建议持开放态度。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句