SQL Server Connecting to SQL db with ADODB

x3naxklr  于 2023-11-16  发布在  其他
关注(0)|答案(1)|浏览(119)

I am trying to connect to a SQL database with ADODB connection. I am not getting an error with my code, however nothing is populating. Can anyone advise what I may be doing wrong? thanks!

Also I'm trying to use adodbapi as I am seeing a lot of samples using this package, however I am unable to install on terminal and python packages screen

`import win32com.client

def ado():
    conn = win32com.client.Dispatch(r'ADODB.Connection')
    DSN = ('Provider=SQLOLEDB.1; DATA SOURCE =' +myserver+';Initial Catalog='+mydatabase+';UID='+user+';pwd='+password)
    conn.Open(DSN)

    rs=win32com.client.Dispatch(r'ADODB.Recordset')
    strsql = "SELECT * FROM DocumentInvoiceDetail"
    rs.open(strsql, conn)
    t = rs.GetRows()
    conn.close()
    return t`

I've also tried to use pyodbc method and getting stuck with an error "Data source name not found and no default driver specified

import pyodbc
cnxn = pyodbc.connect('DRIVER={Devart ODBC Driver for SQL Server};Server=myservername;Database=databasename;Port=port;User ID=user;Password=pw')

EDIT: I was able to resolve utilizing the code below, looks like changing the driver to SQL Server did the trick thanks!

import pyodbc
from pprint import pp
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=server;DATABASE=database;UID=user;PWD=pw')
cursor = cnxn.cursor()
mpgws1up

mpgws1up1#

I was able to resolve utilizing the code below, looks like changing the driver to SQL Server did the trick thanks!

import pyodbc
from pprint import pp
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=server;DATABASE=database;UID=user;PWD=pw')
cursor = cnxn.cursor()

相关问题