Answer the question
In order to leave comments, you need to log in
When updating data in the sqlite3 database, it updates only 1 row, how can I do it to update everything at once?
The for loop in the database iterates over all vk_id, and when passing them to UPDATE, only one is updated, the very first in the database.
import sqlite3
import vk_api
import datetime
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
token = "token vk"
vk = vk_api.VkApi(token=token)
longpoll = VkBotLongPoll(vk, group_vk_id)
db = sqlite3.connect("vitaliy.db")
qwe = db.cursor()
qwe.execute(""" CREATE TABLE IF NOT EXISTS stata (
id INTEGER PRIMARY KEY AUTOINCREMENT,
vk_id INTEGER NOT NULL UNIQUE,
nickname TEXT DEFAULT "Unnamed",
coins BIGINT DEFAULT 5000,
prem TEXT DEFAULT "Нет",
dohod BIGINT DEFAULT 1000
) """)
##################################################################################################
def sender(chat_id, message):
vk.method("messages.send", {"chat_id": chat_id, "message": message, "disable_mentions": 1, "random_id": 0})
##################################################################################################
while True:
now = datetime.datetime.now()
timeC = now.second
if timeC != datetime.datetime.now().second:
for i in qwe.execute(""" SELECT vk_id FROM stata """):
qwe.executemany("""
UPDATE stata
SET coins = coins + dohod
WHERE vk_id = ?
""", (i,))
db.commit()
else:
None
timeC = datetime.datetime.now().second
Answer the question
In order to leave comments, you need to log in
cursor.execute returns a generator that returns tuples of values. So in the design:
for i in qwe.execute(""" SELECT vk_id FROM stata """):
-- добавить всем 1
UPDATE stata SET coins = coins+1;
-- добавить удвоенный доход всем с вип-статусом, если срок его действия ещё не истёк
UPDATE stata SET coins = coins+dohod*2 WHERE vip_status=1 AND vip_status_end>=NOW();
because there can't be more than 1 entry because of this:
vk_id INTEGER NOT NULL UNIQUE ,
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question