- # -*- coding: UTF-8 -*-
- import dbi, odbc
- db = odbc.odbc("dsn=data;uid=sa;pwd=master")
- print("登入成功")
- print('db:')#,type(db))
- c = db.cursor()
- print('c:')#,type(c))
- query="SELECT * FROM TESTTABLE"
- c.execute (query)
- rs = c.fetchall()# see also fetchone() and fetchmany()
- c.close()
- # looping over the results
- for r in rs:
- print(r)
- print('行的类型',type(r))
- for col in r:
- print(col,type(col))
- #print the name of column 0 of the result set
- print ('描述',c.description[0][0])
- #print the type, length, precision etc of column 1.
- print (c.description[1][1:5])
- db.close()
# -*- coding: UTF-8 -*-
import dbi, odbc
db = odbc.odbc("dsn=data;uid=sa;pwd=master")
print("登入成功")
print('db:')#,type(db))
c = db.cursor()
print('c:')#,type(c))
query="SELECT * FROM TESTTABLE"
c.execute (query)
rs = c.fetchall()# see also fetchone() and fetchmany()
c.close()
# looping over the results
for r in rs:
print(r)
print('行的类型',type(r))
for col in r:
print(col,type(col))
#print the name of column 0 of the result set
print ('描述',c.description[0][0])
#print the type, length, precision etc of column 1.
print (c.description[1][1:5])
db.close()
NND搞了一晚上,终于搞出来了,记录一下:
pywin32连接SQLServer,成功!
步骤:
1.首先建立ODBC数据源 odbcad32
2.使用pywin32的ODBC连接数据源,怎样操作数据库这里就不费口舌了。注意要写上用户名和密码,不然会报错:
dbi.opError: [Microsoft][SQL Server Native Client 10.0][SQL Server]用户 '' 登录失败。 in LOGIN