Advertisement
Jakzon123

Mod 13 Column Store DB

May 3rd, 2023 (edited)
835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.53 KB | None | 0 0
  1. # Create SQLite DB
  2.  
  3. import sqlite3
  4. import pandas
  5.  
  6. conn = sqlite3.connect('bomojobrandindices.db')
  7. cur = conn.cursor()
  8.  
  9. # Drops the table you are going to create if it exists
  10. conn.execute("""DROP TABLE IF EXISTS bomojobrandindices""")
  11.  
  12. # commit the changes to db
  13. conn.commit()
  14.  
  15. # Creates a table that contains all the attributes in your data file
  16. cur.execute("""CREATE TABLE bomojobrandindices (Brand TEXT, Total INTEGER, Releases INTEGER, "#1 Release" INTEGER, "Lifetime Gross" INTEGER)""")
  17.  
  18. # commit the changes to db
  19. conn.commit()
  20.  
  21. df = pandas.read_csv('bomojobrandindices.csv')
  22. print(df.columns)
  23.  
  24. df.to_sql('bomojobrandindices', conn, if_exists='append', index = False)
  25.  
  26. cur.execute("""SELECT * FROM bomojobrandindices""")
  27. print(cur.fetchall())
  28.  
  29. # close connection
  30. conn.close()
  31. print('Connection closed.')
  32.  
  33. ################################
  34.  
  35. # Create Column Store DB
  36.  
  37. import monetdbe
  38. import pandas
  39.  
  40. # Path to the database file you want to create
  41. database = 'bomojobrandindices.mdbe'
  42.  
  43. try:
  44.     with monetdbe.connect(database) as conn:
  45.         # Drops the table you are going to create if it exists
  46.         conn.set_autocommit(True)
  47.         conn.execute("""DROP TABLE IF EXISTS bomojobrandindices""")
  48.  
  49.         # Creates a table that contains all the attributes in your data file
  50.         # attributes are Brand (string), Total (int), Releases (int), #1 Release (int),  Lifetime Gross (int)
  51.         conn.execute("""CREATE TABLE bomojobrandindices
  52.        (Brand TEXT, Total INTEGER, Releases INTEGER, "#1 Release" INTEGER, "Lifetime Gross" INTEGER)""")
  53.  
  54.         # Load the table created with the data from the data file you selected
  55.         conn.execute(f"""COPY OFFSET 2 INTO bomojobrandindices FROM R'C:\\Users\Jackson McAfee\\Documents\\Programming\\mod13\\bomojobrandindices.csv' USING DELIMITERS ',' NULL AS '' BEST EFFORT""")
  56.  
  57.     with monetdbe.connect(database) as conn:
  58.         # Print the results
  59.         print("bomojobrandindices Load Errors")
  60.         results = conn.execute("""SELECT * FROM sys.rejects""")
  61.         for r in results.fetchall():
  62.             print(r)
  63.  
  64.     conn.close()
  65.  
  66. except Exception as inst:
  67.     print(type(inst))
  68.     print(inst.args)
  69.  
  70. ################################
  71.  
  72. # Query Good For Row Store
  73.  
  74. import sqlite3
  75. import monetdbe
  76. import time
  77.  
  78. # Path to the database file you want to access
  79. database = 'bomojobrandindices.mdbe'
  80.  
  81. monetdb_conn = monetdbe.connect(database)
  82.  
  83. # Execute query and measure execution time
  84. start_time_monetdb = time.time()
  85.  
  86. # Query monetdb
  87. query_monetdb = 'SELECT AVG("Lifetime Gross") AS avg_lifetime_gross FROM bomojobrandindices'
  88. result_monetdb = monetdb_conn.execute(query_monetdb).fetchall()
  89.  
  90. end_time_monetdb = time.time()
  91. execution_time_monetdb = end_time_monetdb - start_time_monetdb
  92.  
  93. # Display results and execution time for MonetDB
  94. print("Results from MonetDB:")
  95. print(result_monetdb)
  96. print("Execution Time (MonetDB): {:.6f} seconds".format(execution_time_monetdb))
  97.  
  98. # Connect to the SQLite database
  99. sqlite_conn = sqlite3.connect('bomojobrandindices.db')
  100. cur = sqlite_conn.cursor()
  101.  
  102. # Execute query and measure execution time
  103. start_time_sqlite = time.time()
  104.  
  105. # query sqlite
  106. query_sqlite = 'SELECT AVG("Lifetime Gross") AS avg_lifetime_gross FROM bomojobrandindices'
  107. cur.execute(query_sqlite)
  108. result_sqlite = cur.fetchall()
  109.  
  110. end_time_sqlite = time.time()
  111. execution_time_sqlite = end_time_sqlite - start_time_sqlite
  112.  
  113. # Display results and execution time for SQLite
  114. print("Results from SQLite:")
  115. print(result_sqlite)
  116. print("Execution Time (SQLite): {:.6f} seconds".format(execution_time_sqlite))
  117.  
  118. # Close connections
  119. monetdb_conn.close()
  120. sqlite_conn.close()
  121.  
  122. ################################
  123.  
  124. # Query Good for Column Store
  125.  
  126. import sqlite3
  127. import monetdbe
  128. import time
  129.  
  130. # Path to the database file you want to access
  131. database = 'bomojobrandindices.mdbe'
  132.  
  133. monetdb_conn = monetdbe.connect(database)
  134.  
  135. # Execute query and measure execution time
  136. start_time_monetdb = time.time()
  137.  
  138. # Query monetdb
  139. query_monetdb = '''SELECT SUM("Lifetime Gross") AS total_lifetime_gross, Brand
  140.               FROM bomojobrandindices
  141.               GROUP BY Brand
  142.               ORDER BY total_lifetime_gross DESC;'''
  143. result_monetdb = monetdb_conn.execute(query_monetdb).fetchall()
  144.  
  145. end_time_monetdb = time.time()
  146. execution_time_monetdb = end_time_monetdb - start_time_monetdb
  147.  
  148. # Display results and execution time for MonetDB
  149. print("Results from MonetDB:")
  150. for row in result_monetdb:
  151.     print("Brand:", row[1])
  152.     print("Total Lifetime Gross:", row[0])
  153.     print("-----")
  154. print("Execution Time (MonetDB): {:.6f} seconds".format(execution_time_monetdb))
  155.  
  156. # Connect to the SQLite database
  157. sqlite_conn = sqlite3.connect('bomojobrandindices.db')
  158. cur = sqlite_conn.cursor()
  159.  
  160. # Execute query and measure execution time
  161. start_time_sqlite = time.time()
  162.  
  163. # query sqlite
  164. query_sqlite = '''SELECT SUM("Lifetime Gross") AS total_lifetime_gross, Brand
  165.               FROM bomojobrandindices
  166.               GROUP BY Brand
  167.               ORDER BY total_lifetime_gross DESC;'''
  168. cur.execute(query_sqlite)
  169. result_sqlite = cur.fetchall()
  170.  
  171. end_time_sqlite = time.time()
  172. execution_time_sqlite = end_time_sqlite - start_time_sqlite
  173.  
  174. # Display results and execution time for SQLite
  175. print("Results from SQLite:")
  176. for row in result_sqlite:
  177.     print("Brand:", row[1])
  178.     print("Total Lifetime Gross:", row[0])
  179.     print("-----")
  180. print("Execution Time (SQLite): {:.6f} seconds".format(execution_time_sqlite))
  181.  
  182. # Close connections
  183. monetdb_conn.close()
  184. sqlite_conn.close()
  185.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement