-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10-executemany.py
More file actions
67 lines (55 loc) · 2.1 KB
/
Copy path10-executemany.py
File metadata and controls
67 lines (55 loc) · 2.1 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: utf-8 -*-
import sqlite3
with sqlite3.connect("new.db") as connection:
c = connection.cursor()
# insert multiple records using a list of tuples
cities = [
('Boston', 'MA', 600000),
('Los Angeles', 'CA', 38000000),
('Houston', 'TX', 2100000),
('Philadelphia', 'PA', 1500000),
('San Antonio', 'TX', 1400000),
('San Diego', 'CA', 130000),
('Dallas', 'TX', 1200000),
('San Jose', 'CA', 900000),
('Jacksonville', 'FL', 800000),
('Indianapolis', 'IN', 800000),
('Austin', 'TX', 800000),
('Detroit', 'MI', 700000)
]
c.executemany("INSERT INTO population VALUES(?, ?, ?)", cities)
c.execute("SELECT * FROM population WHERE population > 1000000")
rows = c.fetchall()
for r in rows:
print r[0], r[1], r[2]
# create a table and populate it with data
c.execute("DROP TABLE IF EXISTS regions")
c.execute("""CREATE TABLE IF NOT EXISTS regions
(city TEXT, region TEXT)
""")
cities = [
('New York City', 'Northeast'),
('San Francisco', 'West'),
('Chicago', 'Midwest'),
('Houston', 'South'),
('Phoenix', 'West'),
('Boston', 'Northeast'),
('Los Angeles', 'West'),
('Houston', 'South'),
('Philadelphia', 'Northeast'),
('San Antonio', 'South'),
('San Diego', 'West'),
('Dallas', 'South'),
('San Jose', 'West'),
('Jacksonville', 'South'),
('Indianapolis', 'Midwest'),
('Austin', 'South'),
('Detroit', 'Midwest')
]
c.executemany("INSERT INTO regions VALUES(?, ?)", cities)
c.execute("SELECT * FROM regions ORDER BY region ASC")
# rows = c.fetchall()
# DAMN! The cursor object works as an iterator,
# invoking fetchall() automatically
for r in c:
print r[0], r[1]