python:Data insertion error in SQL Server - python

I have data that I need to insert into SQL Server in a list
values = [('Hello', 'McDonald'), ('Hi', 'Jennifer'), ('Ola', 'Janice'),('Hey', 'Bob')]
And I' trying to insert this using the following command:
columns_list_template = ','.join('[%s]'*len(noofcolumns)) # In this case 'noofcolumns' = 2
sql = 'INSERT INTO test VALUES ({0})'.format(columns_list_template)
cursor.execute(sql,values)
And I get the following error when I execute the code:
pypyodbc.ProgrammingError: ('HY000', 'The SQL contains 0 parameter markers, but 10 parameters were supplied')
I'm really stuck and don't know where there is an error.
PS. The list can contain more than 100000 records also. It's very dynamic
EDIT:Thank you everyone for trying to help me out. I had different datatypes defined in source and destinations and hence the problem. Couldn't have found it out myself. Thanks for always helping me out

Related

Python/Pyodbc/SQL - Updating a table and setting a field to a CSV File

I am trying to use pyodbc to update an existing MS Access database table with a very long multiline string. The string is actually a csv that has been turned into a string.
The query I am trying to use to update the table is as follows:
query = """
UPDATE Stuff
SET Results = '{}'
WHERE AnalyteName =
'{}'
""".format(df, analytename)
The full printed statement looks as follows:
UPDATE Stuff
SET Results =
'col a,col b,col c,...,col z,
Row 1,a1,b1,c1,
...,...,...,...,
Row 3000,a3000,b3000,c3000'
WHERE AnalyteName = 'Serotonin'
However this does not seem to be working, and I keep getting the following error:
pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC Microsoft Access Driver] Syntax error in UPDATE statement. (-3503) (SQLExecDirectW)')
Which I assume is due to the format of the csv string I am trying to use to update the table with.
I have tried using INSERT and inserting a new row with the csv string and other relevant information and that seems to work. However, I need to use UPDATE as I will eventually be adding other csv strings to these columns. This leads me to believe that there is A) Something is wrong with the syntax of my UPDATE query (I am new to SQL syntax) or B) I am missing something from the documentation regarding UPDATE queries.
Is executing an UPDATE query like this possible? If so, where am I going wrong?
It would be determined by the table's field type.
For large amounts of text you'd need a blob field in your database table.
A blob field will store binary info so using blob will not 'see' illegal characters.
Answering my own question in case anyone else wants to use this.
It turns out what I was missing was brackets around the table column fields from my UPDATE statement. My final code looked something like this.
csv = df.to_csv(index=False)
name = 'some_name'
query = """
UPDATE Stuff
SET
[Results] = ?
WHERE
[AnalyteName] = ?
"""
self.cursor.execute(query, (csv, name))
I've seen several other posts here where brackets were not around the column names. However, since this is MS Access, I believe they were required for this query, or rather this specific query since it included a very long strong in the SET statement.
I welcome anyone else here to provide a more efficient method of performing this task or someone else who can provide more insight into why this is what worked for me.

Parameter error for pyodbc insert/apppend

I am trying to insert/append into access a dataframe using pyodbc. However; when I run the code, I get an error: ProgrammingError: ('The SQL contains 21 parameter markers, but 1 parameter were supplied', 'HY000')
my sample code is: for row in tDjango: cursor.execute( 'INSERT INTO TDjango (Eid, Eventtype, Location, Lat, Lon, Created, TMCClosed,FirstArrival(min), PatrolArrival(min), TowArrival(min), LanesCleared(min), RoadwayCleared(min),Camera, DayofWeekOpened, DayofWeekClosed, sameDay, confirmClosed, confirmFirstAr, confirmPtrl, confirmTow, confirmLnClear) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',tDjango) conn.commit()
I’m not entirely sure what I am missing in the SQL statement to make the error go away.
Instead of using cursor.execute(), I used cursor.executemany().

How can I resolve this SQLAlchemy error regarding SQL parameters?

