SQLite INSERT / LEFT JOIN Statement Returns an Unknown Syntax Error [closed]

I’m using an SQL database to try to keep track of music files after they have been copied to a new location and modified. I’m executing the statement inside of a Python script.

There is three tables: Seed_Album_Log, Sorted_Album_Log, and Full_Album_Log.

Seed_Album_Log is a record of music files on a NAS, three columns: root, artist, album.

Sorted_Album_Log does the same thing but with files that have been copied to a different computer. Three columns, root, artist, album.

Full_Album_Log is a combination of the other two, six columns: seed_root, seed_artist, seed_album, sorted_album, sorted_artist, sorted_root.

The SQL statement should combine rows of the Seed_Album_Log and Sorted_Album_Log where they have matching Album and Artist. Basically it should end up so that the Full_Album_Log is a record of which albums have not been copied to the destination and sorted, and which ones have already.

For speed’s sake, I want to ignore any albums that already have a row in Full_Album_Log that already have all 6 columns filled out. They don’t have to match, as sometimes the sorted files will have different tags that I will fill in manually.

Here is the current command, written with the help of CoPilot and ChatGPT:

    sql = """
    INSERT INTO full_album_log (seed_root, seed_artist, seed_album, sorted_album, sorted_artist, sorted_root)
    SELECT
        seed.root,
        seed.artist,
        seed.album,
        sort.album,
        sort.artist,
        sort.root
    FROM
        seed_album_log seed
    LEFT JOIN
        sorted_album_log sort
    ON
        seed.artist = sort.artist
        AND seed.album = sort.album
    WHERE 
        NOT EXISTS (
            SELECT 1
            FROM full_album_log full
            WHERE full.seed_root = seed.root
            AND full.sorted_root IS NOT NULL
        );
    """
    cursor.execute(sql)

The WHERE statement checks to see if there is a row in full_album_log with the same root directory that also has the sorted root directory filled in with anything. If no such row exists, the INSERT statement executes.

I appreciate any and all help with trying to figure this out!

Problem: When I run this, it returns the error: “sqlite3.OperationalError: near “WHERE”: syntax error”

Neither ChatGPT nor CoPilot could figure out what the error might be, and I’m at a total loss myself. I know the statement above the WHERE works, if I comment out the WHERE it still fills the database fine but doesn’t skip the full rows.

4

You mentioned Full_Album_Log has a column named sorted_dir, but your SQL uses sorted_root.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật