1import sqlite3
2
3def insertMultipleRecords(recordList):
4 try:
5 sqliteConnection = sqlite3.connect('SQLite_Python.db')
6 cursor = sqliteConnection.cursor()
7 print("Connected to SQLite")
8
9 sqlite_insert_query = """INSERT INTO SqliteDb_developers
10 (id, name, email, joining_date, salary)
11 VALUES (?, ?, ?, ?, ?);"""
12
13 cursor.executemany(sqlite_insert_query, recordList)
14 sqliteConnection.commit()
15 print("Total", cursor.rowcount, "Records inserted successfully into SqliteDb_developers table")
16 sqliteConnection.commit()
17 cursor.close()
18
19 except sqlite3.Error as error:
20 print("Failed to insert multiple records into sqlite table", error)
21 finally:
22 if (sqliteConnection):
23 sqliteConnection.close()
24 print("The SQLite connection is closed")
25
26recordsToInsert = [(4, 'Jos', 'jos@gmail.com', '2019-01-14', 9500),
27 (5, 'Chris', 'chris@gmail.com', '2019-05-15',7600),
28 (6, 'Jonny', 'jonny@gmail.com', '2019-03-27', 8400)]
29
30insertMultipleRecords(recordsToInsert)
1import sqlite3
2
3def insertVaribleIntoTable(id, name, email, joinDate, salary):
4 try:
5 sqliteConnection = sqlite3.connect('SQLite_Python.db')
6 cursor = sqliteConnection.cursor()
7 print("Connected to SQLite")
8
9 sqlite_insert_with_param = """INSERT INTO SqliteDb_developers
10 (id, name, email, joining_date, salary)
11 VALUES (?, ?, ?, ?, ?);"""
12
13 data_tuple = (id, name, email, joinDate, salary)
14 cursor.execute(sqlite_insert_with_param, data_tuple)
15 sqliteConnection.commit()
16 print("Python Variables inserted successfully into SqliteDb_developers table")
17
18 cursor.close()
19
20 except sqlite3.Error as error:
21 print("Failed to insert Python variable into sqlite table", error)
22 finally:
23 if (sqliteConnection):
24 sqliteConnection.close()
25 print("The SQLite connection is closed")
26
27insertVaribleIntoTable(2, 'Joe', 'joe@pynative.com', '2019-05-19', 9000)
28insertVaribleIntoTable(3, 'Ben', 'ben@pynative.com', '2019-02-23', 9500)