MSSQL in python 2.7

后端 未结 4 1261
囚心锁ツ
囚心锁ツ 2020-12-12 17:35

Is there a module available for connection of MSSQL and python 2.7?

I downloaded pymssql but it is for python 2.6. Is there any equivalent module for python 2.7?

4条回答
  •  天命终不由人
    2020-12-12 18:06

    Install pyodbc using pip as follows: pip install pyodbc

    import pyodbc
    cnxn = pyodbc.connect("DRIVER={SQL Server};SERVER=SOME-PC;DATABASE=my_db")
    cursor = cnxn.cursor()
    
    
    cursor.execute("insert into test_tb values(6, 'name')")
    
    cursor.execute("select id, name from my_tb")
    rows = cursor.fetchall()
    for row in rows:
        print row.id, row.name
    

    For details, see

    https://github.com/mkleehammer/pyodbc/wiki

提交回复
热议问题