这里是 python 用户,请保持温柔......;-)
OSX 上的 Python 2.6
得到了一个类,它只是对 sqlite 进行了一些包装...在这里
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
我在一个单独的文件中这样调用它
import sqlitedb
def main():
db = sqlitedb.SqliteDB()
db.connect
url_id = db.find_or_create('urls', 'url', 'http://www.example.com')
但是我得到这个错误,
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'
所以这几乎就像 self.curser 没有得到一个 curser,或者 self 不正确?
不确定我正在做的是否就在这里....
干杯
最佳答案
我不知道哪里出了问题,但至少:
db.connect
应该是
db.connect()
例如调用函数。
好的。 S.Lott 有答案,我刚刚发现了另一个错误 :)
关于python - 'str' 对象没有属性 'execute',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4006257/