-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path30db_insert.py
More file actions
31 lines (28 loc) · 989 Bytes
/
30db_insert.py
File metadata and controls
31 lines (28 loc) · 989 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python
import sqlite3
dbPath = "DBtesty/db01.sqlite"
tableNameList = ["t01"] # , "t02", "t03"
columnName = "id" # id / col01
startValue = 42.5
totalRecords = 1
values = "?"
try:
con = sqlite3.connect(dbPath)
print("OK: connected to", dbPath)
except sqlite3.Error as error:
print("Problem to connect to {}: {}".format(dbPath, error))
else:
try:
with con:
cur = con.cursor()
for tableName in tableNameList:
query = "INSERT INTO {}({}) VALUES({})".format(
tableName, columnName, values)
for iter in range(totalRecords):
newRecord = (startValue + iter, )
cur.execute(query, newRecord)
print("OK: simple record inserted")
print("Last rowid:", cur.lastrowid)
print("Modified rows:", cur.rowcount)
except sqlite3.Error as error:
print("Problem to insert:", error)