Python Forum
Getting error when running "MINUS" between 2 databases
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting error when running "MINUS" between 2 databases
#1
Goal: Keep content of TableA except items listed on TableB.

I am getting the following error. Code is below. Can you please let me know if you see where problem is when using the "MINUS' syntax?
Error:
(venv) user@3c0630078aad venv % python delmain2.py Traceback (most recent call last): File "/Users/user/venv/delmain2.py", line 61, in <module> diff("apix.db", "APIX", "APIW") File "/Users/user/venv/delmain2.py", line 55, in diff res = connection_obj.execute(statement) sqlite3.OperationalError: near "SELECT": syntax error (venv) user@3c0630078aad venv %
import sqlite3
def diff(database, tableA, tableB):
        #Testing with hardcoded syntax for now

        connection_obj = sqlite3.connect(database)
        cursor_obj = connection_obj.cursor()
        statement = '''SELECT * FROM APIX MINUS SELECT * FROM APIW'''
        res = connection_obj.execute(statement)
        return res

diff("apix.db", "APIX", "APIW")
Reply
#2
(Nov-09-2022, 05:00 AM)marlonbown Wrote:
connection_obj = sqlite3.connect(database)
cursor_obj = connection_obj.cursor()
statement = '''SELECT * FROM APIX MINUS SELECT * FROM APIW'''
res = connection_obj.execute(statement)
You must use the connection_obj to execute the query. Use the cursor_obj instead.
Reply
#3
I find it annoying that all the sql tutorials make a cursor object. There are lots of things that a cursor object cannot do, and most of the things you can do with a cursor object can be done without one. Just use your connection object where you would normally use a cursor object (because you've been brainwashed into thinking they are requried!)
Reply
#4
Hi, thanks for the quick replies.
I attempted to use cursor obj and now luck. Same error.
Please see message:
Traceback (most recent call last):
File "/Users/u/venv/delmain2.py", line 61, in <module>
diff("apix.db", "APIX", "APIW")
File "/Users/u/venv/delmain2.py", line 55, in diff
res = cursor_obj.execute(statement)
sqlite3.OperationalError: near "SELECT": syntax error


Code now is:
def diff(database, tableA, tableB):
        table1 = "APIX"
        table2 = "APIW"

        connection_obj = sqlite3.connect('apix.db')
        cursor_obj = connection_obj.cursor()
        statement = '''SELECT * FROM APIX MINUS SELECT * FROM APIW'''
        res = cursor_obj.execute(statement)
        return res

diff("apix.db", "APIX", "APIW")
======
(Nov-09-2022, 07:47 PM)deanhystad Wrote: I find it annoying that all the sql tutorials make a cursor object. There are lots of things that a cursor object cannot do, and most of the things you can do with a cursor object can be done without one. Just use your connection object where you would normally use a cursor object (because you've been brainwashed into thinking they are requried!)
Reply
#5
Looking into this a tiny bit. Are you sure about "MINUS"? Looks like you should be using "EXCEPT".
ibreeden likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error "cannot identify image file" part way through running hatflyer 0 683 Nov-02-2023, 11:45 PM
Last Post: hatflyer
  Error when running kivy on python janeik 8 2,076 Jun-16-2023, 10:58 PM
Last Post: janeik
  [Solved]Help comparing 2 databases Extra 2 1,123 Jul-15-2022, 11:12 PM
Last Post: Extra
  Error while running code on VSC maiya 4 3,785 Jul-01-2022, 02:51 PM
Last Post: maiya
  Pandas - error when running Pycharm, but works on cmd line zxcv101 1 1,377 Jun-18-2022, 01:09 PM
Last Post: snippsat
  Error when running a matplot lib example aurelius_nero 3 6,916 Apr-24-2022, 01:24 PM
Last Post: Axel_Erfurt
  Keep getting Session management error when running imshow in pycharm pace 0 2,105 Mar-25-2021, 10:06 AM
Last Post: pace
  Error when running script on startup in Linux NoahTheNerd 0 1,978 Mar-07-2021, 04:54 PM
Last Post: NoahTheNerd
  Error when running mktorrent subprocess command pythonnewbie138 4 3,884 Sep-16-2020, 01:55 AM
Last Post: pythonnewbie138
  error while running in debug mode ModuleNotFoundError avipy123 0 2,640 Jul-14-2020, 02:05 PM
Last Post: avipy123

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020