Guest User

Untitled

a guest
Feb 14th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. import pyodbc
  2.  
  3. ## Provide connection details here
  4. host = ''
  5. database = ''
  6. user = ''
  7. password = ''
  8.  
  9. connection = pyodbc.connect('DRIVER={};SERVER={};DATABASE={};UID={};PWD={}'.format('{SQL Server}', host,database,user,password))
  10. cursor = connection.cursor()
  11. cursor.fast_executemany = True
  12.  
  13. ddl = '''
  14. IF OBJECT_ID('[dbo].[emtest]') IS NOT NULL
  15. DROP TABLE [dbo].[emtest]
  16.  
  17. CREATE TABLE [dbo].[emtest](
  18. [OBJECTID] [int] IDENTITY(1,1) NOT NULL,
  19. [Shape] [geometry] NULL,
  20. [LINK_ID] [numeric](10, 0) NULL,
  21. [NAME1] [nvarchar](254) NULL,
  22. [NAME2] [nvarchar](254) NULL,
  23. [FROM_NODE] [numeric](10, 0) NULL,
  24. [TO_NODE] [numeric](10, 0) NULL,
  25. [SPEEDCAR_T] [int] NULL,
  26. [SPEEDCAR_B] [int] NULL,
  27. [SPEEDTRU_T] [int] NULL,
  28. [SPEEDTRU_B] [int] NULL,
  29. [VMAX_CAR_T] [int] NULL,
  30. [VMAX_CAR_B] [int] NULL,
  31. [VMAX_TRU_T] [int] NULL,
  32. [VMAX_TRU_B] [int] NULL,
  33. [ACCESS_TOW] [numeric](10, 0) NULL,
  34. [ACCESS_BKW] [numeric](10, 0) NULL,
  35. [LENGTH] [numeric](7, 2) NULL,
  36. [FRC] [int] NULL,
  37. [CAP_TOW] [numeric](10, 0) NULL,
  38. [CAP_BKW] [numeric](10, 0) NULL,
  39. [LANES_TOW] [numeric](2, 1) NULL,
  40. [LANES_BKW] [numeric](2, 1) NULL,
  41. [FORMOFWAY] [int] NULL,
  42. [BRUNNEL] [int] NULL,
  43. [MAXHEIGHT] [numeric](4, 1) NULL,
  44. [MAXWIDTH] [numeric](4, 1) NULL,
  45. [MAXPRESS] [numeric](4, 1) NULL,
  46. [ABUTTERCAR] [int] NULL,
  47. [ABUTTERTRU] [int] NULL,
  48. [URBAN] [numeric](10, 0) NULL,
  49. [WIDTH] [numeric](4, 1) NULL,
  50. [INT_LEVEL] [numeric](3, 1) NULL,
  51. [TOLL] [int] NULL,
  52. [BAUSTATUS] [int] NULL,
  53. [SUBNET_ID] [numeric](10, 0) NULL,
  54. [ONEWAY_CAR] [int] NULL,
  55. [ONEWAY_BK] [int] NULL,
  56. [ONEWAY_BUS] [int] NULL,
  57. [EDGE_ID] [numeric](19, 0) NULL,
  58. [EDGECAT] [nvarchar](3) NULL,
  59. [REGCODE] [nvarchar](31) NULL,
  60. [SUSTAINER] [nvarchar](19) NULL,
  61. [SDE_ID] [numeric](10, 0) NULL,
  62. [Shape_STLe] [numeric](38, 8) NULL
  63. )'''
  64.  
  65. cursor.execute(ddl)
  66. cursor.commit()
  67.  
  68.  
  69. sql = 'INSERT INTO emtest (SDE_ID,WIDTH,TO_NODE,CAP_TOW,URBAN,VMAX_CAR_T,LENGTH,LANES_BKW,SHAPE,SPEEDTRU_T,SUSTAINER,REGCODE,MAXPRESS,INT_LEVEL,SPEEDCAR_T,FROM_NODE,SPEEDTRU_B,ONEWAY_BUS,ABUTTERCAR,TOLL,MAXWIDTH,MAXHEIGHT,NAME1,NAME2,CAP_BKW,SPEEDCAR_B,ACCESS_TOW,EDGECAT,EDGE_ID,LANES_TOW,FRC,ONEWAY_CAR,LINK_ID,VMAX_TRU_B,VMAX_TRU_T,ACCESS_BKW,FORMOFWAY,SUBNET_ID,VMAX_CAR_B,ABUTTERTRU,ONEWAY_BK,BAUSTATUS,BRUNNEL) Values (?,?,?,?,?,?,?,?,geometry::STGeomFromWKB(?, 31287),?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)'
  70.  
  71. ## this results in an error "pyodbc.DataError: ('String data, right truncation: length 89 buffer 73', '22001')"
  72. params = [
  73. (6429256, 5.0, 18128899, -1, 1, -1, 50.17, 1.0, b"\x01\x02\x00\x00\x00\x04\x00\x00\x00\x86\x8d_\xa93O A\x98\xe9\xfa\xd4\xd0w\x1dA\xa9\x17#\x8bBO A\x12\xdb\xdb1\xb6w\x1dA\x8e\x9b \xc0vO A\xeeb\xa5'pw\x1dA\xeaWd;\x87O A\xc8'\xbf\x8ddw\x1dA", 0, '31523', 'AT31523', -1.0, 0.0, 15, 18017250, 0, 2, 0, -1, -1.0, -1.0, 'Untererla', None, -1, 15, 2383631, 'G', 18435988.0, 1.0, 12, 2, 23033181, -1, -1, 2383631, 3, 407, -1, 0, 2, 5, -1),
  74. (6428457, 5.0, 1579262, -1, 1, -1, 122.96, 1.0, b'\x01\x02\x00\x00\x00\x05\x00\x00\x00\xb8i\x84\x80>\t"A\xe9\xa8+-\xa7W\x1eA\xd5\xad~\x83D\t"A\xcc\xf1\xa1\xb8>Y\x1eA\x08w\x82\x8eG\t"Aw\xcb|\xb3rY\x1eA\x98R\xa3\nI\t"AQT\xb6r\x80Y\x1eA\xc6g\xec\xb3K\t"A\x9b\x1a\x8c9\x91Y\x1eA', 0, '32141', 'AT32141', -1.0, 0.0, 34, 18062246, 0, 2, 0, -1, -1.0, -1.0, 'Hauptstraße', 'Zwentendorf a.d.Donau', -1, 34, 2383631, 'GW', 18318305.0, 1.0, 5, 2, 23028031, -1, -1, 2383631, 3, 407, -1, 0, 2, 5, -1)
  75. ]
  76.  
  77. ## this works
  78. #params = [
  79. # (6428457, 5.0, 1579262, -1, 1, -1, 122.96, 1.0, b'\x01\x02\x00\x00\x00\x05\x00\x00\x00\xb8i\x84\x80>\t"A\xe9\xa8+-\xa7W\x1eA\xd5\xad~\x83D\t"A\xcc\xf1\xa1\xb8>Y\x1eA\x08w\x82\x8eG\t"Aw\xcb|\xb3rY\x1eA\x98R\xa3\nI\t"AQT\xb6r\x80Y\x1eA\xc6g\xec\xb3K\t"A\x9b\x1a\x8c9\x91Y\x1eA', 0, '32141', 'AT32141', -1.0, 0.0, 34, 18062246, 0, 2, 0, -1, -1.0, -1.0, 'Hauptstraße', 'Zwentendorf a.d.Donau', -1, 34, 2383631, 'GW', 18318305.0, 1.0, 5, 2, 23028031, -1, -1, 2383631, 3, 407, -1, 0, 2, 5, -1)
  80. #]
  81.  
  82. cursor.executemany(sql, params)
  83. cursor.commit()
Add Comment
Please, Sign In to add comment