-
Notifications
You must be signed in to change notification settings - Fork 294
/
data_access.py
executable file
·47 lines (39 loc) · 1.18 KB
/
data_access.py
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
#!/usr/bin/env python
from random import randint
import sys
import mysql.connector
NUM_EMPLOYEES = 10000
def insert():
cursor = connection.cursor()
try:
cursor.execute("drop table employees")
except:
pass
cursor.execute("create table employees (id integer primary key, name text)")
cursor.close()
print("Inserting employees...")
for n in xrange(0, NUM_EMPLOYEES):
cursor = connection.cursor()
cursor.execute("insert into employees (id, name) values (%d, 'Employee_%d')" %
(n, n))
connection.commit()
cursor.close()
def select():
print("Selecting employees...")
while True:
cursor = connection.cursor()
cursor.execute("select * from employees where name like '%%%d'" % randint(0, NUM_EMPLOYEES))
for row in cursor:
pass
cursor.close()
connection = mysql.connector.connect(host='localhost', database='test', user='root')
if "insert" in sys.argv:
while True:
insert()
elif "insert_once" in sys.argv:
insert()
elif "select" in sys.argv:
select()
else:
print("USAGE: data_access.py <insert|insert_once|select>")
connection.close()