所以我有一个函数可以读取一个txt,解析它,然后将它加载到一个sql表中。
def main():
connection = sqlite3.connect("myTable.db")
crsr = connection.cursor()
lines = open("LLA03132A.txt", "r").readlines()
input = Thpt(lines)
sql = """CREATE table if not exists my_table (
date STRING,
site STRING,
sector STRING,
avg_thpt FLOAT
);"""
crsr.execute(sql)
for x in input:
time = x[0]
site = x[1][1:8]
sector = x[1]
avg_thpt = x[2]
sql = """INSERT INTO my_table VALUES ( %s , %s , %s , %f );"""%(time, site, sector, avg_thpt)
print(sql)
crsr.execute(sql)
connection.commit()
connection.close()
但是,当我调用它时,我得到了这个错误。我究竟做错了什么?我是否错误地命名了我的类别或我的价值观?某处有隐藏角色吗?我也在使用 datetime 包,仅供参考
Traceback (most recent call last):
INSERT INTO my_table VALUES ( 2018-07-03 10:14:18.060416 , LA03132 , LLA03132A11 , 2637.806265 );
File "C:/Users/myname/PycharmProjects/Parser/parser2.py", line 60, in <module>
main()
File "C:/Users/myname/PycharmProjects/Parser/parser2.py", line 55, in main
crsr.execute(sql)
sqlite3.OperationalError: near "10": syntax error