forked from jiangwen365/pypyodbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathodbc_bench.py
142 lines (114 loc) · 5.29 KB
/
odbc_bench.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# -*- coding: utf-8 -*-
'''
import pypyodbc
conn=pypyodbc.connect('dsn=mssql')
cur = conn.cursor()
cur.execute('select top 3 product_name from pyodbc_t')
for row in cur.fetchall():
for field in row:
print field.decode('mbcs'),
print ''
import sqlalchemy
engine = sqlalchemy.create_engine('mssql+pypyodbc://mssql')
for row in engine.execute('select top 2 product_name from pyodbc_t'):
for field in row:
print field.decode('mbcs'),
print ''
'''
# Let Python load it's ODBC connecting tool pypyodbc
from __future__ import print_function
import os, os.path, time, sys
from pypyodbc import win_create_mdb
for x in range(120):
print ('file: '+str(x))
#import pypyodbc_reuse_param as pypyodbc
#import pyodbc as pypyodbc
if 'pyodbc' in sys.argv:
import pyodbc as pypyodbc
print ('Running with pyodbc %s' %pypyodbc.version)
else:
import pypyodbc as pypyodbc
print ('Running with pypyodbc %s' %pypyodbc.version)
t_begin = time.time()
fix = str(x%10)
if os.path.exists(u'D:\\pypyodbc_mdb_test\\YourMDBfilepath'+fix+'.mdb'):
os.remove(u'D:\\pypyodbc_mdb_test\\YourMDBfilepath'+fix+'.mdb')
if os.path.exists(u'D:\\pypyodbc_mdb_test\\YourMDBfilepath'+fix+'c.mdb'):
os.remove(u'D:\\pypyodbc_mdb_test\\YourMDBfilepath'+fix+'c.mdb')
if os.path.exists(u'D:\\pypyodbc_mdb_test\\YourMDBfilepath'+fix+'copy.mdb'):
os.remove(u'D:\\pypyodbc_mdb_test\\YourMDBfilepath'+fix+'copy.mdb')
win_create_mdb( u'D:\\pypyodbc_mdb_test\\YourMDBfilepath'+fix+'.mdb' )
conn = pypyodbc.connect(u'Driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\pypyodbc_mdb_test\\YourMDBfilepath'+fix+'.mdb')
print (conn.getinfo(pypyodbc.SQL_DRIVER_NAME))
cur = conn.cursor()
cur.execute(u"""create table saleout (ID COUNTER PRIMARY KEY, customer_name varchar(255),
product_name varchar(255),
price float,
volume int,
sell_time datetime);""")
conn.commit()
for b in range(2500):
cur.executemany(u'''INSERT INTO saleout(customer_name,product_name,price,volume,sell_time)
VALUES(?,?,?,?,?)''', [(u'杨天真','Apple IPhone 5','5500.1',1,'2012-1-21'),
(u'郑现实','Huawei Ascend D2',None,1,'2012-1-21'),
(u'莫小闵','Huawei Ascend D2','5000.5',2,'2012-1-21'),
(u'顾小白','Huawei Ascend D2','5000.5',None,'2012-1-22')])
cur.commit()
if b%100 == 0:
print (b*4, end='\r')
#cur.execute('INSERT INTO saleout (customer_name,product_name,price,volume,sell_time) SELECT customer_name,product_name,price,volume,sell_time FROM saleout;')
cur.commit()
cur.close()
conn.commit()
conn.close()
write_time = time.time() - t_begin
print ('\tWrite time: '+str(write_time))
#pypyodbc.win_compact_mdb('D:\\pypyodbc_mdb_test\\YourMDBfilepath'+fix+'.mdb','D:\\pypyodbc_mdb_test\\YourMDBfilepath'+fix+'c.mdb')
t_begin = time.time()
conn = pypyodbc.connect(u'Driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\pypyodbc_mdb_test\\YourMDBfilepath'+fix+'.mdb',unicode_results=True)
#conn = pypyodbc.win_connect_mdb('D:\\pypyodbc_mdb_test\\YourMDBfilepath'+fix+'.mdb')
cur = conn.cursor()
win_create_mdb( u'D:\\pypyodbc_mdb_test\\YourMDBfilepath'+fix+'copy.mdb' )
conn_copy = pypyodbc.connect(u'Driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\pypyodbc_mdb_test\\YourMDBfilepath'+fix+'copy.mdb')
cur_copy = conn_copy.cursor()
cur_copy.execute(u"""create table saleout (ID COUNTER PRIMARY KEY, customer_name varchar(255),
product_name varchar(255),
price float,
volume int,
sell_time datetime);""")
cur.execute('select customer_name,product_name,price,volume,sell_time from saleout')
r = cur.fetchmany(4)
r_n = 0
while r:
#if r_n == 0:
# print (r['product_name'],r[:])
cur_copy.executemany('''INSERT INTO saleout(customer_name,product_name,price,volume,sell_time)
VALUES(?,?,?,?,?)''', r)
cur_copy.commit()
if r_n % 400 == 0:
print (r_n, end='\r')
r = cur.fetchmany(4)
r_n +=4
#cur_copy.close()
cur_copy.commit()
conn_copy.close()
conn.close()
R_W_time = time.time() - t_begin
print ('\tR & W time: '+str(time.time() - t_begin))
t_begin = time.time()
conn = pypyodbc.connect(u'Driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\pypyodbc_mdb_test\\YourMDBfilepath'+fix+'copy.mdb',unicode_results=True)
#conn = pypyodbc.win_connect_mdb('D:\\pypyodbc_mdb_test\\YourMDBfilepath'+fix+'.mdb')
cur = conn.cursor()
cur.execute('select customer_name,product_name,price,volume,sell_time from saleout')
r = cur.fetchmany(4)
r_n = 0
while r:
#if r_n == 0:
# print (r['product_name'],r[:])
if r_n % 400 == 0:
print (r_n, end='\r')
r = cur.fetchmany(4)
r_n +=4
#cur_copy.close()
conn.close()
print ('\tRead time: '+str(time.time() - t_begin))