Incorrect syntax near 's' in pyodbc - python

Sometimes I get this error randomly when I try to insert data in to the database. I fetch the data with using request.get and parsing the JSON data.
This is the error that I get:
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17
for SQL Server][SQL Server]Incorrect syntax near 's'. (102)
(SQLExecDirectW); [42000] [Microsoft][ODBC Driver 17 for SQL
Server][SQL Server]Unclosed quotation mark after the character string
')'. (105)")
This is my code:
for product in parsed['products']:
cursor.execute("INSERT INTO dbo.producten (productid, datum_toegevoegd, naam, prijs_excl, prijs_incl, gewicht) VALUES ('%s','%s','%s','%s','%s','%s')" %(product['id'],product['created_at'], product['nl']['title'],product['price_excl'],product['price_incl'],product['weight']))

You must not use string interpolation for SQL queries. The db-api will do correct parameter substitution for you - replace that % with a comma.
cursor.execute('SELECT.... ', (product['id'],product['created_at'...))
# ^

cursor.execute('SELECT.... product = ?', (value_for_product))
works for python 3.^

Related

Alter database query with variables using Python

I am trying to execute below SQL query by introducing 2 variables in Python but getting error,
Original Query:
ALTER DATABASE db1 MODIFY (SERVICE_OBJECTIVE = 'DW300');
I want to use variables for db1 and 'DW300'. I have tried below statement in Python and got error.
dwu = 'DW100'
sqlpool = 'db1'
cursor.execute("""ALTER DATABASE ? MODIFY (SERVICE_OBJECTIVE = ?) """, (sqlpool, dwu))
Error: pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near '#P1'. (102) (SQLExecDirectW)")
I use sqlite and it accepts this answe.
try the use of fstrings
f"""Alter Database {sqlpool} modify (service objective = {dwu}) """

Conversion error using pyodbc & CODEPAGE is not supported on the Linux

I am trying to save some data from file to database using pyodbc bulk insert.
I am getting a lot of these exceptions:
pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC Driver 17
for SQL Server][SQL Server]Bulk load data conversion error (type
mismatch or invalid character for the specified codepage) for row
90411, column 3 (ARTIST_NAME).
I guess it could be caused by some speical chars like %, ó, ś. I could not even point all of them cause there is over 10 milions lines in a file. Is there any way to get rid of this exception?
My bulk insert looks like this:
BULK INSERT someSchema.SomeTable FROM 'filePath'
WITH (
FIELDTERMINATOR='',
ROWTERMINATOR='\n'
);
I tried to add CODEPAGE = '65001' to this command but then I got
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Keyword or statement option 'CODEPAGE' is not supported on the 'Linux' platform. (16202) (SQLExecDirectW)")

SQLAlchemy Core - Subquery missing an alias

I was working on a straightforward SQLAlchemy Core (Python 3.x) count unique query using the following code:
table_object = sqlalchemy.Table(table_name, metadata, autoload=True, autoload_with=engine, schema=schema)
agg_fields = [get_column_correct_case(table_object, col) for col in agg_fields]
agg_col_obj = [table_object.c[col] for col in agg_fields]
agg_query = sqlalchemy.select(agg_col_obj).select_from(table_object)\
.group_by(*agg_fields).count()
engine.scalar(agg_query)
While I believe this should be valid SQLAlchemy Core code, its execution returned a PYODBC error:
(pyodbc.ProgrammingError) ('42000', "[42000] [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]
Incorrect syntax near ')'. (102) (SQLExecDirectW);
[42000] [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Statement(s) could not be prepared. (8180)")
The SQL query generated by this code which failed to execute properly was:
[SQL: SELECT count([ACCOUNT]) AS tbl_row_count
FROM (SELECT dbo.history.[ACCOUNT] AS [ACCOUNT], dbo.history.[RTXNNBR] AS [RTXNNBR]
FROM dbo.history
GROUP BY dbo.history.[ACCOUNT], dbo.history.[RTXNNBR])]
Running the query directly in SQL Server, it appears to be failing because the nested select is missing an alias. Is this a bug? If not, how do I fix this code?
I figured out the answer. I was surprised it was necessary, but this can be resolved by giving the query to be counted an alias.
So
agg_query = sqlalchemy.select(agg_col_obj).select_from(table_object)\
.group_by(*agg_fields).count()
becomes:
agg_query = sqlalchemy.select(agg_col_obj).select_from(table_object)\
.group_by(*agg_fields).alias('a').count()
which now works.

Getting incorrect syntax for SQL statement in python

When I run below code which incorporates TOP ? in a select statement and I am unable to resolve.
Code:
cnt = self.getCount()
#cnt = 2
query = "select distinct top ? id, indicator from [TABLE] ORDER BY id ASC"
connsql = self.sql_connection()
resultset = connsql.cursor().execute(query, cnt).fetchall()
connsql.cursor().commit()
connsql.close()
I get this syntax error:
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near '#P1'. (102) (SQLExecDirectW); [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared. (8180)")

Inserting Names with apostrophe to a Database Table using Python [duplicate]

This question already has answers here:
format single quote in SQL statement
(1 answer)
Python - format single quote in SQL statement PyODBC
(1 answer)
Closed 4 years ago.
I am encountering an error when trying to insert data into a database using pyodbc.
The error is as follows:
('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL
Server]Incorrect syntax near 'Anthony'. (102) (SQLExecDirectW);
[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Unclosed
quotation mark after the character string ')\n '. (105)")
The code I am currently using is:
msconn=pyodbc.connect(driver='{Test Server}',
server='Test\Test',
database='Test',
trusted_msconnection='yes')
cursor=msconn.cursor()
for index, row in Terms.iterrows():
I1 = 'COLUMNIDENTIFIER'
I2 = row['EID']
I3 = row['Legal Name']
insert_query = """
INSERT INTO Test.Table
VALUES ('{}','{}','{}')
""".format(I1,I2,I3)
cursor.execute(insert_query)
cursor.commit()
cursor.close()
msconn.close()
Checking the source file shows that the cause of the error is a name with an apostrophe. (I3)
Is there a way for me to upload the name with the "'"?
Thanks in advance.

Categories

Resources