Advertisement
medajibka

Пример работы с sqlite в Python

Apr 12th, 2023 (edited)
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. import sqlite3
  2.  
  3. con = sqlite3.connect("kvant.db")
  4.  
  5. cursor = con.cursor()
  6.  
  7. cursor.execute(""" CREATE TABLE IF NOT EXISTS product
  8.                        (id INTEGER PRIMARY KEY AUTOINCREMENT,
  9.                        name TEXT,
  10.                        type TEXT)
  11.               """)
  12.  
  13.  
  14. cursor.execute(""" CREATE TABLE IF NOT EXISTS place
  15.                        (id INTEGER PRIMARY KEY AUTOINCREMENT,
  16.                        name TEXT,
  17.                        x_cord INTEGER,
  18.                        y_cord INTEGER)
  19.               """)
  20.  
  21. cursor.execute(""" CREATE TABLE IF NOT EXISTS 'order'
  22.                        (id INTEGER PRIMARY KEY AUTOINCREMENT,
  23.                        product_id INTEGER,
  24.                        place_id INTEGER,
  25.                        delivery_time INTEGER,
  26.                        FOREIGN KEY (product_id) REFERENCES product (id) ON DELETE CASCADE,
  27.                        FOREIGN KEY (place_id) REFERENCES place (id) ON DELETE CASCADE)
  28.                """)
  29.  
  30. products = [("Baikal", "Gazirovka"), ("Tarhun", "Gazirovka"), ("Snikers", "Chocolate"), ("Vkusnoteevo", "Milk"), ("Yura", "Tubik")]
  31.  
  32. cursor.executemany("INSERT INTO product (name, type) VALUES (? , ?)", products)
  33.  
  34. con.commit()
  35.  
  36. print(cursor.execute(""" SELECT * FROM product """).fetchall())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement