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