0% found this document useful (0 votes)
13 views4 pages

Reliable PyODBC Inserts Plus Technical Documnets

Uploaded by

citota1835
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Reliable PyODBC Inserts Plus Technical Documnets

Uploaded by

citota1835
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

RELIABLE PyODBC Inserts

Overview
You read that right. How do you insert values into a table from Python using PyODBC in such
a way that you don't have to worry about certain characters messing up your insert
operation? Most of the time, things I was inserting to my SQL tables through my SQL API
written in FastAPI didn't have any problems. But occassionally, there'd be an apostrophe, or
some other character that'd give me fits. I just got tired of trying to deal with this, so I did
some deeper research, and I had forgotten a method of using PyODBC that handles this for
you.

My Recent Bad Way Of Doing SQL Inserts


For economy of explanation, I will not show all the code of my class and my API. I'll show
pretty spaghetti code. Meaning, it's not bad code, but I wouldn't usually code it this way.
However, for explaining the concepts, this will work well.

Below is the style of coding that I was using ... roughly.


In [ ]: import pyodbc

# Build a specific connection string


my_connection_string = 'DRIVER={SQL Server};SERVER=myserver;DATABASE=mydatabase;UID=myusername

# Build my SQL statement


my_data_dictionary = {"column_1": value_1, "column_2": value_2}

column_names = tuple(my_data_dictionary.keys())
values_tuple = tuple(my_data_dictionary.values())

sql_cmd = f"INSERT INTO mytable {column_names} VALUES {values_tuple}"

# Use a context manager to handle automatic closing of the cursor and connection
with pyodbc.connect(my_connection_string) as connection:
cursor = connection.cursor()
result = type(cursor.execute(sql_cmd))

if result is pyodbc.Cursor:
print("SUCCESS")
else:
print("FAILED")

The code above works great if there aren't any parameterization errors in your values. If
there are errors, cleaning them up can be TEDIOUS. Let's see how to let PyODBC handle
them.

My New MO BETTUH Way Of Inserting To SQL


In the next code block, the SQL command has ?'s for placeholders for the values to be
inserted into. The tuple of values are passed to the execute method. We NOW let the
PyODBC execute method handle the parameterization of the values. It ensures that special
characters, like apostrophes for example, are escaped properly.
In [ ]: import pyodbc

# Build a specific connection string


my_connection_string = 'DRIVER={SQL Server};SERVER=myserver;DATABASE=mydatabase;UID=myusername

# Build my SQL statement


my_data_dictionary = {"column_1": value_1, "column_2": value_2}

column_names = tuple(my_data_dictionary.keys())
values_tuple = tuple(my_data_dictionary.values())

question_marks_list = ["?" for _ in range(len(values_tuple))]


question_marks = ", ".join(question_marks_list)
place_holders = f"({question_marks})"

# Store Meta Data Table Values


sql_cmd = f"INSERT INTO mytable {column_names} VALUES {place_holders}"

# Use a context manager to handle automatic closing of the cursor and connection
with pyodbc.connect(my_connection_string) as connection:
cursor = connection.cursor()
result = type(cursor.execute(sql_cmd, values_tuple))

if result is pyodbc.Cursor:
print("SUCCESS")
else:
print("FAILED")

And the SQL command will look like ...

sql_cmd = "INSERT INTO mytable (column_1, column_2) VALUES (?, ?)"

Alternative Insert
IF you are inserting to ALL values of the table, you can also just use ...

In [ ]: sql_cmd = f"INSERT INTO mytable VALUES {place_holders}"

My Recent Experience
I REALLY wanted to solve the issues I was occasionally having with my automated SQL
inserts, and this solution NAILED IT! I wish I had remembered this sooner. I am hoping this
document will help MANY of you that might be experiencing the same thing, OR help
people new to SQL automation thru Python avoid these challenges in the first place.

Summary
I showed you a way to use ? type placeholders in a SQL command that is passed to PyODBC.
You also pass in your tuple of values to the cursor.execute method as shown above. Then
PyODBC appropriately handles the parameterizations of our values as needed without us
having to do so much heavy lifting of the same.

Until next time, Thom

You might also like