简体   繁体   中英

SQLite3 + Python sqlite3.OperationalError: table A has no column named message B

I don't understand, what wrong with my code? I create 2 tables 'users' and 'messages', but when I try to insert data in to messages table, raise this strange error -

sqlite3.OperationalError: table messages has no column named message

Here is my code

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()

Your are using create table if not exists , which will keep any old version of the table.

To ensure that the table has the desired schema, use a plain create table . Any old table can be deleted with drop table if exists .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM