从Python程序连接到SQLite数据库

时间:2015-01-22 14:58:52

标签: python python-2.7

我正在使用我的Python程序逐行接收来自设备的流数据。数据如下所示:

MSG,4,0,0,80068C,0,2015/01/22,14:40:15.815,2015/01/22,14:40:15.815,,,228.9,212.5,,,-1280,,,,,0
MSG,4,0,0,80068C,0,2015/01/22,14:40:28.722,2015/01/22,14:40:28.722,,,230.5,212.2,,,-1920,,,,,0
MSG,8,0,0,80068C,0,2015/01/22,14:40:34.284,2015/01/22,14:40:34.284,,,,,,,,,,,,0
MSG,8,0,0,80068C,0,2015/01/22,14:40:35.144,2015/01/22,14:40:35.144,,,,,,,,,,,,0
MSG,5,0,0,80068C,0,2015/01/22,14:40:35.144,2015/01/22,14:40:35.144,,5400,,,,,,,,,0,0
MSG,3,0,0,80068C,0,2015/01/22,14:40:36.065,2015/01/22,14:40:36.065,,5375,,,,,,,,,,0
MSG,4,0,0,80068C,0,2015/01/22,14:40:36.644,2015/01/22,14:40:36.644,,,231.6,211.7,,,-1728,,,,,0
MSG,8,0,0,80068C,0,2015/01/22,14:40:37.503,2015/01/22,14:40:37.503,,,,,,,,,,,,0
MSG,4,0,0,80068C,0,2015/01/22,14:40:38.815,2015/01/22,14:40:38.815,,,231.6,211.7,,,-1408,,,,,0

现在我想从程序连接到SQLite数据库,并在收到它们的瞬间将每行保存为CSV格式的数据库。

我的Python代码如下:

from socket import *

HOST = 'localhost'
PORT = 30003    #our port from before
ADDR = (HOST,PORT)
BUFSIZE = 4096

sock = socket( AF_INET,SOCK_STREAM)
sock.connect((ADDR))


def readlines(sock, recv_buffer=4096, delim='\n'):
    buffer = ''
    data = True
    while data:
        data = sock.recv(recv_buffer)
        buffer += data

        while buffer.find(delim) != -1:
            line, buffer = buffer.split('\n', 1)
            yield line.strip('\r\n')

    return

for line in readlines(sock):
    print line

0 个答案:

没有答案