-
Notifications
You must be signed in to change notification settings - Fork 74
/
test.py
290 lines (224 loc) · 12.7 KB
/
test.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# -*- coding: utf-8 -*-
import sys, os, time
from decimal import Decimal
def main():
for database_name, conn_string, create_table_sql in database_strings:
source_to_test = [
'Access',
'SQLServer',
#'MySQL',
'PostgreSQL'
]
if database_name in source_to_test:
print_header(database_name)
else:
database_name+' skipped.'
continue
print 'Connecting database server with pypyodbc...'
conn = pypyodbc.connect(conn_string, ansi = False, unicode_results = True, readonly = False, timeout = 2)
print conn.getinfo(pypyodbc.SQL_SERVER_NAME)
print conn.getinfo(pypyodbc.SQL_DATABASE_NAME)
print conn.getinfo(pypyodbc.SQL_PROCEDURES)
print conn.getinfo(pypyodbc.SQL_MAX_DRIVER_CONNECTIONS)
print 'Has table "pypyodbc_test_tabl"? ',
cur = conn.cursor()
has_table_data = cur.tables(table='pypyodbc_test_tabl').fetchone()
cur.close()
if has_table_data:
print 'pypyodbc_test_tabl exists. Dropping the existing pypyodbc_test_tabl now.',
cur = conn.cursor()
cur.execute ('Drop table pypyodbc_test_tabl')
else:
print 'pypyodbc_test_tabl does not exist',
cur = conn.cursor()
if hasattr(cur,'execdirect'):
cur.execdirect(create_table_sql)
else:
cur.execute(create_table_sql)
cur.commit()
print ('pypyodbc_test_tabl has been created. Now listing the columns:')
for row in cur.columns(table='pypyodbc_test_tabl').fetchall():
print row
cur.close()
print 'Inserting rows now with execute()... ',
start_time = time.time()
cur = conn.cursor()
cur.execute(u"insert into pypyodbc_test_tabl values(1,'Hello! 这是pypyodbc模块',12.3,1234.55,'2012-11-11','17:31:32','2012-11-11',NULL, ?)", (binary_logo,))
longtext = u''.join([u'我在马路边,捡到一分钱。']*25)
cur.execute("insert into pypyodbc_test_tabl values (?,?,?,?,NULL,NULL,NULL,NULL,?)", \
(2, \
longtext,\
Decimal('1233.4513'), \
123.44, \
# datetime.datetime.now(), \
# datetime.datetime.now().time(),\
# datetime.date.today(),\
mv))
row_num = 1500
print 'Inserting 5*'+str(row_num)+' rows now with executemany()... ',
for i in xrange(3,row_num):
cur.executemany(u"""insert into pypyodbc_test_tabl values
(?,?,12.32311, 1234.55, NULL,NULL,'2012-12-23',NULL,NULL)""",
[
(i+500000, "【巴黎圣母院】".decode('utf-8')),
(i+100000, u"《普罗米修斯》"),
(i+200000, longtext),
(i+300000, '〖!@#$$%"^&%&〗'.decode('utf-8')),
(i+400000, "querty-','"),
]\
)
end_time = time.time()
print 'Inserting completed, total time ',
print end_time-start_time,
conn.commit()
print ' Commit comlete, commit time ',
print time.time() - end_time
print 'Excute selecting from pypyodbc_test_tabl... '
if database_name in ['Access','MySQL']:
# Access and MySQL do not support batch SQL, so can not test the nextset() method.
cur.execute(u"""select * from pypyodbc_test_tabl""")
print cur.description
#Get results
field = cur.fetchone()[-1]#.bin_logo
file(cur_file_dir()+'\\logo_'+database_name+'.gif','wb').write(field)
field = cur.fetchone()[-1]#.bin_logo
file(cur_file_dir()+'\\logo2_'+database_name+'.gif','wb').write(field)
for row in cur.fetchmany(6):
for field in row:
print type(field),
if isinstance(field, unicode): print field.encode('mbcs'),
elif isinstance(field, bytearray): pass
else: print field,
print ('')
cur.close()
#conn.rollback()
cur = conn.cursor()
start_time = time.time()
print 'Updating one column...',
cur.execute(u'update pypyodbc_test_tabl set 数量 = ? where 数量 > 0 '.encode('mbcs'),(time.time(),))
print 'Updated: '+str(cur.rowcount)+' rows',
print ' Total time: '+ str(time.time()-start_time)
else:
cur.execute(u"""select * from pypyodbc_test_tabl;update pypyodbc_test_tabl set kong = 5 where ID = 2;select ID, kong, riqi, product_name from pypyodbc_test_tabl where ID = 2 """)
print cur.description
#Get results
field = cur.fetchone()[-1]#.bin_logo
file(cur_file_dir()+'\\logo_'+database_name+'.gif','wb').write(field)
field = cur.fetchone()[-1]#.bin_logo
file(cur_file_dir()+'\\logo2_'+database_name+'.gif','wb').write(field)
for row in cur.fetchmany(6):
for field in row:
print type(field),
if isinstance(field, unicode): print field.encode('mbcs'),
elif isinstance(field, bytearray): pass
else: print field,
print ('')
cur.nextset()
print 'After calling nextset, get the information of updated: '+str(cur.rowcount)+' rows'
cur.nextset()
for field in cur.fetchone():
if isinstance(field, unicode):
print field.encode('mbcs')+'\t',
elif isinstance(field, bytearray):
pass
else:
print str(field)+'\t',
print ('')
print ' Total time: '+ str(time.time()-start_time)
cur.close()
conn.commit()
cur = conn.cursor()
for field in cur.execute(u"""select * from pypyodbc_test_tabl""").fetchone():
if isinstance(field, unicode):
print field.encode('mbcs')+'\t',
elif isinstance(field, bytearray):
pass
else:
print str(field)+'\t',
print ('')
start_time = time.time()
i = 1
row = cur.fetchone()
cur.skip(1500)
while row != None:
for field in row:
x = field
i += 1
if i % 1000 == 0:
print i,
row = cur.fetchone()
print ' Total records retrive time:',
print time.time() - start_time
#print conn.FetchAll()
#Close before exit
cur.close()
#cProfile.run('prof_func()')
#conn.close()
print ('End of testing')
time.sleep(3)
def cur_file_dir():
return os.path.dirname(os.path.realpath(sys.argv[0]))
import base64
binary_data = base64.b64decode('iVBORw0KGgoAAAANSUhEUgAAABoAAAAcCAYAAAB/E6/TAAAABHNCSVQICAgIfAhkiAAAAAFzUkdCAK7OHOkAAAAEZ0FNQQAAsY8L/GEFAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAHY0lEQVRIS41WC3CU1RX+/n0k+8hjN+8HSZSQLAmgQIBREWSiglqHp0Bt61tatBVxqh3xAQXqQGkBQVQipcx0cEqtCFItoh1BjEmAPAhsIDEQks2yG5N9JJvdJPu8Pff+2U1S6Ix35+7///d1zj3nO985UoSFGEY1CRJ9KUYPjbyzyJhxhuGtfMuoxsflc0aaNFqQBCXN+LGl82184vgUV/0dCEWC0Co0qCh6Bw8YF8AT8ojd/KBUdSoU0lilGAvzyRvasCCugQqXI7WYtK4CGfeeRWJqCArGBYP0Y3AGneJpVBmgltToCTnhDXsRCAdIYArKDfPwVObjeDBlgSyEbs+kEWNJYWE6iYylwKRGI7pePArDug1QpjkgMUkYJxgJQUWat85sukHTYCSAs946HHYcQYV9PwYjg3gzfx02Frw5rCTdkLcQo8u2tzO2aBm7pqRzK8uZobSeTbiSwbJrCpi+MpWV1d3BvCEvreQtTF3sGtWHp+hxuOcIS6nKZonfpbPWgVYxwd2jEK7/+IgwkCYUwNPJU2F6/jOYIlPxy6xn8PVtx1E7vRp6pZ5WhBERPzb85G/8FxYdNLo0bTGcd9qwPG0Zis4U4Wz/ObIXuYBLbB64yrxscEQtFhj1Lt+Ca3WzHmZBmpV7dJ7fgbcnmp9lCZVp4l3if/ebH8YkfQnevvVPQisBW+4c4UzupxvheiOuxo5Ew0Q6JcFMvhXYVNNjn+0AmgdauCHlHQKiP15ILKZi8hTYYd0FhVIHR9Ahn3ptqAN58dmYdX4OTvZ+I2wqx5QcLz+m8XXcANG9WyzbsMHyB6Sr03B1qE0WVKorocBT4be5a1B+YT4ea34anf7O2CaxmaDOG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Ny3CE9nP4M+3bsU3xHnbr+/GRbpNAuWiEtKUgyE3PofgnYguQuK1wQ4KiRZCqYUAosPSlIVYTbF3xHUMtf11cId6RR77YvIxxIoTLqzTb8WiS4+ggRa9kLMGL+etRX58HtqJdHnMtA22Ua3g4GYg8KiJPcYTAMahQJOHeEU89tr2CbSuL3gVPwQc2Nq5Fe67emCgOmOMoI+IclZkLMcliub1ls343HkcKoWaoF2Gucl3o5j8lkX+iKOfPdgFCxFvVV81qvvPwBPux0+MD2Bx6kLssu2huGxA47QalOpLRfYdJUiiIiSMh5uWoixhGtbm/gYZcRlEiHYizQacI+Jsp0D2ESg4/XBAmLRFKNVOJIiPg5WC9V37Xnzp/ByPUqAfNP1VlGJyiqf0Ea3rRBYlb0l0yDFa/Erbq0LLh6h84qVUsaaY/BAvArCH+M5KZj5OhFvpqSFlrMjXFGB11rP4FfkoRW3kCUVUFtE2poCMDQ4nPSrFRABW9lUKU/CSjFOSQpKQpErCvKR7BL+V6CaOyo3y4XLRMlIC3FSQnNCiTc60/7/JmvP10XLxZjXGfwELZFMqTp226QAAAABJRU5ErkJggg==')
if os.path.exists(cur_file_dir()+'/Logo.gif'):
binary_data = file(cur_file_dir()+'/Logo.gif','rb').read()
binary_logo = bytearray(binary_data)
mv = bytearray(binary_data)
#c_Path = ctypes.create_string_buffer(u"CREATE_DB=.\\e.mdb General\0\0".encode('mbcs'))
#ODBC_ADD_SYS_DSN = 1
#ctypes.windll.ODBCCP32.SQLConfigDataSource(None,ODBC_ADD_SYS_DSN,"Microsoft Access Driver (*.mdb)", c_Path)
def u8_enc(v, force_str = False):
if v == None: return ('')
elif isinstance(v,unicode): return (v.encode('utf_8','replace'))
elif isinstance(v, buffer): return ('')
else:
if force_str: return (str(v))
else: return (v)
def print_header(database_name):
print ' *'.join(['' for i in range(40)])
print ' '*30 + database_name
print ' *'.join(['' for i in range(40)])
if __name__ == "__main__":
if 'pyodbc' in sys.argv:
print 'Running with pyodbc'
import pyodbc as pypyodbc
else:
print 'Running with pypyodbc'
import pypyodbc
print pypyodbc.version
mdb_path = cur_file_dir()+'\\e.mdb'
if hasattr(pypyodbc,'win_create_mdb') and sys.platform in ('win32','cli'):
pypyodbc.win_create_mdb('"'+mdb_path+'"')
database_strings = [\
('Access',
u'''Driver={Microsoft Access Driver (*.mdb)};DBQ='''+mdb_path,
u"""create table pypyodbc_test_tabl (ID integer PRIMARY KEY,product_name text,数量 numeric,价格 float,日期
datetime,shijian time,riqi datetime, kong float, bin_logo LONGBINARY)""",
),
('SQLServer',
'DSN=MSSQL',
u"""create table pypyodbc_test_tabl (ID integer PRIMARY KEY,product_name text,数量 numeric(14,4),价格 float,日期
datetime,shijian varchar(20),riqi varchar(20), kong float, bin_logo varbinary(8000))""",
),
('MySQL',
'DSN=MYSQL',
u"""create table pypyodbc_test_tabl (ID integer PRIMARY KEY,product_name text,数量 numeric(14,4),价格 float,日期
datetime,shijian time,riqi date, kong float, bin_logo BLOB)""",
),
('PostgreSQL',
'DSN=PostgreSQL35W',
u"""create table pypyodbc_test_tabl (ID integer PRIMARY KEY,product_name text,数量 numeric(14,4),价格 float,日期
timestamp,shijian time,riqi date, kong float, bin_logo bytea)""",
),
]
pypyodbc.DEBUG = 0
DSN_list = pypyodbc.dataSources()
print (DSN_list)
if 'profile' in sys.argv:
import cProfile
cProfile.run('main()')
else:
main()
if hasattr(pypyodbc,'win_compact_mdb') and sys.platform in ('win32','cli'):
mdb_file_path = '"'+mdb_path.encode('mbcs')+'"'
pypyodbc.win_compact_mdb(mdb_file_path,mdb_file_path.replace('.mdb','_compact.mdb'))