forked from denisenkom/pytds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.py
37 lines (33 loc) · 1.2 KB
/
shell.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
# simple interactive shell for MSSQL server
import pytds
import os
def main():
conn = pytds.connect(dsn=os.getenv("HOST", "localhost"),
user=os.getenv("SQLUSER", "sa"),
password=os.getenv("SQLPASSWORD"),
cafile="/Users/denisenk/opensource/pytds/ca.pem",
enc_login_only=True)
while True:
try:
sql = input("sql> ")
except KeyboardInterrupt:
return
with conn.cursor() as cursor:
try:
cursor.execute(sql)
except pytds.ProgrammingError as e:
print("Error: " + str(e))
else:
for _, msg in cursor.messages:
print(msg.text)
if cursor.description:
print('\t'.join(col[0] for col in cursor.description))
print('-' * 80)
count = 0
for row in cursor:
print('\t'.join(str(col) for col in row))
count += 1
print('-' * 80)
print("Returned {} rows".format(count))
print()
main()