Error connecting to mysqldb
Question: how to solve the following error on the below code. The application is a
python phones application that retrieves names and phone numbers from a
mysqldb database. The line that gives me the error is phoneList =
c.fetchrows(). I really appreciate your assistance.

# AttributeError: 'NoneType' object has no attribute '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()
    


Answer: So the main reason is that `db.query` doesn't return anything - you have to
use either `db.store_result()` or `db.use_result()` (see the full docs
[here](http://mysql-python.sourceforge.net/MySQLdb.html)). However that is
using `_mysql`\- we'll use `MySQLdb` in this example:

    
    
    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)
    

Hope that helps!

