2016-06-17 9 views
0

私のコードで何が間違っていますか? 私は2つのテーブルに「ユーザ」と「メッセージ」を作成しますが、私はこの奇妙なエラーが発生し、メッセージテーブルにデータを挿入しようとする - ここでSQLite3 + Python sqlite3.OperationalError:テーブルAにメッセージBという名前の列がありません

sqlite3.OperationalError: table messages has no column named message 

は私のコード

conn = sqlite3.connect(self.path) 
cur = conn.cursor() 
# Create the table 'users' 
cur.execute('''create table if not exists users 
      (uid INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, 
      name TEXT NOT NULL)''') 
conn.commit() 
# Create the table 'messages' 
cur.execute('''create table if not exists messages (uid INTEGER NOT NULL, message TEXT)''') 
conn.commit() 
example_data1 = [(1,'Alex'), (2,'Jane'), (3,'Max'), (4,'Lui')] 
example_data2 = [(1,'Hi!!!'), (1, 'Hello...'), (1, 'See you late!:)))'), 
       (2, 'Nice to meet you John'), (2, 'Bye-bye...'), 
       (3, 'Good morning')] 
# Inserts example data to table -'users' 
cur.executemany('''INSERT OR IGNORE INTO users(uid, name) VALUES(?,?)''', example_data1) 
conn.commit() 
cur.executemany('''INSERT INTO messages(uid, message) VALUES(?,?)''', example_data2) 
conn.commit() 
# Takes users name and count of messages of this users 
cur.execute('SELECT users.name, count(messages.message) as count_msg FROM users LEFT JOIN messages ON users.uid = messages.uid GROUP BY users.name ORDER BY count_msg DESC') 
result = cur.fetchall() 
cur.close() 
+0

「テーブルメッセージにはメッセージという名前の列がありますか? – gdlmx

+0

データベースファイルを削除した後に機能しますか? –

+0

ありがとう、@ CL!データベースファイルを削除すると、作業が始まりました!!! –

答えて

0

あなたのアールですcreate table if not existsを使用すると、古いバージョンのテーブルが保持されます。

表に目的のスキーマがあることを確認するには、平文create tableを使用します。 古いテーブルはすべてdrop table if existsで削除できます。

関連する問題