Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.67 KB | None | 0 0
  1. import sqlite3, csv
  2. from pprint import pprint
  3.  
  4.  
  5. sqlite_file = 'mydb_NEW.db' # name of db file
  6.  
  7. # connect to database:
  8. conn = sqlite3.connect(sqlite_file)
  9. conn.text_factory = str
  10.  
  11. # Get a cursor object
  12. cur = conn.cursor() # Return a cursor for the connection.
  13.  
  14. '''nodes_tags section'''
  15.  
  16. cur.execute('''DROP TABLE IF EXISTS nodes_tags''') # exucutes an sql statement
  17. conn.commit()
  18.  
  19.  
  20. # Create the table, specifying the column names and data types:
  21. cur.execute('''
  22.    CREATE TABLE nodes_tags(id INTEGER, key TEXT, value TEXT,type TEXT)
  23. ''')
  24. # commit the changes
  25. conn.commit()
  26.  
  27.  
  28. # open nodes_tags.csv
  29. with open('nodes_tags.csv', 'rb') as fin:
  30.     dr = csv.DictReader(fin) # comma is default delimiter
  31.     to_db = [(i['id'], i['key'], i['value'], i['type']) for i in dr]
  32.    
  33.  
  34.    
  35.  
  36.  
  37. # inseart the formatted data
  38. cur.executemany(
  39.     'INSERT INTO nodes_tags(id, key, value,type) VALUES (?, ?, ?, ?);', to_db)
  40.    
  41. conn.commit()
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49. ''' nodes section'''
  50.  
  51. cur.execute('''DROP TABLE IF EXISTS nodes''') # exucutes an sql statement
  52. conn.commit()
  53.  
  54.  
  55. cur.execute('''
  56.    CREATE TABLE nodes(id INTEGER, lat DECIMAL, lon DECIMAL, user TEXT, uid INTEGER, version INTEGER , changeset INTEGER, timestamp TIMESTAMP)
  57. ''')
  58. # commit the changes
  59. conn.commit()
  60. # Read the csv file as a dictionary,
  61. # then format as a list of tuples
  62.  
  63. # open nodes.csv
  64. # id    lat lon user    uid version changeset   timestamp
  65. with open('nodes.csv', 'rb') as fin:
  66.     dr = csv.DictReader(fin)
  67.     to_db = [(i['id'], i['lat'], i['lon'], i['user'], i['uid'], i['version'], i['changeset'], i['timestamp']) for i in dr]
  68.    
  69. cur.executemany('INSERT INTO nodes(id, lat, lon, user, uid, version, changeset, timestamp) VALUES (?,?,?,?,?,?,?,?);', to_db)
  70.  
  71.  
  72.  
  73.  
  74.  
  75. ''' ways section'''
  76.  
  77. cur.execute('''DROP TABLE IF EXISTS ways''') # exucutes an sql statement
  78. conn.commit()
  79.  
  80. #id user    uid version changeset   timestamp
  81.  
  82.  
  83. cur.execute('''
  84.    CREATE TABLE ways(id INTEGER, user TEXT, uid INTEGER, version INTEGER , changeset INTEGER, timestamp TIMESTAMP)
  85. ''')
  86. # commit the changes
  87. conn.commit()
  88. # Read the csv file as a dictionary,
  89. # then format as a list of tuples
  90.  
  91. # open ways.csv
  92. with open('ways.csv', 'rb') as fin:
  93.     dr = csv.DictReader(fin)
  94.     to_db = [(i['id'], i['user'], i['uid'], i['version'], i['changeset'], i['timestamp']) for i in dr]
  95.    
  96. cur.executemany('INSERT INTO ways(id, user, uid, version, changeset, timestamp) VALUES (?,?,?,?,?,?);', to_db)
  97.  
  98.  
  99.  
  100.  
  101. ''' ways nodes section'''
  102.  
  103. cur.execute('''DROP TABLE IF EXISTS ways_nodes''') # exucutes an sql statement
  104. conn.commit()
  105.  
  106. #id node_id position
  107.  
  108.  
  109. cur.execute('''
  110.    CREATE TABLE ways_nodes(id INTEGER, node_id INTEGER, position INTEGER)
  111. ''')
  112. # commit the changes
  113. conn.commit()
  114. # Read the csv file as a dictionary,
  115. # then format as a list of tuples
  116.  
  117. # open ways_nodes.csv
  118. with open('ways_nodes.csv', 'rb') as fin:
  119.     dr = csv.DictReader(fin)
  120.     to_db = [(i['id'], i['node_id'], i['position']) for i in dr]
  121.    
  122. cur.executemany('INSERT INTO ways_nodes(id, node_id, position) VALUES (?,?,?);', to_db)
  123.  
  124.  
  125.  
  126. ''' ways tags section'''
  127.  
  128. cur.execute('''DROP TABLE IF EXISTS ways_tags''') # exucutes an sql statement
  129. conn.commit()
  130.  
  131.  
  132. #id key value   type
  133.  
  134. cur.execute('''
  135.    CREATE TABLE ways_tags(id INTEGER, key TEXT, value TEXT, type TEXT)
  136. ''')
  137. # commit the changes
  138. conn.commit()
  139. # Read the csv file as a dictionary,
  140. # then format as a list of tuples
  141.  
  142. # open ways_nodes.csv
  143. with open('ways_tags.csv', 'rb') as fin:
  144.     dr = csv.DictReader(fin)
  145.     to_db = [(i['id'], i['key'], i['value'], i['type']) for i in dr]
  146.    
  147. cur.executemany('INSERT INTO ways_tags(id, key, value, type) VALUES (?,?,?,?);', to_db)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement