skip420

3partdbMaker.py

Sep 13th, 2021 (edited)
761
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.48 KB | None | 0 0
  1. # 3 part db python files
  2.  
  3.  
  4.  
  5. # python db.py
  6. import sqlite3
  7. import pandas as pd
  8.  
  9. conn = sqlite3.connect('test_database')
  10. c = conn.cursor()
  11.                    
  12. c.execute('''
  13.          SELECT
  14.          a.product_name,
  15.          b.price
  16.          FROM products a
  17.          LEFT JOIN prices b ON a.product_id = b.product_id
  18.          ''')
  19.  
  20. df = pd.DataFrame(c.fetchall(), columns=['product_name','price'])
  21. print (df)
  22.  
  23.  
  24.  
  25. #python d.py
  26.  
  27. import sqlite3
  28.  
  29. conn = sqlite3.connect('test_database')
  30. c = conn.cursor()
  31.  
  32. c.execute('''
  33.          CREATE TABLE IF NOT EXISTS products
  34.          ([product_id] INTEGER PRIMARY KEY, [product_name] TEXT)
  35.          ''')
  36.          
  37. c.execute('''
  38.          CREATE TABLE IF NOT EXISTS prices
  39.          ([product_id] INTEGER PRIMARY KEY, [price] INTEGER)
  40.          ''')
  41.                      
  42. conn.commit()
  43.  
  44.  
  45.  
  46. # python ddb,py
  47.  
  48. import sqlite3
  49.  
  50. conn = sqlite3.connect('test_database')
  51. c = conn.cursor()
  52.                    
  53. c.execute('''
  54.          INSERT INTO products (product_id, product_name)
  55.  
  56.                VALUES
  57.                (1,'Computer'),
  58.                (2,'Printer'),
  59.                (3,'Tablet'),
  60.                (4,'Desk'),
  61.                (5,'Chair')
  62.          ''')
  63.  
  64. c.execute('''
  65.          INSERT INTO prices (product_id, price)
  66.  
  67.                VALUES
  68.                (1,800),
  69.                (2,200),
  70.                (3,300),
  71.                (4,450),
  72.                (5,150)
  73.          ''')
  74.  
  75. conn.commit()
Add Comment
Please, Sign In to add comment