-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path33db_insert_smart.py
More file actions
36 lines (30 loc) · 1.12 KB
/
33db_insert_smart.py
File metadata and controls
36 lines (30 loc) · 1.12 KB
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
32
33
34
35
36
#!/usr/bin/env python
import sqlite3
import random
dbPath = "DBtesty/db01.sqlite"
tableName = "t01"
totalRecords = 2
def generateRandomValues(tableInfo, totalRow):
totalCol = len(tableInfo)
return [tuple(random.random() for i in range(totalCol)) for x in range(totalRow)]
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()
query = "SELECT * FROM PRAGMA_TABLE_INFO('{}');".format(tableName)
tableInfoList = cur.execute(query).fetchall()
# for columnTuple in tableInfoList:
# print(columnTuple)
questionMarksInQuery = ("?," * len(tableInfoList))[:-1]
query = "INSERT INTO {} VALUES({})".format(
tableName, questionMarksInQuery)
cur.executemany(query, generateRandomValues(
tableInfoList, totalRecords))
print("Modified rows:", cur.rowcount)
except sqlite3.Error as error:
print("Problem to insert:", error)