1

I'm using mysqlx XDevAPI for python (NoSQL). I can't insert a date into my table.

table = my_schema.get_table("date_table")
field = "date_field"
insert = table.insert(field)
value = datetime.datetime.now()
insert.values(value)
insert.execute

I get an error:

ValueError: Expected token type 19 at pos 0 but found type 72

I'm presuming it's to do with the date/datetime format but I'm not sure how to find what tokens 19 or 72 are. If I try to insert a string or int I get the same error.

ChiPhi85
  • 61
  • 5
  • 2
    What is the MySQL column data type? `DATETIME`, `TIMESTAMP` or other? In any case, apparently there is no support for inserting `datetime` class instances directly but you can always stringify your values with `datetime.now().isoformat()`, for instance, and it should work. – ruiquelhas Jul 14 '20 at 08:44

1 Answers1

1

Yes. Like Rui said, Connector/Python doesn't support Python object conversion in the X DevAPI, you need to convert the datetime to a string format before inserting.

Nuno Mariz
  • 561
  • 3
  • 8
  • 1
    Thanks both! I couldn’t figure out how to accept Rui’s comment as an answer. I incidentally figured this out in the end, it just seemed odd as when using MySQL connector it insisted on a datetime object. – ChiPhi85 Jul 17 '20 at 20:35