简体   繁体   English

SQlite3 和 python 中的日期时间对象

[英]datetime objects in SQlite3 and python

Trying to store datetimes in my database project using python and sqlite3.尝试使用 python 和 sqlite3 在我的数据库项目中存储日期时间。 Here's what I have so far...这是我到目前为止...

import sqlite3

def connect():
    conn = sqlite3.connect("road_works.db")
    cur = conn.cursor()
    cur.execute("""CREATE TABLE IF NOT EXISTS road_works(id INTEGER PRIMARY 
        KEY, location TEXT, client TEXT, start_date TEXT, end_date TEXT)""")
    conn.commit()
    conn.close()


def insert(location, client, start_date, end_date):
    conn = sqlite3.connect("road_works.db")
    cur = conn.cursor()
    cur.execute("INSERT INTO road_works VALUES (NULL, ?, ?, ?, ?)",
               (location, client, start_date, end_date))
    conn.commit()
    conn.close()

insert("M25", "Lindor", "2019-03-16 20:00", "2019-07-16 06:00")

At the minute the database takes start_date and end_date as text.此时数据库将 start_date 和 end_date 作为文本。 I want to change that to dates that I can then start doing calculations with like counting down the days/nights until the job is finished etc. The user will also be inputing the dates and times so I cant use datetime.datetime.now()/today() I have been reading into datetime but its more advanced than I'm used to if anyone could explain it better I would appreciate it.我想将其更改为日期,然后我可以开始进行计算,例如倒数天/夜直到工作完成等。用户还将输入日期和时间,因此我无法使用 datetime.datetime.now() /today() 我一直在阅读日期时间,但它比我习惯的更先进,如果有人能更好地解释它,我将不胜感激。

I have been searching for the solution for a few days this is my first project it may be a little out of my league but I appreciate your attention.几天来我一直在寻找解决方案,这是我的第一个项目,它可能有点超出我的范围,但我感谢您的关注。

Thanks :)谢谢 :)

fixed!固定的! Thanks for the help!谢谢您的帮助!

def insert(location, client, start_date, end_date):
    conn = sqlite3.connect("road_works.db")
    cur = conn.cursor()
    cur.execute("INSERT INTO road_works VALUES (NULL, ?, ?, ?, ?)",
                (location, client, start_date, end_date))
    start_date = datetime.strptime(start_date, "%Y-%m-%d %H:%M")
    end_date = datetime.strptime(end_date, "%Y-%m-%d %H:%M")
    conn.commit()
    conn.close()

There is no native datatype for dates in sqlite. sqlite 中的日期没有本机数据类型 The manual that I link to advises (see 2.2) three possible ways to store a date:我链接到的手册建议(参见 2.2)三种可能的日期存储方式:

  • TEXT (ISO 8601 string)文本(ISO 8601 字符串)
  • REAL (Julian day numbers) REAL(儒略日数)
  • INTEGER (Unix Time)整数(Unix 时间)

My suggestion would be to use ISO 8601 format, since it is standardized and unambiguous.我的建议是使用 ISO 8601 格式,因为它是标准化且明确的。 Additionally, as opposed to the alternatives it is human-readable without conversion.此外,与替代方案相反,它是人类可读的,无需转换。 And datetime objects have standard conversion methods to and from a string for it.并且datetime对象具有与字符串之间的标准转换方法。

See examples below:请参阅以下示例:

In [1]: from datetime import datetime                                                                    

In [2]: now = datetime.now()                                                                             

In [3]: now                                                                                              
Out[3]: datetime.datetime(2019, 1, 3, 14, 53, 38, 596477)

In [4]: string = now.isoformat()                                                                         

In [5]: string                                                                                           
Out[5]: '2019-01-03T14:53:38.596477'

In [6]: datetime.fromisoformat(string)                                                                   
Out[6]: datetime.datetime(2019, 1, 3, 14, 53, 38, 596477)

You can use datetime.strtime to convert a string to datetime object您可以使用datetime.strtime将字符串转换为 datetime 对象

eg例如

from datetime import datetime
datetime_object = datetime.strptime('2019-03-16 20:00', '%Y-%m-%d %H:%M')

SO所以

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM