开发者

python - 'str' object has no attribute 'execute'

开发者 https://www.devze.com 2023-01-21 11:50 出处:网络
First time python user here, be gentle.... ;-) Python 2.6 on OSX Got a class which just has some wrappers around sqlite... here it is

First time python user here, be gentle.... ;-)

Python 2.6 on OSX

Got a class which just has some wrappers around sqlite... here it is

from pysqlite2 import dbapi2 as sqlite

class SqliteDB:
    connection = ''
    curser = ''

    def connect(self):
        try:
            self.connection = sqlite.connect("pagespeed.sqlite")
            self.curser = self.connection.cursor()
         except sqlite.Error, e:
            print "Ooops: ", e.args[0]

    def find_or_create(self, table, column, value):
        self.curser.execute("SELECT id FROM ? WHERE ?=? LIMIT 1", (table, column, value))
        records = self.curser.fetchall()
        if records.count() == false:
            self.curser.execute("INSERT into ? SET ?=?", (table, column, value))
            self.curser.execute("SELECT id FROM ? WHERE ?=? LIMIT 1", (table, column, value))
        print records

and I call it like this in a separate file

import sqlitedb

def main():
    db = sqlitedb.SqliteDB()
    db.connect    
    url_id = db.find_or_create('urls', 'url', 'http://www.example.com')

however I get this error,

Traceback (most recent call last):

  File "update_urls.py", line 17, in <module>

  main()

  File "update_urls.py", line 11, in main

  url_id = db.find_or_create('urls', 'url', 'http://www.example.com')

  File "....../sqli开发者_开发知识库tedb.py", line 16, in find_or_create

  self.curser.execute("SELECT id FROM ? WHERE ?=? LIMIT 1", (table, column, value))

AttributeError: 'str' object has no attribute 'execute'

So it's almost like self.curser is not getting a curser, or is self not correct?

Not to sure if what I am doing is right here....

cheers


I don't know what's wrong, but at the very least:

db.connect  

should be

db.connect()

e.g. call the function.

OK. S.Lott had the answer, I just found an other bug :)


Do Not Do This.

class SqliteDB:
    connection = ''
    curser = ''

It doesn't "declare" any variables. This isn't C++ or Java.

Do this.

class SqliteDB:
    def __init__( self ):
        self.connection = None
        self.cursor= None


And the 3rd bug:

self.curser.execute("SELECT id FROM ? WHERE ?=? LIMIT 1", (table, column, value))

You can't parameterise table names and column names. All you can parameterise are things that can be an expression in SQL syntax. You'll need to do something like this:

sql = "SELECT id FROM %s WHERE %s = ? LIMIT 1" % (table, column)
self.curser.execute(sql, (value, ))

Oh yeah, to save the flurry of comments: or use the modern string.format(data) method instead of the antique string % data operator.


I will also add that this will not work :

curser.execute("SELECT id FROM ? WHERE ?=? LIMIT 1", (table, column, value))

because placeholders (?) doesn't work for table name, you should rather use string formatting before if you still want to use table name as parameter:

query = "SELECT id FROM %s WHERE %s=? LIMIT 1" % (table, column)
curser.execute(query, (value, ))

and one last thing "curser" is misspelled :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号