Answer the question
In order to leave comments, you need to log in
Why can't I write to the SQlite database?
the code:
def add_randcode(self, code):
with self.connection:
code = self.cursor.execute("SELECT * FROM `subscriptions` WHERE `code` = ?", (code, )).fetchone()
if code == None:
code = random.randint(1000,9999)
self.cursor.execute("INSERT INTO subscriptions VALUES(?)", code,)
else:
return code
PRAGMA foreign_keys = 0;
CREATE TABLE sqlitestudio_temp_table AS SELECT *
FROM subscriptions;
DROP TABLE subscriptions;
CREATE TABLE subscriptions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INT (255),
first_name TEXT,
money INT DEFAULT (50),
status INT DEFAULT (5),
code INTEGER
);
INSERT INTO subscriptions (
id,
user_id,
first_name,
money,
status,
code
)
SELECT id,
user_id,
first_name,
money,
status,
code
FROM sqlitestudio_temp_table;
DROP TABLE sqlitestudio_temp_table;
PRAGMA foreign_keys = 1;
r = db.add_randcode(message.from_user.id)
bot.send_message(message.chat.id, '✅ Добро пожаловать '+str(r)+')
Answer the question
In order to leave comments, you need to log in
Simplify the code, remove all unnecessary lines for debugging (even if they are needed at work), so that you can clearly see where the miracle happens and why. Add prints to see how the code passes and print variables. Further, most likely, you will see for yourself where your problem is, and if not, you can reduce it to a code with 2-3 lines.
Perhaps they forgot to do a commit() ?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question