How do I run sqlite3 sql statement on Python only once?

Asked 2 years ago, Updated 2 years ago, 135 views

For example

conn = sqlite3.connect("test.db")
cursor = conn.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS TESTTABLE(ID INTEGER PRIMARY KEY AUTOINCREMENT, 
NAME1 CHAR(5), NAME2 CHAR(5))')
conn.commit()

cursor.execute("INSERT INTO TESTTABLE(ID,NAME1,NAME2) VALUES(NULL',Hong Gil-dong,Kim Gap-hwan")")
conn.commit()

I made it roughly like this. The problem is that every time you run it, the insert statement is executed and the value continues to go in. How do I get this done only once when I run a program?

python sqlite3

2022-09-22 14:44

1 Answers

Simply put, you only have to do it once (I'm not kidding.)

Single tone pattern can be used to make it called only once and init of the package.I think we can use py.

However, if you have the same concern as above, do not use the key as auto increment, but use the key as a unique value such as resident registration number or class number.

If the keys are the same, you can handle the exception.


2022-09-22 14:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.