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 "....../sqlitedb.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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With