我有一个Python程序,该程序如下生成一个单独的sqlite3 shell,并进入循环 发送/执行SQLite3 Shell命令:
sqlpipe = Popen(['sqlite3', '-interactive'], stdout=PIPE, stderr=PIPE, stdin=PIPE,
universal_newlines=True, bufsize=1)
sendcommand(r".prompt _abc\n")
while True:
action = input('Enter:')
answer = sendcommand(action)
if not answer:
break
print("!", answer)
发送的第一个命令是更改.prompt,此后我将能够检测到产生的输出的结尾。 sendcommand如下所示:
def sendcommand(stmt): 全局sqlpipe
print('Sent command', stmt)
sqlpipe.stdin.writelines([stmt])
sqlpipe.stdin.flush()
answer = ''
while True:
line = sqlpipe.stdout.readline()
print(line[:-1])
if line.startswith("_abc"):
break
answer += line
print('ANSWER', answer)
return answer
发出命令sendcommand(r“ .prompt _abc \ n”)时,它将挂起。永远不要产生答案:
python test.py 发送命令.prompt _abc \ n SQLite版本3.19.3 2017-06-27 16:48:08 输入“ .help”作为使用提示。