SHARE
TWEET

Untitled

a guest Sep 16th, 2019 101 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import db_config
  2.  
  3.  
  4. import sqlite3
  5. from sqlite3 import Error
  6.  
  7.  
  8. def create_connection(db_file):
  9.     """ create a database connection to the SQLite database
  10.         specified by the db_file
  11.     :param db_file: database file
  12.     :return: Connection object or None
  13.     """
  14.     conn = None
  15.     try:
  16.         conn = sqlite3.connect(db_file)
  17.     except Error as e:
  18.         print(e)
  19.  
  20.     return conn
  21.  
  22.  
  23. def select_all_sample1(conn):
  24.     """
  25.     Query all rows in the tasks table
  26.     :param conn: the Connection object
  27.     :return:
  28.     """
  29.     cur = conn.cursor()
  30.     cur.execute("SELECT * FROM sample1")
  31.  
  32.     rows = cur.fetchall()
  33.     names = [description[0]
  34.              for description in cur.description]  # to extract the table headers
  35.     data = {}  # bank dictonary
  36.  
  37.     for row in rows:
  38.         # print(row)
  39.  
  40.         for i in range(0, len(names)):
  41.             data['app'] = 'DSL' # adding custom flags for splunk
  42.             data['app-category'] = 'IL' # adding custom flags for splunk
  43.             data['Environment'] = 'DEV' # adding custom flags for splunk
  44.             data[names[i]] = row[i]
  45.  
  46.         print(data)
  47.  
  48.  
  49. def select_task_by_priority(conn, priority):
  50.     """
  51.     Query tasks by priority
  52.     :param conn: the Connection object
  53.     :param priority:
  54.     :return:
  55.     """
  56.     cur = conn.cursor()
  57.     cur.execute("SELECT * FROM tasks WHERE priority=?", (priority,))
  58.  
  59.     rows = cur.fetchall()
  60.  
  61.     for row in rows:
  62.         print(row)
  63.  
  64.  
  65. def main():
  66.     database = r"/home/alamin/allianz/testDB"
  67.  
  68.     # create a database connection
  69.     conn = create_connection(database)
  70.     with conn:
  71.         #print("1. Query task by priority:")
  72.         #select_task_by_priority(conn, 1)
  73.         print("connected")
  74.         print("1. Query all sample1")
  75.         select_all_sample1(conn)
  76.  
  77.  
  78. if __name__ == '__main__':
  79.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top