简体   繁体   中英

How to fetch hourly data of particular date through sql query using for loop by passing python list as an argument or parameter to that sql query?

I wanted to fetch a very large amount of one day data through sql database table using python but it was taking a very long time. So I've decided to fetch partly data, do some operation and append the whole as a result. For that I took hourly data so that the server shouldn't take longer time. I took a list as l = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24] those items ie 1 to 24 are hours number which I have iterated through for loop and pass to sql query to get the result part by part.

Following is code for referrence

(My code):

l = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]

for i in l:

    quoted = urllib.parse.quote_plus('DRIVER={SQL Server};'
                                       'SERVER=servername;'
                                       'DATABASE=DB;'
                                       'uid=userid;'
                                       'pwd=****;')
    engine = 
      create_engine('mssql+pyodbc:///odbc_connect{}'.format(quoted))
    sql = "select DISTINCT a.companyid, c.RegistrationNo, c.gpsdatetime, c.GPSOdoMeter, 
         c.GroundSpeed, c.TxnReason  from Vehicle_Master a with(nolock), GPSEventsData c 
         with(nolock) where a.RegistrationNo = c.RegistrationNo and a.companyid = c.customerid and 
         a.active = 1 and c.gpsdatetime >= DATEADD(day, DATEDIFF(day, 0, GETDATE()) - 1, 0) and DATEPART(HOUR, c.gpsdatetime) = i ORDER BY gpsdatetime"

    df1 = pd.read_sql_query(sql, engine)"
    df1['ChangeInTime'] = df1.groupby('RegistrationNo')['GPSDateTime'].apply(lambda x: x - 
      x.shift(1))
    df1['GroundSpeed'] = df1['GroundSpeed']*(5/18)
    df1['ChangeInTimeInSec'] = df1['ChangeInTime'].dt.total_seconds()
    df1['ChangeInSpeed'] = df1.groupby('RegistrationNo')['GroundSpeed'].apply(lambda x: x - 
    x.shift(1))
    df1['Acceleration'] =  df1['ChangeInSpeed']/df1['ChangeInTimeInSec']
    df1.to_sql(name="Accelerations_Breaking",schema = 'dbo', con = engine,if_exists = 
    'append',index=False)

Following is an error which It's thowing:

ProgrammingError: (pyodbc.ProgrammingError) ('42S22', "[42S22] [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid column name 'i'. (207) (SQLExecDirectW)") [SQL: select DISTINCT a.companyid, c.RegistrationNo, c.gpsdatetime, c.GPSOdoMeter, c.GroundSpeed, c.TxnReason from Vehicle_Master a with(nolock), GPSEventsData c with(nolock) where a.RegistrationNo = c.RegistrationNo and a.companyid = c.customerid and a.active = 1 and c.gpsdatetime >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 1, 0) and DATEPART(HOUR, c.gpsdatetime) = i ORDER BY gpsdatetime] (Background on this error at: Z80791B3AE7002CB88C246 876D9FAA8F8Z://sqlalche.me/e/f405 )

I don't know where I am going wrong.
Please help!!

The error says "Invalid column name 'i'." The problem is DATEPART(HOUR, c.gpsdatetime) = i in your SQL. The i there is a variable in your Python, but you're referring to it directly in your string of SQL. You should use a bind variable, like DATEPART(HOUR, c.gpsdatetime) =? , convert that SQL into a PreparedStatement, or whatever your Python libraries call it, and then set the bind variable to the value of i in your Python.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM