convert sql to python

Solutions on MaxInterview for convert sql to python by the best coders in the world

showing results for - "convert sql to python"
Nicola
22 Jan 2017
1# credit to Stack Overflow user in the source link
2# NOTE: this is ust an example, you have to adapt the query and other stuff to your needs
3
4import sqlite3
5import json
6
7query = """
8{
9    "ID": "4",
10    "Name": "David",
11    "Transformation": "SELECT ID || Name AS NewField FROM inputdata"
12}"""
13
14query_dict = json.loads(query)
15
16db = sqlite3.Connection('mydb')
17db.execute('create table inputdata ({} VARCHAR(100));'.format(' VARCHAR(100), '.join(query_dict.keys())))
18db.execute('insert into inputdata ({}) values ("{}")'.format(','.join(query_dict.keys()),'","'.join(query_dict.values())))
19
20r = db.execute(query_dict['Transformation'])
21response = {}
22response[r.description[0][0]] = r.fetchone()[0]
23
24print(response)
25>>> {'NewField': '4David'}
26
27db.execute('drop table inputdata;')
28db.close()
29