我很难理解为什么在我的函数中第二次使用数据库后我得到了 1 返回。我需要访问两个不同的表来获取我需要的信息。
class getSubdivs:
def __init__(self):
self.con = mdb.connect('localhost', 'root', 'root', 'shannon')
self.cur = self.con.cursor()
def openConnection(self):
self.con = mdb.connect('localhost', 'root', 'root', 'shannon')
self.cur = self.con.cursor()
def closeConnection(self):
self.con.commit()
self.cur.close()
def getAllSubdivsforTable(self):
self.cur.execute('SELECT * FROM SUBDIVS')
info = self.cur.fetchall()
print info
dict = {} #dictionary object that creates the RR collection of the RR resources
for x in info:
littleDict = {} #makes a little dictionary object that will fill the other dictionary object
ID = x[0]
name = x[3]
que = ('SELECT RailroadName FROM RAILROADS WHERE SCAC = %s')
company = self.cur.execute(que, ID)
print 'This is the company'
print company
book['name'] = name
book['company'] = company
book['link'] = None #place holder for now, will be used later
data = json.dumps(book)
return data
有没有办法可以清除 mySQL 中的游标,以便我可以从第二个表中获取信息?
我的代码返回这个 这是公司 1 {“公司”:1,“链接”:null,“名称”:“微型卡车导轨”}
即使数据库存储了“铁路公司”。
最佳答案
您是否尝试过使用第二个光标而不是重复使用它?
编辑,您没有在第二个查询上调用 fetchall!!
关于python - 在同一个函数中使用 MySQL 数据库两次返回 1 而不是实际字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30926855/