sqlalchemy从mysql/mariadb数据库中读取0000-00-00 00 00:00 00 00 00:00,并使用dateTime/timestamp数据类型值为相同

发布于 2025-02-07 20:55:41 字数 1639 浏览 2 评论 0 原文

当我尝试从一个数据库中的表中读取数据时(使用Python,Sqlalchemy或Pandas对数据进行一些操作),并将此数据发送到另一个数据库中的同一表(具有表格的Shema)值 DateTime/Timestamp MySQL等于 0000-00-00-00 00:00:00 值或 null (因此,此列可以是 null ,一些日期(包括0000-00-00 00:00:00)),在python中,它们具有与 none (如果只是sqlalchemy)或相同的值NAT (如果我们阅读到Pandas DataFrame)。 如何保存此值,然后发送到另一个表有效数据(如果是0000-00-00 00:00:00,请发送 0000-00-00-00 00:00:00:00 ,如果是无效的 null

示例:

create table some_table
(
    userid           int auto_increment,
    username         varchar(255)        not null,
    email            varchar(255)        not null,
    lastLogin_Date   datetime            null,
    primary key (userid)
)
    collate = utf8mb4_unicode_ci;

mysql 列中的数据

strens lastlogin_date

null

0000-00-00-00-00 00:00:00

表格的

<

发送

这些数据如何读取Python Sqlalchemy-

如果SQLalchemy输出: [(none,),(none),(none),(none),(none),(none,)]

输出pandas:

0 nat

1 nat

2 nat 2 nat

3 nat

4 nat 4 nat

5 nat

我在python中如何制作它,因此只需使用sqlalchemy,我们就会得到上述(也是我所知道的,在Pandas sqlalchemy的引擎盖下):

engine3 = create_engine(
    'mysql+mysqlconnector://' + 'root' + ':' + 'root' + 
    '@localhost:' + '3306' + '/' + 'testdb',
    echo=False)

for chunk_dataframe in pd.read_sql(
        "SELECT * FROM table_name", engine3, chunksize=10000):
    pass

用方法DF.TO_SQL(...)将数据返回MySQL后,而不是0000-00-00-00 00 00: 00:00,将分别为null,如何保存原始值?

When I try to read data from table in one database(using Python, sqlalchemy or pandas to make some manipulations with data) and send this data to the same table in another database (with the same shema of table) values with the type datetime/timestamp in mysql which are equal to 0000-00-00 00:00:00 value or NULL (so this column can be NULL, some date(include 0000-00-00 00:00:00)), in Python they have the same value as None(if it's just sqlalchemy), or NaT(if we read to pandas dataframe).
How can I save this values, and send to another table valid data(if was 0000-00-00 00:00:00, send 0000-00-00 00:00:00, if was NULL send NULL)

example shema of a table:

create table some_table
(
    userid           int auto_increment,
    username         varchar(255)        not null,
    email            varchar(255)        not null,
    lastLogin_Date   datetime            null,
    primary key (userid)
)
    collate = utf8mb4_unicode_ci;

Data in MYSQL

column:

lastLogin_Date

null

0000-00-00 00:00:00

null

null

null

How this data reads python sqlalchemy -

Output if sqlalchemy:
[(None,), (None,), (None,), (None,), (None,)]

Output pandas:

0 NaT

1 NaT

2 NaT

3 NaT

4 NaT

5 NaT

How I make it in Python,so if just use sqlalchemy we will get as above None (also as I know under the hood in pandas sqlalchemy):

engine3 = create_engine(
    'mysql+mysqlconnector://' + 'root' + ':' + 'root' + 
    '@localhost:' + '3306' + '/' + 'testdb',
    echo=False)

for chunk_dataframe in pd.read_sql(
        "SELECT * FROM table_name", engine3, chunksize=10000):
    pass

After the data is returned to mysql with the method df.to_sql(...), instead of 0000-00-00 00:00:00, it will be NULL, respectively, how to save the original values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文