I wrote def addmember to add member to database.db. I want no repeat in user name, example: when i add member (1,1) to database, i cannot add member (1,2) to my database because already have user with username "1". But my code still allow it ! Can some one help me ? Node that self.textEdit.toPlainText(), self.textEdit_2.toPlainText() is just my qtextedit object, dont mind it
def addmember(self):
self.connection = sqlite3.connect("database.db")
cur = self.connection.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS USERS(USERNAME TEXT ,PASSWORD TEXT)")
cur.execute("SELECT * FROM USERS WHERE USERNAME=?", (self.textEdit.toPlainText()))
for row in cur.fetchall():
print(row)
if (len(cur.fetchall())==0):
cur.execute("INSERT INTO USERS VALUES(?,?)",(self.textEdit.toPlainText(), self.textEdit_2.toPlainText()))
self.connection.commit()
self.textEdit.clear()
self.textEdit_2.clear()
cur.close()
else:
self.textEdit.clear()
self.textEdit_2.clear()
cur.close()
self.connection.commit()
self.connection.close()
Your calling fetchall twice. The first fetches all the rows. The second will never get any rows and the len will always be 0 so your insert will always happen.