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

时间:2015-04-02 13:25:29

标签: python datetime sqlite typeerror 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(&#39;&#39;&#39; CREATE TABLE stock(日期字符串,temphydro bin)&#39;&#39;)   sqlite3.OperationalError:表库存已存在

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

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

  

databa.execute(&#34; INSERT INTO stock VALUES&#34;(&#39; date&#39;,&#39; test_file&#39;))   TypeError:&#39; str&#39;对象不可调用

这是,如果您需要我的副文件中的内容:

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

谢谢!很快见到你

1 个答案:

答案 0 :(得分:1)

您的INSERT字符串错误。收尾双引号应该在最后,例如&#34; INSERT INTO stock VALUES(&#39; date&#39;,&#39; test_file&#39;)&#34;

相关问题