I'm trying to connect my SQL database with Python. I used:
import pyodbc
import pandas as pd
cnxn_str = ("Driver={SQL Server};"
"Server=server_name;"
"Database=database_name;"
"Trusted_Connection=yes;"
)
cnxn = pyodbc.connect(cnxn_str)
cursor = cnxn.cursor()
data = pd.read_sql("select * from dbo.table", cnxn)
But I get this error:
DatabaseError: Execution failed on sql Select ... : ('HY000', '[HY000] [Microsoft][ODBC SQL Server Driver]
Related
I am trying to pull data from SQL server, I am able to pull the sample data ( up to top 1000 records) but when I run it for all records I get following error:
('01000', '[01000] [Microsoft][ODBC SQL Server
Driver][DBNETLIB]ConnectionWrite (send()). (10054) (SQLGetData);
[01000] [Microsoft][ODBC SQL Server Driver][DBNETLIB]General network
error. Check your network documentation. (11)')
I am using following syntax
import pyodbc
conn = pyodbc.connect('Driver={SQL Server};'
'Server=g8w00761s.inc.hpicorp.net;'
'Database=ContraDigital2.0;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
sql_query = pd.read_sql_query("""SELECT top 1000 * FROM [ContraDigital2.0].[dbo].[contra_anomaly_upfront_backend_1]""",conn)
print(sql_query)
I have connected SQL Server with Python using pyodbc module. The script seems to have run correctly, however, I am getting errors when I try to execute my SQL statement.
This is what I have done:
import pandas
import numpy
import pyodbc
conn = pyodbc.connect(
'Driver={SQL Server};'
'Server=test\SQLEXPRESS;'
'Database=test1;'
'Trusted_Connection=yes;'
)
cursor = conn.cursor()
def read(conn):
print("Read")
cursor = conn.cursor()
cursor.execute("select * from table")
for row in cursor:
print(f'row = {row}')
print()
read(conn) #to execute
I am wanting to execute a query that I would normally run within my SQL Server, but in Python:
SELECT * FROM table
This is the error:
ProgrammingError: ('42S02', "[42S02] [Microsoft][ODBC SQL Server Driver][SQL
Server]Invalid object name 'Node'. (208) (SQLExecDirectW)")
I am actively researching this.
Try this:
def read(conn):
print("Read")
cursor = conn.cursor()
cursor.execute("select * from table")
allrows = cursor.fetchall()
for row in allrows:
print(f'row = {row}')
print()
Hi I'm using sql server V17.3 on Micorsoft Azure platform. I'm trying to upload data from python 3.7 data frame to my table test on sql server. So I wrote following code
import time
start_time = time.time()
import pyodbc
from sqlalchemy import create_engine
import urllib
dataToUpload=pd.read_csv("intermediate.csv")
params = urllib.parse.quote_plus(r'DRIVER=SQLServer};SERVER=nesbaexplsql001.database.windows.net;DATABASE=mydatabase;Trusted_Connection=False;Encrypt=True;uid=myuid;pwd=my password')
conn_str = 'mssql+pyodbc:///?odbc_connect={}'.format(params)
engine = create_engine(conn_str)
dataToUpload.to_sql(name='test',con=engine, if_exists='append',index=False)
But I'm getting error message
DBAPIError: (pyodbc.Error) ('HY000', '[HY000] [Microsoft][ODBC SQL Server Driver][SQL Server]Windows logins are not supported in this version of SQL Server. (40607) (SQLDriverConnect); [HY000] [Microsoft][ODBC SQL Server Driver][SQL Server]Windows logins are not supported in this version of SQL Server. (40607)') (Background on this error at: http://sqlalche.me/e/dbapi)
While executing to_sql. I also tried by putting Trusted_Connection=yes & removing Encryption=True.But I got same error. Can you guide me to resolve this issue?
Please refer to my sample code:
import pyodbc
import csv
server = 'tcp:***.database.windows.net'
database = '***'
username = '***'
password = '***'
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute("select * from ***")
row = cursor.fetchone()
while row:
print(row[0])
row = cursor.fetchone()
mycsv = r'D:\insert.csv' # SET YOUR FILEPATH
with open (mycsv, 'r') as f:
reader = csv.reader(f)
columns = next(reader)
query = 'insert into <TABLE NAME>({0}) values ({1})'
query = query.format(','.join(columns), ','.join('?' * len(columns)))
cursor = cnxn.cursor()
for data in reader:
cursor.execute(query, data)
cursor.commit()
Add one piece to your connection string:
Integrated Security = False;
Hope this helps.
I am using the following code to extract data from SQL into a Data Frame and it works fine.
import pyodbc
cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
"Server=DESKTOP-5422GFU;"
"Database=Python_Data;"
"Trusted_Connection=yes;"
"uid=User;pwd=password")
df = pd.read_sql_query('select * from Persons', cnxn)
df
But when I add this line
df.to_sql('test', schema = 'public', con = cnxn, index = False,
if_exists = 'replace')
to send data back to the Server I get an error that says
DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': ('42S02', "[42S02] [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid object name 'sqlite_master'. (208) (SQLExecDirectW); [42S02] [Microsoft][SQL Server Native Client 11.0][SQL Server]Statement(s) could not be prepared. (8180)")
I have tried a variety of solutions and just can't get it to work.
I believe the issue is in your "con = cnxn" statement. Try this:
import pandas as pd
import sqlalchemy
from sqlalchemy import create_engine
OBDC_cnxn='Python_Data_ODBC' #this will be the name of the ODBC connection to this database in your ODBC manager.
engine = create_engine('mssql+pyodbc://'+ODBC_cnxn)
df.to_sql('test', schema = 'public', con = engine, index = False,
if_exists = 'replace')
I'm trying to fetch data from SQL database with pyodbc with the code given below.The connection works rarely, most of the time it gives the error,
OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC SQL Server
Driver]Login timeout expired (0) (SQLDriverConnect)')
import numpy as np
import pyodbc as odbc
conn_string = ('DRIVER={SQL Server};SERVER=test;DATABASE=DEV;UID=me;PWD=whatever;')
cnxn = odbc.connect(conn_string)
cursor = cnxn.cursor()
cursor.execute("Select * from PurchaseOrders")
rows = cursor.fetchall()
ID = [i[1] for i in rows]
ID_array = np.fromiter(ID, dtype= np.int32)
I have tried setting the timeout to zero and DRIVER={ODBC Driver 11 for SQL Server} as I'm using SQL Server 2014. None of these works.
There was a problem with DNS. I used the IP address of the server instead, works fine now.