I have an sql file containing, say,
"INSERT INTO mytable (ID, Date) VALUES (1, datetime.date(2023, 2, 22));"
How do I create a table that will accept this? More explicitly, in
"CREATE TABLE mytable(ID INT NOT NULL, Date ???)) ENGINE=INNODB;"
what should I put in place of ‘????’?
When I substitute varchar(12) for ‘????’, I get the error message:
mysql.connector.errors.ProgrammingError: 1630 (42000): FUNCTION datetime.date does not exist. Check the ‘Function Name Parsing and Resolution’ section in the Reference Manual. Checking doesn’t reveal any insights, at least for me. What should I be doing?
2
Answers
You can directly pass a date in string format (ex. ‘2023-02-22’), notice the quotes since it should be a string. Also, the data type of the column should simply be a DATE.
Create table:
CREATE TABLE mytable(ID INT NOT NULL, Date DATE) ENGINE=INNODB;
Insert values:
INSERT INTO mytable (ID, Date) VALUES (1, '2023-02-22');
The error message says that MySQL is interpreting
datetime.date(2023, 2, 22)
as a function call, which doesn’t exist in MySQL, hence the error. This is because you useddatetime.date(2023, 2, 22)
inside your INSERT INTO statement, and it seems that the MySQL connector is treating it as a function rather than a date value.So ideally you should convert the date objects to the desired MySQL date format and then use that in your INSERT queries.