【问题标题】:Error connecting to mysqldb连接到 mysqldb 时出错
【发布时间】:2012-08-17 22:50:48
【问题描述】:

如何解决以下代码中的以下错误。该应用程序是一个 python 电话应用程序,它从 mysqldb 数据库中检索姓名和电话号码。给我错误的行是 phoneList = c.fetchrows()。非常感谢您的帮助。

AttributeError: 'NoneType' 对象没有属性 'fetchrows'

#connecting to database

from Tkinter import *

import MySQLdb

db = MySQLdb.connect(host = "localhost", user = "xxxxxxx", passwd = "xxxxxxxx", db ="test")

# which item is selected

def whichSelected () :

    print "At %s" % (select.curselection())

    return int(select.curselection()[0])

# doing sql query
def dosql (cmd) :

    print cmd
    c = db.query(cmd)
    setSelect ()

# the generation of new id numbers as new rows are inserted.
def addEntry () :

    c = db.query("select max(id)+1 from phones")
    id = c.fetchdict()[0].values()[0] # digs deep to get next id
    dosql("insert into phones values (%d,'%s','%s')" % (id,nameVar.get(), phoneVar.get()))
#updating the entries

def updateEntry() :

    id = phoneList[whichSelected()][0]
    dosql("update phones set name='%s', phone='%s' where id=%d" %
      (nameVar.get(), phoneVar.get(), id))

# deleting the entries

def deleteEntry() :

    id = phoneList[whichSelected()][0]
    dosql("delete from phones where id=%d" % id)
# loading the entries 

def loadEntry () :

    id, name, phone = phoneList[whichSelected()]
    nameVar.set(name)
    phoneVar.set(phone)

# building my windows
def makeWindow () :


    global nameVar, phoneVar, select
    win = Tk()

    frame1 = Frame(win)
    frame1.pack()
    . 
    . 
    .
    .
# the function "setSelect" which fills in our list control. Here, instead of importing the phone list, we simply use fetchrows to get the same list of lists.

def setSelect () :


    global phoneList
    c = db.query("select id,name,phone from phones order by name")
    phoneList = c.fetchrows()
    select.delete(0,END)
    for id,name,phone in phoneList :
    select.insert (END, name)

win = makeWindow()

setSelect()

win.mainloop()

【问题讨论】:

    标签: mysql-python


    【解决方案1】:

    所以主要原因是 db.query 不返回任何内容 - 您必须使用 db.store_result()db.use_result()(请参阅完整文档 here)。但是,这是使用 _mysql- 在此示例中我们将使用 MySQLdb

    def setSelect():
        global phoneList
        # You have your connection db set up, so now we make a cursor
        c = db.cursor()
    
        # Now execute your query using the cursor 'execute' method
        c.execute("select id,name,phone from phones order by name")
    
        # Now we pull the results from the query and store them in phoneList
        phoneList = c.fetchall()
    
        # And I know nothing about Tkinter, so hopefully the rest will work :)
        select.delete(0,END)
    
        # phoneList will represent each row as a tuple, so make sure to verify this part
        for id,name,phone in phoneList :
          select.insert (END, name)
    

    希望有帮助!

    【讨论】:

    • 很好,现在可以使用了。非常感谢。如果我再打扰你,你能看看 addEntry 方法吗?它不是向数据库中添加和项目。其他方法(删除、加载和更新)运行良好。 addEntry 给出的错误是: 1. AttributeError: 'NoneType' object has no attribute 'fetchdict'
    • 很高兴听到!至于那些错误,听起来和以前一样。尝试使用游标(如示例中所示),execute'在游标上执行查询,然后将结果分配给变量(fetchall() 将全部返回)。
    猜你喜欢
    • 2012-11-04
    • 2022-10-19
    • 2013-09-07
    • 2018-05-23
    • 2014-01-01
    • 2016-06-08
    • 2012-02-05
    • 2011-06-30
    • 2015-10-09
    相关资源
    最近更新 更多