i am finally starting with python. i wanted to ask if i use the mysql db with python, how should i expect python to connect to the db? what i mean is, i have mysql installed in xampp and have my database created in mysql through php myadmin. now my python is in C:\python25\ and my *.py files would be in the same folder as well. now do i need any prior configuration for the connection?
what i am doing now
>>> cnx = MySQLdb.connect(host=’localhost’, user=’root’, passwd=’’, db=’tablename’)
SyntaxError: invalid syntax
how do i need to go around this?
2 Answers 2
the basics is
import MySQLdb
conn = MySQLdb.connect(host="localhost", user="root", passwd="nobodyknow", db="amit")
cursor = conn.cursor()
stmt = "SELECT * FROM overflows"
cursor.execute(stmt)
# Fetch and output
result = cursor.fetchall()
print result
# get the number of rows
numrows = int(cursor.rowcount)
# Close connection
conn.close()
and don ́t use ’ use single or double ' ou " quotes
Comments
If you simply cut and pasted, you have the wrong kind of quotes.
You've got some kind of asymmetric quote.
Use simple apostrophes ' or simple quotes ".
Do not use ’ .