2015-04-02 94 views
0

所以我想用日期和一些二進制數據做一個數據庫。我想要每5分鐘做一次,將我的傳感器的數據,並將其放入具有確切時間的數據庫中。Python/SQlite3'str'對象不可調用

這裏是我的腳本

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

這裏,如果你需要的是在我的bynary文件:

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

謝謝!很快見到你

回答

1

你的INSERT字符串是錯誤的。結束雙引號應該在最後,例如「INSERT INTO stocks VALUES('date','test_file')」

+0

謝謝,我感覺有點愚蠢。我認爲有時可能會發生... – 2015-04-02 13:46:49