集合知のコードの 1 つを模倣しようとしていますが、未知のエラーが原因でつまずきました。クエリを取得するためのメソッドを除いて、すべてのメソッドがクラス内で正常に機能します。
class Connect:
# Create the Database
def __init__(self,dbname):
self.con = sqlite3.connect(dbname)
# Add the row
def create(self,date,name,age):
self.con.execute('create table Profile(date text, name text,age real)')
# Lock the changes
def commit(self):
self.con.commit()
# Retrive the details
def getentry(self,table,field,value):
cursor = self.con.execute(
"select * from %s where %s = '%s'" % (table,field,value))
result_set = cursor.fetchall()
print result_set
実施例:
- DBを作成するには
C = Connect('test.db')
- 行を追加するには
C.create('2013-03-06','喜び',34)
- ファイルを変更してロックするには
C.commit()
- 行を取得する
C.getentry(プロフィール、名前、'ジョイ')
エラー: NameError: 名前 'Profile' が定義されていません
次に、括弧を付けます。
C.getentry('Profile','name','Joy')
結果 = [ ]