Python通过ODBC访问数据库的数据库模块



一、安装包
        安装pyodbc



二、使用方法
1、建立连接
cnxn  =  pyodbc.connect('DRIVER={ODBC  Driver  13  for  SQL 
Server};SERVER=XXXXXXX;DATABASE=test17;UID=test;PWD=my')
cursor  =  cnxn.cursor()



2、查询
>>>  cursor.execute('SELECT  TOP  10  Name,ask  from  dbo.T_test;')
  <  pyodbc.Cursor  object  at  0x00000256ED2C6A80>
>>>  cursor.execute('SELECT  TOP  10  Name,ask  from  T_test;')
  <  pyodbc.Cursor  object  at  0x00000256ED2C6A80>
>>> 
>>>  row  =  cursor.fetchone()
>>>  while  row:
...          X=str(row[0])  "  "  str(row[1])
...          print  (X)     
...          row  =  cursor.fetchone()
... 
abc                what

>>> 
3、插入
>>>  cursor.execute("insert  T_test  (Name,ask)  VALUES    ('hehe','ok') 
;")#必需外面是双引号!
>>>  cnxn.commit()
4、关闭连接
>>>  cnxn.close()

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信