0

I'm having some trouble passing in dynamic variables into my query. Please ignore the poor style. This is what I'm trying to run:

> sql = "SELECT COUNT(*) FROM artifacts WHERE " \
> "url = '%s' AND " \
> "source_id = '%s'"
> self.db.execute(sql, (url, source_id))

I get the error:

self.db.execute(sql)
AttributeError: execute

For the life of me, I can't figure out why it's throwing an attribute error. In the User Guide, the example clearly passes in an a correct attribute.

I've been following: http://mysql-python.sourceforge.net/MySQLdb.html

bites on lip eug.

unutbu
887k197 gold badges1.9k silver badges1.7k bronze badges
asked Mar 13, 2011 at 14:33

1 Answer 1

3

Just for clarification is your self.db attribute a connection or a cursor. Because you can only call the execute on a cursor!

If your following this example then you can see that there is create a cursor from the connection attribute and this cursor contains the execute method.

here is a small example:

import MySQLdb
## This is the connection to the database
self.db = MySQLdb.connect(host=self.host, port=self.port, user=self.user, passwd=self.passwd, db=self.dbname)
## To query you need a cursor, this is created here
c = self.db.cursor()
## On the cursor you can execute a sql stamement and look at result
rows = c.execute('select count(*) from test_table')
## To look at the result use a fetch method, here there is only one result so:
print rows.fetchone()
answered Mar 13, 2011 at 14:38
Sign up to request clarification or add additional context in comments.

2 Comments

Ahhhhh! This is starting to make sense. This is what I've done now: def db_connect(self): dbhandle = _mysql.connect(host = self.host, port = self.port, user = self.user, passwd = self.passwd, db = self.dbname) self.db = dbhandle.cursor() - does that make sense, because it doesn't seem to be working still.
Nevermind. I was using _mysql instead of MySQLdb which is apparently a wrapper for it. D'oh. Thanks!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.