尝试将日期和时间记录到sqlite3中

2024-06-26 00:20:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我的目标是获取两个变量xdate和xtime,并使用python脚本将它们以两个独立的列存储到sqlite数据库中。我的密码是

from datetime import datetime
import sqlite3 as mydb
import sys

con = mydb.connect('testTime.db')

def logTime():
   i=datetime.now()
   xdate = i.strftime('%Y-%m-%d')
   xtime = i.strftime('%H-%M-%S')
   return xdate, xtime
z=logTime()

这是我挂断电话的地方我试过了

try:
    with con:
        cur = con.cursor
        cur.execute('INSERT INTO DT(Date, Time) Values (?,?)' (z[0],z[1]))
        data = cur.fetchone()
        print (data)
    con.commit()
except:
    with con:
        cur=con.cursor()
        cur.execute("CREATE TABLE DT(Date, Time)')
        cur.commit()

当我试图获取数据时,总是一无所获。你知道吗

有什么建议吗??你知道吗


Tags: importexecutedatetimedatetimewithdtcon
1条回答
网友
1楼 · 发布于 2024-06-26 00:20:50

您正在执行一个insert查询,它的结果是没有任何东西可获取。您应该运行select查询,然后获取数据。你知道吗

fetchone()

Fetches the next row of a query result set, returning a single sequence, or None when no more data is available.

举个例子-

>>> cur.execute('INSERT INTO DT(Date, Time) Values (?,?)', (z[0],z[1]))
<sqlite3.Cursor object at 0x0353DF60>
>>> print cur.fetchone()
None
>>> cur.execute('SELECT Date, Time from DT')
<sqlite3.Cursor object at 0x0353DF60>
>>> print cur.fetchone()
(u'2016-02-25', u'12-46-16')

相关问题 更多 >