10
def insert(self):
    conn = pyodbc.connect(
        'Driver={SQL Server};'
        'Server=DESKTOP-S0VG212\SQLEXPRESS;'
        'Database=MovieGuide;'
        'Trusted_Connection=yes;'
    )
    cursor = conn.cursor()

Error occurs when executing the query but I don't know what's causing it.

cursor.execute('insert into Movies(MovieName,Genre,Rating,Username) values(?,?,?,?);',
               (self.moviename, self.moviegenre, self.ratebox, self.username))
conn.commit()
7
  • Can you provide all of error massage?
    – shimo
    Jan 2, 2020 at 12:42
  • File "C:/Users/Dell/PycharmProjects/untitled/gui.py", line 47, in insert cursor.execute('INSERT INTO Movies(MovieName,Genre,Rating,Username) VALUES(?,?,?,?);', SystemError: <class 'pyodbc.Error'> returned a result with an error set Jan 2, 2020 at 12:43
  • Thanks. I think (?,?,?,?); is strange and part of (self. .... ) should be inside '. So, cursor.execute('insert ...... username) ; ' ) will work.
    – shimo
    Jan 2, 2020 at 13:05
  • @shimo I can't just write variable names in query right ? , that will save the variable names in the database instead of the values stored in variables Jan 2, 2020 at 13:14
  • You are right, sorry. But I'm not sure ; is needed.
    – shimo
    Jan 2, 2020 at 13:28

3 Answers 3

4

I know my answer is late, but it can be useful to someone.

SystemError: <class 'pyodbc.Error'> returned a result with an error set error appears when the query is wrong, make sure you are executing the correct query using SQL server query window then you can able to identify the problem.

In the question, the semicolon should not come to the end of the query, if you still get an error, it might chances for the column to have some constraint issue. So follow the below method when you are facing this issue.

Execute one insert query in the SQL server query tab and identify the problem.

1
  • 1
    I get this error even when the query is perfectly valid, but the table already exists when I execute a 'CREATE TABLE' query without dropping the existing table. This exception did not include an ORA-00955 like I expected, and it later causes a UnicodeDecodeError, which threw me off for a couple hours while debugging.
    – Excel Help
    Apr 28, 2021 at 13:29
0

Using SQL Server with an Openquery to an Oracle Server I got this error because I had a comment in my Oracle SQL.

SET NOCOUNT ON;
If(OBJECT_ID('tempdb..#Temp_OracelTable') Is Not Null)
Begin
    Drop Table #Temp_OracleTable
    End


SELECT *
    INTO #Temp_OracleTable
    FROM    OPENQUERY (OracelConnectionNameOnSQLServer, '
Select Column -- Comment I had to remove
From TableName')
Select * From #Temp_OracleTable
Drop table #Temp_OracleTable
0

If you're having this problem with Apache Airflow, you should look for 2 possibles causes:

  • If you're calling your function inside the file, Airflow doesn't deal with it.

  • If your query string is correct with you default encoding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.