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

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()

猜你喜欢

转载自blog.csdn.net/www_rsqdz_net/article/details/79675311