如何通过 websocket 将传入的 json 存储到 SQL 数据库
How to store incoming json via websocket to SQL database
首先,我对 python(和编码期)还很陌生,所以如果我以错误的方式解决这个问题,我深表歉意。
我正在从 websocket 流接收这些 json 消息:
import websocket, json, dateutil.parser
import dateparser
import csv
import itertools
current_tick = None
dataframe = []
symbols = 'AAPL', 'FB', 'AMZN', 'NFLX'
def on_open(ws):
print("opened")
auth_data = {
"action": "auth",
"params": 'APIKEY'
}
ws.send(json.dumps(auth_data))
for s in symbols:
channel_data = {
"action": "subscribe",
"params": s
}
ws.send(json.dumps(channel_data))
def on_message(ws, message):
global current_tick, dataframe
current_tick = json.loads(message)
print(current_tick)
if current_tick[0]['ev'] == 'T':
dataframe.append(current_tick)
def on_close(ws):
print("closed connection")
socket = "WEBSITE"
ws = websocket.WebSocketApp(socket, on_open=on_open, on_message=on_message, on_close=on_close)
#ws.run_forever()
输出:
[{'ev': 'T', 'sym': 'AAPL', 'i': '227161', 'x': 4, 'p': 134.605, 's': 100, 't': 1609275343055, 'z': 3}]
[{'ev': 'T', 'sym': 'AAPL', 'i': '227162', 'x': 4, 'p': 134.605, 's': 3, 'c': [37], 't': 1609275343072, 'z': 3}]
[{'ev': 'T', 'sym': 'AAPL', 'i': '155273', 'x': 12, 'p': 134.6, 's': 25, 'c': [14, 37, 41], 't': 1609275343104, 'z': 3}]
[{'ev': 'T', 'sym': 'FB', 'i': '47501', 'x': 4, 'p': 276.5, 's': 1, 'c': [12, 37], 't': 1609276352067, 'z': 3}]
[{'ev': 'T', 'sym': 'NFLX', 'i': '10420', 'x': 11, 'p': 531.5, 's': 147, 'c': [14, 12, 41], 't': 1609276352376, 'z': 3}]
我的目标是在它们进入时将它们存储到 SQL 数据库中,这就是我目前所拥有的
import psycopg2
postgresConnection = psycopg2.connect(
host='HOST',
user='USER',
password='PASSWORD',
database='DATABASE'
cursor= postgresConnection.cursor()
sqlCreateTable = "create table datas(sym varchar(256), price int, size int, exchange int, time int);"
cursor.execute(sqlCreateTable)
postgresConnection.commit()
sqlpoptable = "intert into datas(sym, price, size) VALUES(%(sym)s, %(price)s, %(size)s)", {"sym":current_tick['sym'],"price":current_tick['p'], "size":current_tick['s']}
cursor.execute(sqlpoptable)
postgresConnection.commit()
我的输出是:
TypeError: list indices must be integers or slices, not str
有什么想法吗?提前致谢。
您可能已经知道,您可以使用索引访问数组数据。索引是一个数字,表示数据在数组中的位置,从数字零开始。
如果您有这样一组项目:
items = ['a', 'z', 'c', 'd']
则'a'
的索引为0,'z'
的索引为1,'c
'的索引为2,依此类推
如果您编写了一些 Python 代码,如下所示:
items = ['a', 'z', 'c', 'd']
myChosenItem = items[0]
那么myChosenItem
就等于'a'
。有道理吗?
您收到的错误告诉您,您在从数组中获取数据时尝试提供除整数(整数)以外的其他内容作为索引。
这就像这样写:
items = ['a', 'z', 'c', 'd']
myChosenItem = items['chicken']
Python 不会明白你的意思。查看您的代码并弄清楚您在哪里使用索引访问数组数据,然后弄清楚该索引是否为整数。然后你应该能够让这个错误消失。
首先,我对 python(和编码期)还很陌生,所以如果我以错误的方式解决这个问题,我深表歉意。
我正在从 websocket 流接收这些 json 消息:
import websocket, json, dateutil.parser
import dateparser
import csv
import itertools
current_tick = None
dataframe = []
symbols = 'AAPL', 'FB', 'AMZN', 'NFLX'
def on_open(ws):
print("opened")
auth_data = {
"action": "auth",
"params": 'APIKEY'
}
ws.send(json.dumps(auth_data))
for s in symbols:
channel_data = {
"action": "subscribe",
"params": s
}
ws.send(json.dumps(channel_data))
def on_message(ws, message):
global current_tick, dataframe
current_tick = json.loads(message)
print(current_tick)
if current_tick[0]['ev'] == 'T':
dataframe.append(current_tick)
def on_close(ws):
print("closed connection")
socket = "WEBSITE"
ws = websocket.WebSocketApp(socket, on_open=on_open, on_message=on_message, on_close=on_close)
#ws.run_forever()
输出:
[{'ev': 'T', 'sym': 'AAPL', 'i': '227161', 'x': 4, 'p': 134.605, 's': 100, 't': 1609275343055, 'z': 3}]
[{'ev': 'T', 'sym': 'AAPL', 'i': '227162', 'x': 4, 'p': 134.605, 's': 3, 'c': [37], 't': 1609275343072, 'z': 3}]
[{'ev': 'T', 'sym': 'AAPL', 'i': '155273', 'x': 12, 'p': 134.6, 's': 25, 'c': [14, 37, 41], 't': 1609275343104, 'z': 3}]
[{'ev': 'T', 'sym': 'FB', 'i': '47501', 'x': 4, 'p': 276.5, 's': 1, 'c': [12, 37], 't': 1609276352067, 'z': 3}]
[{'ev': 'T', 'sym': 'NFLX', 'i': '10420', 'x': 11, 'p': 531.5, 's': 147, 'c': [14, 12, 41], 't': 1609276352376, 'z': 3}]
我的目标是在它们进入时将它们存储到 SQL 数据库中,这就是我目前所拥有的
import psycopg2
postgresConnection = psycopg2.connect(
host='HOST',
user='USER',
password='PASSWORD',
database='DATABASE'
cursor= postgresConnection.cursor()
sqlCreateTable = "create table datas(sym varchar(256), price int, size int, exchange int, time int);"
cursor.execute(sqlCreateTable)
postgresConnection.commit()
sqlpoptable = "intert into datas(sym, price, size) VALUES(%(sym)s, %(price)s, %(size)s)", {"sym":current_tick['sym'],"price":current_tick['p'], "size":current_tick['s']}
cursor.execute(sqlpoptable)
postgresConnection.commit()
我的输出是:
TypeError: list indices must be integers or slices, not str
有什么想法吗?提前致谢。
您可能已经知道,您可以使用索引访问数组数据。索引是一个数字,表示数据在数组中的位置,从数字零开始。
如果您有这样一组项目:
items = ['a', 'z', 'c', 'd']
则'a'
的索引为0,'z'
的索引为1,'c
'的索引为2,依此类推
如果您编写了一些 Python 代码,如下所示:
items = ['a', 'z', 'c', 'd']
myChosenItem = items[0]
那么myChosenItem
就等于'a'
。有道理吗?
您收到的错误告诉您,您在从数组中获取数据时尝试提供除整数(整数)以外的其他内容作为索引。
这就像这样写:
items = ['a', 'z', 'c', 'd']
myChosenItem = items['chicken']
Python 不会明白你的意思。查看您的代码并弄清楚您在哪里使用索引访问数组数据,然后弄清楚该索引是否为整数。然后你应该能够让这个错误消失。