C
C
char9062021-02-06 22:18:35
Python
char906, 2021-02-06 22:18:35

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


SQLite looks like this:

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;


I output it as a message like this:

r = db.add_randcode(message.from_user.id)

bot.send_message(message.chat.id, '✅ Добро пожаловать '+str(r)+')


As a result, I always get ✅ Welcome and 4 random numbers that are not recorded in the database. How to make these 4 digits recorded in the database?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yaroslav, 2021-02-06
@char906

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

0
0xD34F, 2017-08-21
@JaredWinter

Replace text with html. Well, that is - in the click handler on the list item, instead of should be . Like so . $(this).text()$(this).html()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question