Not able to insert DateTime into sqlite
See original GitHub issueI run sandman2ctl sqlite+pysqlite:///storage.db
with an sqlite database.
The following error occurs if I try to insert '2019-02-17T21:48:07.539Z'
into a column with the type datetime
:
[2019-02-17 22:48:27,888] ERROR in app: Exception on /t_purchase/ [POST]
Traceback (most recent call last):
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sqlalchemy\engine\base.py", line 1127, in _execute_context
context = constructor(dialect, self, conn, *args)
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sqlalchemy\engine\default.py", line 669, in _init_compiled
param.append(processors[key](compiled_params[key]))
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sqlalchemy\dialects\sqlite\base.py", line 610, in process
raise TypeError("SQLite DateTime type only accepts Python "
TypeError: SQLite DateTime type only accepts Python datetime and date objects as input.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\flask\views.py", line 88, in view
return self.dispatch_request(*args, **kwargs)
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\flask\views.py", line 158, in dispatch_request
return meth(*args, **kwargs)
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sandman2\decorators.py", line 72, in decorated
return func(instance, *args, **kwargs)
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sandman2\service.py", line 152, in post
db.session().commit()
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sqlalchemy\orm\session.py", line 954, in commit
self.transaction.commit()
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sqlalchemy\orm\session.py", line 467, in commit
self._prepare_impl()
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sqlalchemy\orm\session.py", line 447, in _prepare_impl
self.session.flush()
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sqlalchemy\orm\session.py", line 2313, in flush
self._flush(objects)
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sqlalchemy\orm\session.py", line 2440, in _flush
transaction.rollback(_capture_exception=True)
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sqlalchemy\util\langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sqlalchemy\util\compat.py", line 249, in reraise
raise value
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sqlalchemy\orm\session.py", line 2404, in _flush
flush_context.execute()
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 395, in execute
rec.execute(self)
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 560, in execute
uow
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sqlalchemy\orm\persistence.py", line 181, in save_obj
mapper, table, insert)
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sqlalchemy\orm\persistence.py", line 872, in _emit_insert_statements
execute(statement, params)
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sqlalchemy\engine\base.py", line 948, in execute
return meth(self, multiparams, params)
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sqlalchemy\sql\elements.py", line 269, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sqlalchemy\engine\base.py", line 1060, in _execute_clauseelement
compiled_sql, distilled_params
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sqlalchemy\engine\base.py", line 1132, in _execute_context
None, None)
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sqlalchemy\engine\base.py", line 1413, in _handle_dbapi_exception
exc_info
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sqlalchemy\util\compat.py", line 265, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sqlalchemy\util\compat.py", line 248, in reraise
raise value.with_traceback(tb)
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sqlalchemy\engine\base.py", line 1127, in _execute_context
context = constructor(dialect, self, conn, *args)
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sqlalchemy\engine\default.py", line 669, in _init_compiled
param.append(processors[key](compiled_params[key]))
File "c:\users\lorenz\appdata\local\programs\python\python36\lib\site-packages\sqlalchemy\dialects\sqlite\base.py", line 610, in process
raise TypeError("SQLite DateTime type only accepts Python "
sqlalchemy.exc.StatementError: (builtins.TypeError) SQLite DateTime type only accepts Python datetime and date objects as input. [SQL: 'INSERT INTO t_purchase (date) VALUES (?)'] [parameters: [{'date': '2019-02-17T21:48:07.539Z'}]]
127.0.0.1 - - [17/Feb/2019 22:48:27] "POST /t_purchase/ HTTP/1.1" 500 -
Issue Analytics
- State:
- Created 5 years ago
- Comments:5
Top Results From Across the Web
How do I insert datetime value into a SQLite database?
The format you need is: '2007-01-01 10:00:00'. i.e. yyyy-MM-dd HH:mm:ss. If possible, however, use a parameterised query as this frees you ...
Read more >How Insert datetime value in SQLite database? - Odoo ERP
Method 1. Inserting current timestamp by default. If you don't want to insert the timestamp manually each time you create a row, you...
Read more >Proposed slightly-incompatible change to date-time functions
As you know, SQLite currently supports three different representations for date/time values: TEXT → YYYY-MM-DD HH:MM:SS.SSS.
Read more >Insert time/date into sqlite - Google Groups
SQLite doesn't have a DATE type specifically. SQLAlchemy's Date() type expects Python datetime, i.e. "import datetime; date = datetime.date(year, month day)".
Read more >Python SQLite working with Date and DateTime - PYnative
In a usual scenario, when you execute the insert query with the DateTime object, the Python sqlite3 module converts it into a string...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
If anyone else has this problem as well, I’ve found the solution: You need to create a custom type and cast the string to a datetime object yourself.
with python 3.6.9 I had to make a couple updates.
def datetime_sqlalchemy(value): return datetime.datetime.strptime(value, ‘%Y-%m-%d %H:%M:%S.%f’)
and
datetime_created=datetime_sqlalchemy(str(datetime.datetime.now()))
and sqlalchemy is set up this way:
class Article(Base): tablename = ‘article’ datetime_created = Column(DateTime) datetime_end = Column(DateTime)
Let me know if I’m doing this incorrectly ;but thought I’d leave an update on what worked for me.