I am passing a very simple SQL query to a server via Python's SQLAlchemy library. Here is my code:
def query4():
ed_notes = sa.Table("ED_NOTES_MASTER",metadata,autoload=True,autoload_with=engine)
note_query = sa.select([ed_notes.columns["PT_FIN"],
ed_notes.columns["RESULT_TITLE_TEXT"],
ed_notes.columns["RESULT"],
ed_notes.columns["RESULT_DT_TM"]]).where(ed_notes.columns["PT_FIN"].in_(unique_fins)).where(start_time<ed_notes.columns["RESULT_DT_TM"]).where(end_time>ed_notes.columns["RESULT_DT_TM"])
result = connection.execute(note_query)
resultset = result.all()
note_data_prelim = pd.DataFrame(resultset)
return note_data_prelim
the variable "unique_fins" is a list of over 50,000 unique identifiers that I am trying to specifically query. When this query is run, the following error results:
sqlalchemy.exc.ProgrammingError: (pyodbc.ProgrammingError) ('The SQL contains -11872 parameter markers, but 53664 parameters were supplied', 'HY000')
Any ideas on what is going on? The problem is definitely arising in the ed_notes.columns["PT_FIN"].in_(unique_fins) portion of the query.
Thanks in advance!
The in_ operator will convert your unique_fins list into a single parameter per item. You'll likely hit parameter limits on your underlying database.
Referring to this answer here you could do something like this;
.where(ed_notes.columns["PT_FIN"] == func.any_(unique_fins))

Python Dataframe to Hana Table

I have my dataframe in python environment, which i want to push it to Hana from python environment.
I am trying to do line by line pushing to Hana, that's not happening.However if there a way to push full dataframe to hana in one go, thats the best.
However, for now i am not able to push dynamically the values of dataframe ,line by line:
Here is the python code i tried, so far in best But unfortunately not working :( :
cursor = conn.cursor()
cursor.execute('CREATE TABLE DS.BM_TEXT("Var_ID" VARCHAR (255),"Start_Date"
varchar(255),"End_Date" varchar(255),"ID" varchar(255),"Text" varchar(255))')
conn = dbapi.connect(address="hana1345.lab1.abc.com", port=30015, user='SHEEMAZ',
password='Hello1')
sql_insert_query = """ INSERT INTO DS.BM_TEXT VALUES (%s,%s,%s,%s,%s)"""
insert_tuple_2 = ("2", "Emma", "2019-05-19", "9500","22")
cursor.execute(sql_insert_query, insert_tuple_2)
Error i am getting is :
ProgrammingError: (257, 'sql syntax error: incorrect syntax near "%": line 1 col 53 (at pos 53)')
Appreciate all help.
I am not positive on what module you are using for your db api. But usually ? is the placeholder. Without explicitly calling .format on your string it may not actually insert your sql_insert_query into the string. I could be wrong but I am guessing that is the problem.
As for sending everything at once; it can be done with executemany(). You need an iterable structured like this:
insert_list = [("2", "Emma", "2019-05-19", "9500","22"),("3", "Smith", "2019-05-19", "9500","22")]
To send it to the database use this query:
cursor.executemany("""INSERT INTO DS.BM_TEXT VALUES (?,?,?,?,?);""", insert_list)
This will put the whole iterable into the table. It still does it line by line I believe, but does the heavy lifting for you. If your dataframe is not structured like this you could create an iterable class/function that yields the data in that format from your df.

creating and calling stored procedure, passing values into stored procedure from python

i am a novice at python and sql so i have encounterd some issue while trying to create a stored procedure and passing values to it.
i have looked through few questions here.Of what i could understand i used the ODBC format and it works for procedures not requiring anything to be passed. The query is simple. it takes in 2 parameters route_no and month. the query then count the number of trios taken by a bus per date for that month on that route.
if __name__ == "__main__":
conn=pyodbc.connect('''Driver={SQL Server};'''
'''Server=ABY;'''
'''Database=testing;'''
'''Trusted_Connection=yes;''')
cursor=conn.cursor()
proc3="""CREATE PROCEDURE TRIPS_PER_DAY #ROUTENO NVARCHAR(30),#MONTH NVARCHAR(30)
AS
BEGIN
SELECT [ON DATE],SUM(DISTINCT([TRIP NO])) AS SUM_TRIPS
FROM testing.dbo.sheet1$
WHERE [ROUTE NO#]=#ROUTENO AND MONTH([ON DATE])=#MONTH
GROUP BY [ON DATE]
END"""
drop_proc3="""IF EXIST(SELECT * FROM SYS.OBJECTS WHERE TYPE='P' AND NAME='TRIPS_PER_DAY') DROP PROCEDURE TRIPS_PER_DAY"""
call_proc3="""{CALL testing.dbo.TRIPS_PER_DAY(?)(?)}"""
cursor.execute(drop_proc3)
cursor.execute(proc3)
values=('16','11-11-2017')
dff=pd.read_sql(call_proc3,conn,params=(values,))
print(dff)
this is the error i am getting:
DatabaseError: Execution failed on sql '{CALL testing.dbo.PASS_PER_STOP(?)(?)}': ('42000', '[42000] [Microsoft][ODBC SQL Server Driver]Syntax error or access violation (0) (SQLPrepare)')
i am not sure if the method i am using is right or if there is a syntax error.
I am also not sure how to pass values and return results from the procedure using python. Any help?. Thanks in advance!!!

Categories