tags:

views:

159

answers:

2

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  A: 

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 .

S.Lott
The path (C:\python25\) tells us that Windows is being used. The substitution of so-called smart quotes for the real thing is one of Windows' more irritating habits.
pavium
This isn't a windows issue, its a blog/wordpress issue where he copied the original connection code from. A lot of newer blogging engines pretty up the quotes if the poster isn't careful to identify a block as code.
Soviut
+3  A: 

Hi

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

OctaneFX