What's the simplest way to access mssql with python or ironpython?

前端 未结 8 2065
忘了有多久
忘了有多久 2020-12-04 16:56

I\'ve got mssql 2005 running on my personal computer with a database I\'d like to run some python scripts on. I\'m looking for a way to do some really simple access on the d

8条回答
  •  执念已碎
    2020-12-04 17:31

    pyodbc comes with Activestate Python, which can be downloaded from here. A minimal odbc script to connect to a SQL Server 2005 database looks like this:

    import odbc
    
    CONNECTION_STRING="""
    Driver={SQL Native Client};
    Server=[Insert Server Name Here];
    Database=[Insert DB Here];
    Trusted_Connection=yes;
    """
    
    db = odbc.odbc(CONNECTION_STRING)
    c = db.cursor()
    c.execute ('select foo from bar')
    rs = c.fetchall()
    for r in rs:
        print r[0]
    

提交回复
热议问题