[Tutor] sqlite3 module
Alex Kleider
alexkleider at gmail.com
Sun Apr 16 14:09:48 EDT 2023
I'm running Python3 within a virtualenv on a Debian machine.
My question has to do with the sqlite3 module.
Must I provide a value for every field in the table?
It seems that this shouldn't be necessary if default values are
specified but the following code suggests that one must in fact
supply a value for _every_ field.
When I complied (up to a point, giving the data dict values for
all columns _except_ the autoincrimenting primary key) I got the
same error; It would seem to me that it shouldn't accept an entry
for the primary key rather than insist on it.
Is there a way to make what I want to do possible?
Thanks in advance for any guidance provided.
"""
(p9) alex at t460:~/Git/Sql$ sqlite3 ~/Git/Sql/Secret/club.db
SQLite version 3.34.1 2021-01-20 14:10:07
Enter ".help" for usage hints.
sqlite> .schema Receipts
CREATE TABLE Receipts (
ReceiptID INTEGER PRIMARY KEY,
personID INTEGER NOT NULL,
date_recieved TEXT NOT NULL,
dues INTEGER DEFAULT NULL,
dock INTEGER DEFAULT NULL,
kayak INTEGER DEFAULT NULL,
mooring INTEGER DEFAULT NULL,
acknowledged TEXT DEFAULT NULL
--date value
);
sqlite> .quit
(p9) alex at t460:~/Git/Sql$ cat enter.py
#!/usr/bin/env python3
# File: enter.py
import sqlite3
db_file_name = '/home/alex/Git/Sql/Secret/club.db'
db = sqlite3.connect(db_file_name)
cursor = db.cursor()
data = {"dues": 50,
"date_received": "20230407",
"date_acknowledged": "20230410",
}
f_keys = ", ".join([":"+key for key in data.keys()])
query = "INSERT INTO Receipts VALUES ({});".format(f_keys)
cursor.executemany(query, data)
cursor.close()
db.commit()
db.close()
(p9) alex at t460:~/Git/Sql$ ./enter.py
Traceback (most recent call last):
File "/home/alex/Git/Sql/./enter.py", line 16, in <module>
cursor.executemany(query, data)
sqlite3.OperationalError: table Receipts has 8 columns but 3 values
were supplied
(p9) alex at t460:~/Git/Sql$
"""
... again, thank you.
a
--
alex at kleider.ca (he/him)
(sent from my current gizmo)
More information about the Tutor
mailing list