I am trying to query on a local MySQL database using Python's (3.4) MySQL module with the following code:
class databases():
def externaldatabase(self):
try:
c = mysql.connector.connect(host="127.0.0.1", user="user",
password="password", database="database")
if c.is_connected():
c.autocommit = True
return(c)
except:
return(None)
d = databases().externaldatabase()
c = d.cursor()
r = c.execute('''select * from tbl_wiki''')
print(r)
> Returns: None
As far as I can tell, the connection is successful, the database is composed of several rows but the query always returns the none type.
What instances does the MySQL execute function return None?
c.execute()doesn't return anything unless you use themulti=Trueoption. Then it returns an iterator that lets you step through the results of each query in the multiple queries. - Barmarc.fetchone()to get one row,c.fetchall()to get all the results. - BarmarexecutereturnsNone. (The DB-API spec just says "Return values are undefined".) The point ofexecuteis to modify the cursor so you can extract rows from it, and in Python, methods that modify an object normally returnNone. Some libraries do have return values forexecutein certain cases for shortcuts or additional functionality, but if you're not using any of those cases, you shouldn't expect anything meaningful fromexecute. - abarnertc.fetchone(), notr.fetchone()? - Barmar