I am trying to use pyodbc (with Python 2.7) to call a stored procedure to insert records into a SQL Server 2012 table. I am passing a temporary table.
I dumped out my sql and when executed through the SQL Server Management console, it generated the following Foreign Key error:
Msg 547, Level 16, State 0, Procedure spInsertBondTickerValues, Line 26
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__BondTickerValue__756D6ECB".
The conflict occurred in database "QuantDev", table "dbo.Tickers".
The statement has been terminated.
However, pyodbc did not raise an exception. How would I test the resulting cursor or connection to know that a problem occurred, and how do I get the error message?
Thank you very much.
EDIT Here is the full sql text:
DECLARE #rawTbl [dbo].TickerValueTableType
INSERT INTO #rawTbl (Ticker, BBName, LastValue, ValueTime, SourceDescr) VALUES
('IBM', 'Equity', 179.230000, '2013-11-01 00:00:00.000000', 'Bloomberg'),
('SPX', 'Index', 1803.710000, '2013-12-10 00:00:00.000000', 'Bloomberg')
EXEC [dbo].spInsertBondTickerValues #rawTbl
EDIT 2 Here is the relevant Python code:
def execSQLwithCommit(self, sql):
cursor = self.conn.cursor()
cursor.execute(sql)
self.conn.commit()
where the connection has been previously made via
self.conn = pyodbc.connect(app = appName,
driver = '{SQL Server Native client 11.0}',
server = server,
database = db,
Trusted_Connection = 'yes')
I was able to recreate your issue using the following code, which fails silently:
import pyodbc
cnxn = pyodbc.connect('DSN=myDb;')
cursor = cnxn.cursor()
sql = """
DECLARE #rawTbl dbo.ClientAddressInputType;
INSERT INTO #rawTbl (ClientID, Addr1) VALUES
(2, 'higgy'),
(3, 'jiggy');
EXEC dbo.AddClientAddress #rawTbl
"""
cursor.execute(sql)
cursor.commit()
cnxn.close()
However, I can get the code to throw the appropriate IntegrityError exception by simply adding SET NOCOUNT ON; at the beginning of the sql string:
import pyodbc
cnxn = pyodbc.connect('DSN=myDb;')
cursor = cnxn.cursor()
sql = """
SET NOCOUNT ON;
DECLARE #rawTbl dbo.ClientAddressInputType;
INSERT INTO #rawTbl (ClientID, Addr1) VALUES
(2, 'higgy'),
(3, 'jiggy');
EXEC dbo.AddClientAddress #rawTbl
"""
cursor.execute(sql)
cursor.commit()
cnxn.close()
which results in
Traceback (most recent call last):
File "C:\Users\Gord\Desktop\pyOdbc.py", line 12, in <module>
cursor.execute(sql)
IntegrityError: ('23000', '[23000] [Microsoft][SQL Server Native Client 10.0][SQL Server]The INSERT statement conflicted with the FOREIGN KEY constraint "FK_ClientAddresses_Clients". The conflict occurred in database "myDb", table "dbo.Clients", column \'ClientID\'. (547) (SQLExecDirectW); [01000] [Microsoft][SQL Server Native Client 10.0][SQL Server]The statement has been terminated. (3621)')
Related
I have a sql file generated during database backup process and I want to load all database content from that sql file to a different MySQL database (secondary database).
I have created a python function to load the whole database in that sql file but when I execute the function, I get an error
'str' object is not callable
Below is python script
def load_database_dump_to_secondary_mysql(file_path='db_backup_file.sql'):
query = f'source {file_path}'
try:
connection = mysql_hook.get_conn() # connection to secondary db
cursor = connection.cursor(query)
print('LOAD TO MYSQL COMPLETE')
except Exception as xerror:
print("LOAD ERROR: ", xerror)
NB: mysql_hook is an airflow connector that contains MySQL DB connection info such as Host, user/passwd, Database name. Also, I don't have connection to the primary database, I'm only receiving sql dump file.
What I'm I missing?
source is a client builtin command: https://dev.mysql.com/doc/refman/8.0/en/mysql-commands.html
It's not an SQL query that MySQL's SQL parser understands.
So you can't execute source using cursor.execute(), because that goes directly to the dynamic SQL interface.
You must run it using the MySQL command-line client as a subprocess:
subprocess.run(['mysql', '-e', f'source {file_path}'])
You might need other options to the mysql client, such as user, password, host, etc.
try this
import mysql.connector as m
# database which you want to backup
db = 'geeksforgeeks'
connection = m.connect(host='localhost', user='root',
password='123', database=db)
cursor = connection.cursor()
# Getting all the table names
cursor.execute('SHOW TABLES;')
table_names = []
for record in cursor.fetchall():
table_names.append(record[0])
backup_dbname = db + '_backup'
try:
cursor.execute(f'CREATE DATABASE {backup_dbname}')
except:
pass
cursor.execute(f'USE {backup_dbname}')
for table_name in table_names:
cursor.execute(
f'CREATE TABLE {table_name} SELECT * FROM {db}.{table_name}')
I am trying to use Python and SQL Server OPENROWSET to import data from an Excel file into a SQL Server temp table. The following code works fine if I import the data into a permanent table using OPENROWSET, but I receive the following SQL Server error when attempting to insert into a temp table. I want to be able to do this with temp tables on servers for which I don't have access to create permanent tables. Any help is greatly appreciated.
('42000', '[42000] [Microsoft][SQL Server Native Client 11.0][SQL
Server]The OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked
server "(null)" reported an error. The provider did not give any
information about the error. (7399) (SQLExecDirectW); [42000]
[Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot
initialize the data source object of OLE DB provider
"Microsoft.ACE.OLEDB.12.0" for linked server "(null)". (7303)')
Python 3.7
PyODBC is up to date
SQL Server 2019
cnx = pyodbc.connect (
"Driver={SQL Server Native Client 11.0};"
"SERVER=SERVERNAME;"
"Database=master;"
"Trusted_Connection=yes;"
"cnx.autocommit = True;"
)
cursor1 = cnx.cursor()
query1 = ('''set nocount on;''')
query2 = ('''if object_id ('tempdb..##TABLE1') is not null drop table ##TABLE1;''')
query3 = ('''
select
cast([Column1] as bigint) Column_1
,cast([Column2] as varchar(255)) Column_2
--etc for all columns in excel file
into ##TABLE1
FROM OPENROWSET(
'Microsoft.ACE.OLEDB.12.0',
'Excel 12.0 Xml;
HDR=YES;
IMEX=1;
Database=F:\Filename.xlsx',
[sheetname$]);
''')
try:
cursor1.execute(query1)
cursor1.execute(query2)
cursor1.execute(query3)
except Exception as e2:
print(e2)
finally:
cursor1.close()
cnx.commit()
cnx.close()
I am trying to execute stored procedure by using pyodbc in databricks, after executing SP I tried to commit the connection but, commit is not happening. Here I am giving my code, please help me out from this issue.
import pyodbc
#### Connecting Azure SQL
def db_connection():
try:
username = "starsusername"
password = "password-db"
server = "server-name"
database_name = "db-name2"
port = "db-port"
conn=pyodbc.connect('Driver={ODBC Driver 17 for SQL server};SERVER=tcp:'+server+','+port+';DATABASE='+ database_name +';UID='+ username +';PWD='+ password)
cursor=conn.cursor()
return cursor, conn
except Exception as e:
print("Faild to Connect AZURE SQL: \n"+str(e))
cursor, conn = db_connection()
# conn1.autocommit=True
cursor.execute("delete from db.table_name")
cursor.execute("insert into db.table_name(BUSINESS_DATE) values('2021-10-02')")
cursor.execute("exec db.SP_NAME '20211023'")
conn.commit()
conn.close()
here I am commiting connection after SP excution. deletion and insertion is not happening at all. and I tried with cursor.execute("SET NOCOUNT ON; exec db.SP_NAME '20211023'") but it's also not working.
Thanks in Advance
If you check this document on pyodbc, you will find that -
To call a stored procedure right now, pass the call to the execute method using either a format your database recognizes or using the ODBC call escape format. The ODBC driver will then reformat the call for you to match the given database.
Note that after connection is set up or done, try doing conn.autocommit = True before calling your SP and it will help. By default it is false.
Executing the Stored Procedure.
You will be able to execute your stored procedure if you follow the below code snippet.
cursor = conn.cursor()
conn.autocommit = True
executesp = """EXEC yourstoredprocedure """
cursor.execute(executesp)
conn.commit()
Delete the Records in SQL Server
You can delete record as shown in the below example.
...#just an example
cursor.execute('''
DELETE FROM product
WHERE product_id in (5,6)
''')
conn.commit()
Don’t forget to add conn.commit() at the end of the code, to ensure that the command would get executed.
Insert record in SQL Server
The below snippet show how we can do the same.
...#just an example
cursor.execute("INSERT INTO EMP (EMPNO, ENAME, JOB, MGR) VALUES (535, 'Scott', 'Manager', 545)")
conn.commit()
I will suggest you to read the for following document for more information.
Delete Record Documentation.
Insert Record Document
Now I have created two different connection for example I have a .py file called connec as the below code:
from sqlalchemy import create_engine
import pyodbc
# pyodbc connection connect to server
conn = pyodbc.connect(
"driver={SQL Server};server=WINKPN-3B5JTT2\SMARTRNO_EXPRESS; database=master; trusted_connection=true",
autocommit=True, Trusted_Connection='Yes')
crsr = conn.cursor()
# connect db (connect to database name) using SQL-Alchemy
engine = create_engine(
'mssql+pyodbc://WINKPN-3B5JTT2\SMARTRNO_EXPRESS/MyNewDatabase?driver=SQL+Server+Native+Client+11.0')
connection = engine.connect()
So now the first connection is related to the connection of the server, and the second one is the connection the database, the both connection works fine, but together I found the below error:
Traceback (most recent call last):
File "C:/Users/haroo501/PycharmProjects/ToolUpdated/app.py", line 16, in <module>
delete_and_create_db.delete_database()
File "C:\Users\haroo501\PycharmProjects\ToolUpdated\database\delete_and_create_db.py", line 9, in delete_database
connec.crsr.execute(delete_bd_query)
pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot drop database "MyNewDatabase" because it is currently in use. (3702) (SQLExecDirectW)')
When trying to run the below code:
from database import connec
def create_db():
create_bd_query = "CREATE DATABASE MyNewDatabase"
connec.crsr.execute(create_bd_query)
def delete_database():
delete_bd_query = "DROP DATABASE MyNewDatabase"
connec.crsr.execute(delete_bd_query
So I think there's a problem to use both connection or something, may be I put the two connection i def and call them when need, but I tried it doesn't work, Any one have an idea how to solve this?
This is my first question here. So, I am sorry if it is repeated or the formatting is off. I searched through other questions and the error is common but appears on multiple situations.
I have a very simple python code where I want to execute a procedure in MSSQL from pyodbc.
import pyodbc
conn = pyodbc.connect(r'DSN=myDSN')
cursor = conn.cursor()
query = r'{call myproc}'
cursor.execute(query)
I am using call instead of exec after reading that ODBC uses call for executing procedures in MSSQL.
The error I am getting is the following:
Traceback (most recent call last):
File "myscript.py", line 26, in <module>
cursor.execute(query)
pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]The current transaction has aborted, and any pending changes have been rolled back. Cause: A transaction in a rollback-only state was not explicitly rolled back before a DDL, DML or SELECT statement. (111233) (SQLExecDirectW)')
Thanks for the help
In case someone is having the same issue. I was able to find out what the problems was. When you open a connection with DSN, the autocommit is set to False. For some reason, this should be True for the code to work (this depends largely on what I was doing on MSSQL).
import pyodbc
conn = pyodbc.connect(r'DSN=myDSN', autocommit=True)
cursor = conn.cursor()
query = r'{call myproc}'
cursor.execute(query)
This runs well!
Here are two examples on how you can execute a stored proc in MS SQL Server through pyodbc:
Passing a NULL, and the VARCHAR 'tallen' as positional parameter variables:
cursor.execute('EXEC usp_get_user_data ?, ?', None, 'flipperpa')
Passing two VARCHAR values as named parameter variables:
cursor.execute('EXEC usp_get_user_data #user_full_name = ?, #user_username = ?', 'flip', 'flipperpa')
Then to loop through the returned data:
rows = cursor.fetchall()
for row in rows:
# Do stuff
print(row.user_id)
Good luck!
I had issue with executing the Stored procedure using the SQL server 2008. The first step I did is to go to control panel > Administrative tools > Data Sources (ODBC) > add the driver
The only change I made in my python code is use "{call procdev_2017}". I got an error when I tried using exec instead of call
import pyodbc
conn = pyodbc.connect(driver='{SQL Server Native Client 11.0}', server='XXXXXXX', database='XXX',
trusted_connection='yes', autocommit=True)
bepcur = conn.cursor()
ipcur.execute("{call procdev_2017}")