custom_data_tables.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import sqlite3
  2. def create_custom_datatable(database_path, columns: list or dict, table_name=''):
  3. if table_name == '':
  4. return
  5. # Create a String
  6. query_str = ''#
  7. if isinstance(columns, list):
  8. for name_count in range(0, len(columns)):
  9. query_str += columns[name_count] + ' VARCHAR, '
  10. else:
  11. for key,v in columns.items():
  12. query_str += key + ' VARCHAR, '
  13. query_str= 'CREATE TABLE IF NOT EXISTS ' + table_name + '(' \
  14. + query_str[:-2] + ')'
  15. # Connect to database
  16. connection = sqlite3.connect(database_path)
  17. cursor = connection.cursor()
  18. cursor.execute(query_str)
  19. connection.commit()
  20. connection.close()
  21. def add_row(database_path, columns:dict, table_name =''):
  22. if table_name == '':
  23. return
  24. # Create a String
  25. column_names_str = ''
  26. values_list = []
  27. for key,value in columns.items():
  28. column_names_str += key + ', '
  29. values_list += [str(value)]
  30. add_questionmarks = '?,' * len(values_list)
  31. query_str = 'INSERT OR IGNORE INTO ' + table_name + '(' + column_names_str[:-2]+ ') VALUES(' + add_questionmarks[:-1] + ')'
  32. # Connect to database
  33. connection = sqlite3.connect(database_path)
  34. cursor = connection.cursor()
  35. cursor.execute(query_str, values_list)
  36. connection.commit()
  37. connection.close()
  38. def delete_rows_based_on_high_value(database_path,value, column:str, table_name =''):
  39. if table_name == '':
  40. return
  41. # Create a String
  42. query_str = 'DELETE FROM ' + table_name + ' WHERE ' + 'CAST('+ column+ ' as decimal)' + ' > ' + str(value)
  43. # Connect to database
  44. connection = sqlite3.connect(database_path)
  45. cursor = connection.cursor()
  46. cursor.execute(query_str)
  47. connection.commit()
  48. connection.close()