python - 在同一个函数中使用 MySQL 数据库两次返回 1 而不是实际字符串

标签 python mysql database

我很难理解为什么在我的函数中第二次使用数据库后我得到了 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/

相关文章:

python - 如何从 PyAudio 中的连续声音数据中删除爆音

python - IntelliJ Python 3 检查 "Expected a dictionary, got a dict"是否是带有 **kwargs 的 super 的误报?

mysql - 将数据复制到另一个数据库后删除数据

php - utf8网站和latin1数据库表字段

sql - 需要评论策略

Python 抽象类 - 如何阻止实例化?

mysql - MySQL 创建函数索引

android - 存储内容类型和子内容的数据库架构(ANDROID _ SQLITE :)

php - jQuery 可排序网格到 MySQL 数据库 PHP

python - GeoJson 图层在 Python Folium map 上不可见