0

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

4

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

Sign up to request clarification or add additional context in comments.

Comments

2

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 .

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.