Guest User

Untitled

a guest
Mar 24th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. # coding=utf-8
  2.  
  3. import geocoder
  4. import pyodbc
  5.  
  6. updateNum = 10
  7.  
  8. # sql setting
  9. local_server = ''
  10. local_database = ''
  11. local_username = ''
  12. local_password = ''
  13. local_driver = '{SQL Server}'
  14.  
  15. # connect sql and read data
  16. connection = pyodbc.connect(
  17. 'Driver='+ local_driver +';'
  18. 'Server='+ local_server +';'
  19. 'Database='+ local_database +';'
  20. 'uid='+ local_username +';pwd=' + local_password)
  21. cursor = connection.cursor()
  22. SQLCommand = ("""
  23. SELECT TOP (%d) [CONT_TARGET_ID]
  24. ,[CONT_TARGET_ADDR]
  25. ,[CONT_TARGET_LAT]
  26. ,[CONT_TARGET_LON]
  27. FROM [DW_CRM].[dbo].[D_CONT_TARGET]
  28. WHERE CONT_TARGET_LAT IS NULL
  29. """ % (updateNum))
  30. cursor.execute(SQLCommand)
  31. results = cursor.fetchall()
  32. print "read sql end"
  33.  
  34. # update lat and lng
  35. for dataRow in results:
  36. g = geocoder.google(dataRow[1],language = 'zh-TW')
  37. if g.lat is not None and g.lng is not None:
  38. SQLUpdate = ("""
  39. UPDATE [dbo].[D_CONT_TARGET]
  40. SET [CONT_TARGET_LAT] = %f
  41. ,[CONT_TARGET_LON] = %f
  42. WHERE [CONT_TARGET_ID] = %d
  43. """ % (g.lat, g.lng, dataRow[0]))
  44. print SQLUpdate
  45. print "update lat and lng success"
  46. cursor.execute(SQLUpdate)
  47. else :
  48. print "update failed"
  49. print dataRow[0], g.address, g.county, g.latlng
  50. connection.commit()
  51. cursor.close()
  52. connection.close()
Add Comment
Please, Sign In to add comment