49 lines
1.3 KiB
Python
49 lines
1.3 KiB
Python
import sys
|
|
import logging
|
|
import logging.handlers
|
|
import re
|
|
import sqlite3
|
|
|
|
con = None
|
|
|
|
def query(query_string, values=[]):
|
|
global con
|
|
|
|
if con is None:
|
|
con = sqlite3.connect("database.db")
|
|
con.row_factory = sqlite3.Row
|
|
|
|
cur = con.cursor()
|
|
cur.execute(query_string, values)
|
|
rows = cur.fetchall()
|
|
con.commit()
|
|
|
|
return rows
|
|
|
|
def insert(table_name, obj):
|
|
columns = ""
|
|
q_marks = ""
|
|
values = []
|
|
typed_columns = "'id' integer primary key autoincrement, "
|
|
|
|
for k in obj.keys():
|
|
value = obj[k]
|
|
columns = columns + f"'{k}', "
|
|
q_marks = q_marks + "?, "
|
|
values.append(value)
|
|
|
|
type = "text"
|
|
if isinstance(type, int): type = "integer"
|
|
if isinstance(type, float): type = "real"
|
|
|
|
typed_columns = typed_columns + f"'{k}' {type} null, "
|
|
|
|
typed_columns = typed_columns + "'inserted_on' timestamp default current_timestamp, "
|
|
columns = columns.rstrip(", ")
|
|
q_marks = q_marks.rstrip(", ")
|
|
typed_columns = typed_columns.rstrip(", ")
|
|
query_str = f"INSERT into '{table_name}' ({columns}) values ({q_marks})"
|
|
|
|
query(f"CREATE table if not exists '{table_name}' ({typed_columns})")
|
|
query(query_str, values)
|