-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyodbc_example.py
79 lines (34 loc) · 1.4 KB
/
pyodbc_example.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
conn = pyodbc.connect(r'DRIVER={SQL Server Native Client 11.0};SERVER=test;DATABASE=test;UID=user;PWD=password')
myconnection = pyodbc.connect(myconnectionstring, autocommit=True)
mycursor = cnxn.cursor()
cnxn.commit()
cnxn.close()
data_source_name = cnxn.getinfo(pyodbc.SQL_DATA_SOURCE_NAME)
num_products = cnxn.execute("SELECT COUNT(*) FROM product")
cnxn = pyodbc.connect('mydsn')
do_stuff
if not cnxn.autocommit:
cnxn.commit()
cursor.execute("select a from tbl where b=? and c=?", (x, y))
for row in cursor.execute("select user_id, user_name from users"):
print(row.user_id, row.user_name)
row = cursor.execute("select * from tmp").fetchone()
rows = cursor.execute("select * from tmp").fetchall()
count = cursor.execute("update users set last_logon=? where user_id=?", now, user_id).rowcount
count = cursor.execute("delete from users where user_id=1").rowcount
cursor.execute("select user_name from users where user_id=?", userid)
row = cursor.fetchone()
if row:
print(row.user_name)
cursor.execute("select user_id, user_name from users where user_id < 100")
rows = cursor.fetchall()
for row in rows:
print(row.user_id, row.user_name)
for row in cursor.tables():
print(row.table_name)
# Does table 'x' exist?
if cursor.tables(table='x').fetchone():
print('yes it does')
# columns in table x
for row in cursor.columns(table='x'):
print(row.column_name)