pythonsqlsqlitesqlite3-python

sqlite3 syntax for creating rows with 2 always unique column values


I have a table "statistic" that contains guild_id, category and number

I need to create rows with values that takes my func, but they need to be both unique:

table

If 1 column value and 2 column value are same: skip

This is my code:

    def save_statistic(self, category: str,
                   guild_id: int):
    self.c.execute('''INSERT OR IGNORE INTO statistic 
                   (statistic_channel_id, category, number) 
                   VALUES ((?), (?), 0)
                   ''', (guild_id, category,))

I don't really know sql syntax, I need it only for this function


Solution

  • For this case I used UNIQUE in creating statistic table:

    CREATE TABLE IF NOT EXISTS statistic (
    guild_id integer,
    category char(255),
    number integer,
    UNIQUE(guild_id, category)
                                          );
    

    then this code works:

    self.c.execute('''INSERT OR IGNORE INTO statistic 
                       (guild_id, category, number) 
                       VALUES ((?), (?), 0)
                       ''', (guild_id, category,))