gpt4 book ai didi

python - Python 和 C 中的 SQlite 时间戳差异

转载 作者:行者123 更新时间:2023-11-30 16:21:06 28 4
gpt4 key购买 nike

当我尝试将程序从 Python 迁移到 C 时,我遇到了问题。在 Python 中,我能够存储“时间戳”,而在 C 中,我必须使用 TEXT 才能使其工作。但现在,当我在给定日期之间搜索时,它只是无限循环。我认为这是时间戳与文本的问题?除此之外,数据库看起来正确吗?

两个搜索看起来都是这样的:

SELECT timestamp, df1 FROM PLCValues where timestamp BETWEEN '2019-02-25 18:42:50' AND '2019-03-04 18:42:50'

Python我编写了创建一个表

cur.execute("CREATE TABLE IF NOT EXISTS PLCValues(ID INTEGER PRIMARY KEY AUTOINCREMENT, [timestamp] timestamp, \
x001 NUMERIC, x002 NUMERIC, x003 NUMERIC, x004 NUMERIC, x005 NUMERIC, x006 NUMERIC, x007 NUMERIC, x008 NUMERIC,\
y001 NUMERIC, y002 NUMERIC, y003 NUMERIC, y004 NUMERIC, y005 NUMERIC, y006 NUMERIC,\
x201 NUMERIC, x202 NUMERIC, x203 NUMERIC, x204 NUMERIC, x205 NUMERIC, x206 NUMERIC, x207 NUMERIC, x208 NUMERIC,\
df1 REAL, df2 REAL, df3 REAL, df4 REAL)")

并用

填充表格
cur.execute("INSERT INTO PLCValues VALUES(null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",\
(datetime.now(), \
regs1[0], regs1[1], regs1[2], regs1[3], regs1[4], regs1[5], regs1[6], regs1[7],\
regs2[0], regs2[1], regs2[2], regs2[3], regs2[4], regs2[5],\
regs3[0], regs3[1], regs3[2], regs3[3], regs3[4], regs3[5], regs3[6], regs3[7],\
Temp/10, Humid/10, 0, 0))

在 C 语言中,我必须做一些不同的事情。

sql = "CREATE TABLE IF NOT EXISTS PLCValues(ID INTEGER PRIMARY KEY AUTOINCREMENT, timestamp TEXT, \
x001 NUMERIC, x002 NUMERIC, x003 NUMERIC, x004 NUMERIC, x005 NUMERIC, x006 NUMERIC, x007 NUMERIC, x008 NUMERIC,\
y001 NUMERIC, y002 NUMERIC, y003 NUMERIC, y004 NUMERIC, y005 NUMERIC, y006 NUMERIC,\
x201 NUMERIC, x202 NUMERIC, x203 NUMERIC, x204 NUMERIC, x205 NUMERIC, x206 NUMERIC, x207 NUMERIC, x208 NUMERIC,\
df1 REAL, df2 REAL, df3 REAL, df4 REAL)";

然后用

填充表格
strftime(TimeStamp, 20, "%Y-%m-%d %H:%M:%S", localtime(&now));
printf("%s\n",TimeStamp);

char *sql = "INSERT INTO PLCValues VALUES(null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

int rc = sqlite3_prepare_v2(db, sql, -1, &res, 0);

if (rc == SQLITE_OK ) {

//Bind the variables
sqlite3_bind_text(res, 1, TimeStamp, -1, SQLITE_TRANSIENT);

最佳答案

这似乎已经解决了问题。

sql = "CREATE TABLE IF NOT EXISTS PLCValues(ID INTEGER PRIMARY KEY AUTOINCREMENT, timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, \
x001 NUMERIC, x002 NUMERIC, x003 NUMERIC, x004 NUMERIC, x005 NUMERIC, x006 NUMERIC, x007 NUMERIC, x008 NUMERIC,\
y001 NUMERIC, y002 NUMERIC, y003 NUMERIC, y004 NUMERIC, y005 NUMERIC, y006 NUMERIC,\
x201 NUMERIC, x202 NUMERIC, x203 NUMERIC, x204 NUMERIC, x205 NUMERIC, x206 NUMERIC, x207 NUMERIC, x208 NUMERIC,\
df1 REAL, df2 REAL, df3 REAL, df4 REAL)";

关于python - Python 和 C 中的 SQlite 时间戳差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54994668/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com