-3

集合知のコードの 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

実施例:

  1. DBを作成するには

C = Connect('test.db')

  1. 行を追加するには

C.create('2013-03-06','喜び',34)

  1. ファイルを変更してロックするには

C.commit()

  1. 行を取得する

C.getentry(プロフィール、名前、'ジョイ')

エラー: NameError: 名前 'Profile' が定義されていません

次に、括弧を付けます。

C.getentry('Profile','name','Joy')

結果 = [ ]

4

1 に答える 1

1

問題は にありConnect.createます。テーブルを作成しますが、その定義の上のコメントが暗示しているように見えるため、データを入力しません。次のようなものに更新する必要があります。

def create(self,date,name,age):
    self.con.execute( 'create table Profile(date text, name text,age real)' )
    self.con.execute( "insert into Profile values('{0}','{1}','{2}')"
                      .format(date,name,age) )
    return
于 2013-03-07T23:37:09.793 に答える