My table bank has three columns: uid, nick, and balance.
I can insert new row perfectly fine, however it would be easy for duplicates of the nick to appear since the uid auto increments, making each row unique. However I don’t want that, I only one want one row for each nick.
target = input.group(2)
cursor.execute ("INSERT INTO bank (nick, balance) VALUES('" + db.escape_string(target.lower()) + "', 5)")
db.commit()
bot.say('An account for ' + target + ' has been created.')
That is my code so far, however I am unsure in how I would create a SELECT query and then check if the nick already exists in the table before inserting a new row.
I would adopt a different approach. I would add a unique constraint at DB-level on the
nickcolumn:Within your Python code, then put a
tryandexceptto handle the unique constraint violated exception appropriately.