Silly/crazy problem with sqlite

Chris Green cl at isbd.net
Fri Nov 24 15:49:20 EST 2023


This is driving me crazy, I'm running this code:-

        #!/usr/bin/env python3 
        # 
        # 
        # Show the electric fence history, default to last 24 hours 
        # 
        import sqlite3
        import datetime
        import sys

        today = datetime.datetime.now()
        today = str(today)
        x = str(today[0:10])
        print(x)

        fdb = sqlite3.connect("/home/chris/.share/newbourne.db")
        cr = fdb.cursor()

        sql = "SELECT * FROM fence where datetime LIKE ?"
        cr.execute(sql, ('%' + "2023-11" + '%'))
        rv = cr.fetchall()
        for d in rv:
            print(d)
        fdb.commit()
        fdb.close()

    Forget about the 'today =' bits, they no longer do anything.

When I run the above I get:-

    chris at esprimo$ fence.py
    2023-11-24
    Traceback (most recent call last):
      File "/home/chris/dev/bin/fence.py", line 19, in <module> cr.execute(sql, ('%' + "2023-11" + '%')) sqlite3.ProgrammingError: Incorrect number of bindings supplied.  The current statement uses 1, and there are 9 supplied.
    chris at esprimo$ 

It's treating the "2023-11" plus % at each end as separate variables to
the binding, this is crazy!  I've done similar elsewhere and it works
OK, what on earth am I doing wrong here?  It has to be something very
silly but I can't see it at the moment.

-- 
Chris Green
·


More information about the Python-list mailing list