Created
October 10, 2019 09:40
-
-
Save dlyapun/d6cb68880f188a3935b3f9c19a717134 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
conn = sqlite3.connect(":memory") | |
cursor = conn.cursor() | |
sql_command = 'CREATE TABLE STUDENTS ( ID INTEGER PRIMARY KEY, FIRST_NAME VARCHAR(50) NOT NULL, LAST_NAME VARCHAR(50) NOT NULL, ADDRESS VARCHAR(100) );' | |
cursor.execute(sql_command) | |
sql_command = "INSERT INTO students VALUES (2, 'Ivan', 'Ivanon', 'My address');" | |
cursor.execute(sql_command) | |
conn.commit() | |
sql_command = "INSERT INTO students VALUES (1, 'DIma', 'Test', 'My address');" | |
cursor.execute(sql_command) | |
conn.commit() | |
sql_command = "SELECT * FROM Students" | |
cursor.execute(sql_command) | |
result = cursor.fetchall() | |
serializer = ['id', 'first_name', 'last_name', 'address'] | |
serializer_data = [] | |
for row in result: | |
item_data = {} | |
for index, column_item in enumerate(row): | |
item_data[serializer[index]] = column_item | |
serializer_data.append(item_data) | |
print('serializer_data: ', serializer_data) | |
# serializer_data: [ | |
# { | |
# 'id': 1, | |
# 'first_name': 'DIma', | |
# 'last_name': 'Test', | |
# 'address': 'My address' | |
# }, | |
# { | |
# 'id': 2, | |
# 'first_name': 'Ivan', | |
# 'last_name': 'Ivanon', | |
# 'address': 'My address' | |
# } | |
# ] | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment