Executing SQL Server Query in Python - python

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()

Related

DatabaseError: Execution failed on SQL Select

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]

SQL python Connection issue

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)

How to put pandas DataFrame using pyodbc in SQL Server Management Studio

With the pandas DataFrame called 'data' (see code), I want to put it into a table in SQL Server. How should I do this?
I read something on the internet with data.to_sql, so I tried a little with this function. However, it gives me an error message (see error message). It probably has to do with the argument con = conn. I don't know how to fix this. The error message says something about SQLite. I am not very advanced in using SQL, so I do not understand all errors.
Do you have any ideas? I think the conn is the only problem. The rest works fine.
Using SQL Server Management Studio 2018.
Ty in advance.
Error message:
Exception has occurred: DatabaseError
Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;':
('42S02', "[42S02] [Microsoft][ODBC SQL Server Driver][SQL Server]
Invalid object name 'sqlite_master'. (208) (SQLExecDirectW);
[42S02] [Microsoft][ODBC SQL Server Driver][SQL Server]
Statement(s) could not be prepared. (8180)")
def connection():
try:
#connect to network database
conn = None
conn = db.connect('Driver={SQL Server};'
'Server=xxsql.database.windows.net;'
'Database=xx;'
'uid=xx;'
'pwd=xx;')
#if connected do
if conn is not None:
print("The connection is established")
sql_query = 'select top 20 * from xx'
#read table into dataframe
#make a dataframe with records from table
dfObj = pd.read_sql_query(sql_query, conn)
#inspect result
print(dfObj)
#read into list
cursor = conn.cursor()
cursor.execute(sql_query)
#put cursor to list
cursor_list = list(cursor.fetchall())
#make copy so that we wont change original data
table = cursor_list.copy()
bibliography = Extract(table)
data = pd.DataFrame(bibliography)
#convert it to SQL
data.to_sql('ExtractedBib', con = conn, if_exists = 'append') #????
except db.Error as e:
print(e)
finally:
if conn is not None:
cursor.close()
conn.close()
print("The connection is closed")
else:
print("No connection established")
def main():
connection()
main()
Try using sqlalchemy instead.
import sqlalchemy
engine = sqlalchemy.create_engine(
"xxsql+pyodbc://uid:pwd#Server/Database",
echo=False)
...
data.to_sql('ExtractedBib', con=engine, if_exists = 'append')
Just change the template values into your database values (uid, pwd etc.)

Azure Windows logins are not supported in this version of SQL Server while connecting sql server using python

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.

"Unclosed quotation mark" when connecting with pypyodbc

I've the following piece of code in Python, to connect to SQL Server but it always fails, even though the same from sqlcmd works.
Any ideas?
import pypyodbc
conn = pypyodbc.connect(DRIVER='{SQL Server}',Trusted_Connection='yes',server='sqldev\dwmaster',DATABASE='risk')
cur = conn.cursor()
The error I'm getting is:
pypyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver
][SQL Server]Unclosed quotation mark after the character string ' '.")
Fixed the error, doing the following.
conn = pypyodbc.connect("DRIVER={SQL Server};server='sqldev\\master',Database='risk',TrustedConnection=yes")
cur = conn.cursor()

Categories