Home > Back-end >  Jupyter Notebook AttributeError: SQL Database in Python
Jupyter Notebook AttributeError: SQL Database in Python

Time:03-13

My problem is about how can I solve this AttributeError , here's the code:

import pandas as pd 
import sqlalchemy
from binance import Client
from binance import BinanceSocketManager

client = Client('api', 'secret_api')
bsm = BinanceSocketManager(client)
socket = bsm.trade_socket('BTCUSDT')

while True:
    await socket.__aenter__()
    msg = await socket.recv()
    frame = createframe(msg)
    frame.to_sql('BTCUSDT', engine, if_exists='append', index=False)
    print(frame) 

But it raises the following Error to me:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_11340/3652326046.py in <module>
      3     msg = await socket.recv()
      4     frame = createframe(msg)
----> 5     frame.to_sql('BTCUSDT', engine, if_exists='append', index=False)
      6     print(frame)

AttributeError: 'NoneType' object has no attribute 'to_sql'

Here is the "createframe" function:

def createframe(msg):
    df = pd.DataFrame([msg])
    df = df.loc[:,['s','E','p']]
    df.columns = ['symbol','Time','Price']
    df.Price = df.Price.astype(float)
    df.Time = pd.to_datetime(df.Time, unit='ms')

CodePudding user response:

It seems that your createframe method, does not return anything. I would propose you to change it to something like:

def createframe(msg):
    df = pd.DataFrame([msg])
    df = df.loc[:,['s','E','p']]
    df.columns = ['symbol','Time','Price']
    df.Price = df.Price.astype(float)
    df.Time = pd.to_datetime(df.Time, unit='ms')
    return df # Here is the change where it returns your DataFrame to where it is called from

I hope this works. Can not really post more because we lack more background to your question

  • Related