Доступ к серверу средствами ODBC, ставим пакет pyodbc
Примеры использования:
def sqlBackup(nHost,nBase,nFileName):
try:
connection = pyodbc.connect(driver='SQL Server Native Client 10.0',host=nHost,trusted_connection='yes',autocommit=True)
cursor = connection.cursor()
cursor.execute("BACKUP DATABASE "+nBase+" TO DISK = '"+nFileName+"' with INIT")
except Exception as e:
print e
raise
def sqlconnect(nHost='localhost',nBase='',nUser='',nPasw=''):
try:
return pyodbc.connect('DRIVER={SQL Server};SERVER='+nHost+';DATABASE='+nBase+';UID='+nUser+';PWD='+nPasw)
except:
return ''
def main():
con = sqlconnect(serv,base,user,pasw)
if not con:
print 'Error connect to db'
return
try:
cursor = con.cursor()
res = cursor.execute('select 1+1')
row = res.fetchone()
while row:
print row
row = res.fetchone()
except:
print 'Error query to sql'
for i in row:
if type(i) == str: i = i.decode('cp1251').strip()
elif type(i) == int: i = str(i)
else: i = str(i).decode('utf-8').strip()
print i