Python/SQlite3 'str' 对象不可调用

Python/SQlite3 'str' object is not callable

所以我想用日期和一些二进制数据做一个数据库。我想要做的是每 5 分钟,获取我的传感器的数据,并将其与准确的时间一起放入数据库中。

这是我的脚本

import serial  # import the serial library
from time import sleep  # import the sleep command from the time library
import sqlite3
import datetime


ser = serial.Serial("/dev/tty.usbserial-A400DUTI", 4800)  # load into a variable 'ser' the information about the usb you are listening. /dev/tty.usbserial.... is the port after plugging in the hygrometer
conn=sqlite3.connect('porjectait.db') #connection with the data base
databa=conn.cursor()
databa.execute('''CREATE TABLE porjectait (date string, temphydro bin)''')
test_file=open("temphydro",'w+b')# creation and/or writing in my binary file

while 1 :
    i = 0
    date=(datetime.datetime.now()) #My way to get the date

    while i < 300 : #300 because it will take at least 2 round of datum

        test_file.write(ser.read(size=1)) #write into my bynary file

        i=i+1

    databa.execute("INSERT INTO porjectait VALUES"('date','test_file')) #insert the datum in my database
    conn.commit()
    sleep(300) #wait 5 minutes

我的第一个问题,但我找到了解决它的方法,是我得到了:

databa.execute('''CREATE TABLE stocks (date string, temphydro bin)''') sqlite3.OperationalError: table stocks already exists

为了避免这种情况,我只是在 SQL 的创建行前面放了一个#。但我想找到一种方法来检查数据库是否存在,如果不存在则创建它,否则只有更多数据。

然后我的第二个也是主要问题是当我执行我的脚本时我得到:

databa.execute("INSERT INTO stocks VALUES"('date','test_file')) TypeError: 'str' object is not callable

这是,如果你需要我的二进制文件中的内容:

@
I01010100B00725030178
V0109EB01
I02020100B00725030148
V0215FBD9
I035300B5
V030225B8
I04550065
V040294AE
$
@
I01010100B00725030178
V0109EB01
I02020100B00725030148
V02160264
I035300B5
V03022704
I04550065
V040295F0
$
@

谢谢!待会儿见

您的 INSERT 字符串有误。结束双引号应该在最后,比如 "INSERT INTO stocks VALUES('date','test_file')"