简体   繁体   English

使用 sqlite3 python 到数据库的日期时间

[英]datetime to database using sqlite3 python

I am currently trying to use a user inputted datetime to subtract a start time of one task from an end time of the task, but I am having problems using the inputted time.我目前正在尝试使用用户输入的日期时间从任务的结束时间中减去一项任务的开始时间,但是我在使用输入的时间时遇到了问题。 can you use datetime for just hours, minutes, and seconds, or can you only start with dates?您可以仅将 datetime 用于小时、分钟和秒,还是只能从日期开始? Here is what i tried:这是我尝试过的:

import sqlite3
import datetime
connection = sqlite3.connect('random.db')
c = connection.cursor()
count=c.execute('select * from table1')
records = c.fetchall()
for row in records:
    print(row[0])
    print(row[1])
    print(row[2])

print(count)
connection.commit()

ans_1 = input('what is your name?(person1 or person2)')
ans_2_1 = int(input('what is your start time for hour?'))
ans_2_2 = int(input('what is your start time for minute?'))
ans_2_3 = int(input('what is your start time for second?'))
ans_3_1 = int(input('what is your end time for hour?'))
ans_3_2 = int(input('what is your end time for minute?'))
ans_3_3 = int(input('what is your end time for second'))
date = datetime.date(1, 1, 1)
start_time = datetime.time(ans_2_1, ans_2_2, ans_2_3)
stop_time = datetime.time(ans_3_1, ans_3_2, ans_3_3)
datetime1 = datetime.datetime.combine(date, start_time)
datetime2 = datetime.datetime.combine(date, stop_time)
time_elapsed = datetime1 - datetime2
c.execute('CREATE TABLE IF NOT EXISTS table1(start text, end text, total text)')
#params = (start_time, stop_time, time_elapsed)
params = ('1', '2', 'time_elapsed')

if ans_1 == 'person1':
    c.execute('select count(*) from table1')
    c.execute('INSERT INTO table1 VALUES(?, ?, ?)', params)
    c.execute('select count(*) from table1')
    connection.commit()
    c.close()
elif ans_1 == 'person2':
    c.execute('select count(*) from table1')
    c.execute('INSERT INTO table1 VALUES(?, ?, ?)', params)
    c.execute('select count(*) from table1')
    connection.commit()
    c.close()
else:
    print('invalid')

it just puts 1, 2, and time_elapsed into the database它只是将 1、2 和 time_elapsed 放入数据库

If i use如果我使用

params = (1, 2, time_elapsed)

then i get this error:然后我得到这个错误:

Traceback (most recent call last):
  File "C:/Users/s-wan/PycharmProjects/1/venv/Scripts/database.py", line 34, in <module>
    c.execute('INSERT INTO table1 VALUES(?, ?, ?)', params)
sqlite3.InterfaceError: Error binding parameter 2 - probably unsupported type.

I do not know how to solve this problem.我不知道如何解决这个问题。 Please help.请帮忙。

SQLite doesn't support timedelta, so you'll have to convert it to one of the types that are supported. SQLite 不支持 timedelta,因此您必须将其转换为支持的类型之一。

If you're going to be using this in further calculations, probably the most convenient would be the number of seconds: params = (1, 2, time_elapsed.total_seconds())如果你打算在进一步的计算中使用它,最方便的可能是秒数: params = (1, 2, time_elapsed.total_seconds())

If it's mostly for display, and you don't expect to do any further calculations, you can instead convert it to text: params = (1, 2, str(time_elapsed))如果它主要用于显示,并且您不希望进行任何进一步的计算,则可以将其转换为文本: params = (1, 2, str(time_elapsed))

If you're not sure, probably go with the number of seconds.如果您不确定,可能会使用秒数。

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

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