0

I have yaml file that contains the data that needs to go into the table, i need to convert each yaml object to sql. can anyone tell me what I can use to convert it to sql statement

for example,

- model: ss
  pk: 2
  fields: {created_by: xxx, created_date: !!timestamp '2018-09-13 
    17:50:30.821769+00:00',
   modified_by: null, modified_date: null, record_version: 0, team_name: 
   privat, team_type: abc}
Anthon
  • 69,918
  • 32
  • 186
  • 246
skidwa
  • 103
  • 3
  • 11
  • https://stackoverflow.com/a/42054860/8166034 this answer has an example on how to parse a YAML file with Python. After extracting the values, you could then construct the SQL statement. – Teemu Apr 25 '19 at 17:14
  • And this one how to generate a query https://stackoverflow.com/questions/9336270/using-a-python-dict-for-a-sql-insert-statement – Serge Apr 25 '19 at 17:24

1 Answers1

0

In python

data_loaded = None

with open("data.yaml", 'r') as stream:
    data_loaded = yaml.load(stream)

if data_loaded:
   cols, vals = data_loaded["fields"].items()
   table = data_loaded["model"]
   sql = "INSERT INTO %s %s VALUES %s;" % ( table, tuple(cols), tuple(vals))

Depending with SQL dialect you might need take additional care of literal, types (e.g. replacing double quotes with singles, timestamp format etc)

Serge
  • 3,387
  • 3
  • 16
  • 34