Почему может не работать UPDATE в sqlite?

sqlite добавляет в таблицу chat.id (cid) и значение lang=0
con = sqlite3.connect('users.sqlite')
c = con.cursor()
c.execute("INSERT INTO users (cid,lang) VALUES ('%s','%s')"%(str(cid),0))
con.commit()
bot.send_message(adminid, "\xF0\x9F\x99\x87 New user: "+str(cid))
c.close()
con.close()


При выборе языка в боте sqlite должен изменять параметр lang на 1 в строке с chat.id пользователя, но он этого не делает.

con = sqlite3.connect('users.sqlite')
c = con.cursor()
c.execute ("""UPDATE users SET lang = 1  WHERE cid="""+str(m.chat.id)+""" """)
c.close()
con.close()


Где ошибка?
  • Вопрос задан
  • 585 просмотров
Решения вопроса 1
dimonchik2013
@dimonchik2013
non progredi est regredi
Пригласить эксперта
Ответы на вопрос 1
sim3x
@sim3x
import os
import sqlite3

db_filename = 'whatever.db'
schema = '''
CREATE TABLE users(
id INTEGER PRIMARY KEY autoincrement not null,
cid INTEGER,
lang INEGER default 1)

'''

cid = 12

db_is_new = not os.path.exists(db_filename)

with sqlite3.connect(db_filename) as conn:
    if db_is_new:
        conn.executescript(schema)


    conn.execute("""
        INSERT INTO users (cid, lang) VALUES (?, ?) """ ,
        (cid, 0)
    )

    cursor = conn.cursor()

    cursor.execute('select id, cid, lang from users')
    for row in cursor.fetchall():
        print(row)

    print('update')
    cursor.execute ("UPDATE users SET lang = 22  WHERE cid=?", (cid,))

    print('after update')
    cursor.execute('select id, cid, lang from users')
    for row in cursor.fetchall():
        print(row)
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы