Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. 1 step: Create a temporary table with pyodbc into sql server for objects
  2. 2 step: Select objects from temporary table and load it into pandas dataframe
  3. 3 step: print dataframe
  4.  
  5. import codecs
  6. import os
  7. import io
  8. import pandas as pd
  9. import pyodbc as po
  10.  
  11. server = 'sql_server'
  12. database = 'sql_database'
  13.  
  14. connection = po.connect('DRIVER={SQL Server};SERVER='+server+';DATABASE='+database+';Trusted_Connection=yes;')
  15. cursor = connection.cursor()
  16.  
  17. query1 = """
  18. CREATE TABLE #ttobject (object_nr varchar(6), change_date datetime)
  19. INSERT INTO #ttobject (object_nr)
  20. VALUES
  21. ('363964'),
  22. ('349897'),
  23. ('371966');
  24. """
  25.  
  26. query2 = """
  27. SELECT *
  28. FROM #ttobject
  29.  
  30. Drop table if exists #ttobject
  31. """
  32.  
  33.  
  34. cursor.execute(query1)
  35. df = pd.read_sql_query(query2, connection)
  36. print(df)
  37.  
  38. ('HY000', '[HY000] [Microsoft][ODBC SQL Server Driver]Connection is busy with results for another hstmt (0) (SQLExecDirectW)')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement