python sqlite 解析日期时间字符串时出错

问题描述 投票:0回答:2

我一直在尝试解决如何将数据导入到 sqlite 表中,我可以做到这一点,但我似乎在日期条目方面遇到错误问题。

这是演示错误的可重现代码

.txt 文件

  1,2019-08-24 
  2,2019-08-24

和 .py 文件

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
import sqlite3
import importlib
import subprocess

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///mydatabase.db'
db = SQLAlchemy(app)

class MyTable(db.Model):
    __tablename__ = 'myTable'
    id = db.Column(db.Integer, primary_key=True)
    date_created = db.Column(db.DateTime)

db.create_all()

p = subprocess.Popen(["sqlite3", "mydatabase.db"], stdout=subprocess.PIPE, stdin=subprocess.PIPE)

p.communicate(b"""
INSERT INTO mytable (id, date_created);
.separator ","
.import repro.txt mytable """)

rows = MyTable.query.all()

for row in rows:
    mytable_update = MyTable.query.get_or_404(row.id)
    mytable_update.date_created = datetime.strptime(mytable_update.date_created, "%Y-%m-%d").date()

db.session.commit()

这给出了错误

ValueError: Couldn't parse datetime string: '2019-08-24 '

或者我收到的完整错误消息是

(env) (base) Benjamats-Air:helloPython benjamattesjaroen$ python repro.py
/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py:835: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
Error: near line 2: near ";": syntax error
Traceback (most recent call last):
  File "repro.py", line 26, in <module>
    rows = MyTable.query.all()
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/sqlalchemy/orm/query.py", line 3178, in all
    return list(self)
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/sqlalchemy/orm/loading.py", line 105, in instances
    util.raise_from_cause(err)
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 398, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 153, in reraise
    raise value
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/sqlalchemy/orm/loading.py", line 85, in instances
    rows = [proc(row) for row in fetch]
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/sqlalchemy/orm/loading.py", line 85, in <listcomp>
    rows = [proc(row) for row in fetch]
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/sqlalchemy/orm/loading.py", line 572, in _instance
    populators,
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/sqlalchemy/orm/loading.py", line 693, in _populate_full
    dict_[key] = getter(row)
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/sqlalchemy/engine/result.py", line 107, in __getitem__
    return processor(self._row[index])
  File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/sqlalchemy/processors.py", line 43, in process
    "'%s'" % (type_.__name__, value)
ValueError: Couldn't parse datetime string: '2019-08-24 '
python sqlite
2个回答
0
投票
ValueError: Couldn't parse datetime string: '2019-08-24 '

显示您的错误。您想要作为日期传递的字符串中有一个空格。

您需要打开 repro.txt 并删除行尾的尾随空格。


-1
投票

.txt
文件似乎包含
Date
类型的条目,而不是
Datetime
类型的条目。您可以将以下行的数据转换为 sqlalchemy 指定的类型
Datetime
Date - 日期时间描述

如果该列中的所有数据都包含

'2019-08-24'
'YYYY-MM-DD'
格式,那么您可以安全地将数据库列类型更改为
Column(Date)

© www.soinside.com 2019 - 2024. All rights reserved.