my_query="INSERT INTO student values(null,'New Name', 'Four', 65, 'female')"
my_conn.execute(my_query)
We can also read the ID ( alow known as rowid ) of most recently added row.
x=my_conn.execute('''select last_insert_rowid()''')
id=x.fetchone()
print(id[0])
Output
2
Or
r_set=my_conn.execute(q,my_data)
print(r_set.lastrowid)
my_data=(None,'Secon Name','Five',75,'male')
my_query="INSERT INTO student values(?,?,?,?,?)"
my_conn.execute(my_query,my_data)
We can check the ROWID of this added record. ( code with output as 3 )
x=my_conn.execute('''select last_insert_rowid()''')
id=x.fetchone()
print(id[0]) # 3
my_data=[(9, 'Tes Qry', 'Six', 78, 'male'),
(10, 'Big John', 'Four', 55, 'female'),
(11, 'Ronald', 'Six', 89, 'female'),
(12, 'Recky', 'Six', 94, 'female'),
(13, 'Kty', 'Seven', 88, 'female')]
my_query="INSERT INTO student values(?,?,?,?,?)"
my_conn.executemany(my_query,my_data)
We can check the row id of the last inserted records by using last_insert_rowid().
x=my_conn.execute('''select last_insert_rowid()''')
id=x.fetchone()
print(id[0])
Output is
13
my_data=[(18, 'Big John', 'Four', 55, 'female'),
(19, 'Ronald', 'Six', 89, 'female'),
(20, 'ONe more', 'Six', 89, 'female')]
my_query="INSERT INTO student values(?,?,?,?,?)"
curs=my_conn.executemany(my_query,my_data)
print(curs.rowcount)
Output
3
try:
my_data=[(24, 'Big John', 'Four', 55, 'female'),
(25, 'Ronald', 'Six', 89, 'female'),
(26, 'ONe more', 'Six', 89, 'female')]
my_query="INSERT INTO student values(?,?,?,?,?)"
curs=my_conn.executemany(my_query,my_data)
print(curs.rowcount())
except sqlite3.Error as my_error:
print("error: ",my_error)
Output ( as we already added the records with the above IDs )
error: UNIQUE constraint failed: student.id
Using SQLalchemy to insert record
Add record to SQLite table using Tkinter GUI
Sqlite Connection
delete
update
select
Order By Blob Data
02-02-2023 | |
Thank you for this article. It offers more than all the other websites I have seen so far. |