Advertisement
teslariu

ejercicio sqlite

Sep 6th, 2023
1,170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. import sqlite3
  5.  
  6. conn = sqlite3.connect("productos.sqlite")
  7. cursor = conn.cursor()
  8.  
  9. cursor.execute("CREATE TABLE IF NOT EXISTS productos(id NUMERIC, nombre TEXT, precio NUMERIC)")
  10.  
  11. conn.commit()
  12.  
  13.  
  14. productos = (
  15.         (1,'Teclado',25),
  16.         (2,"Mouse", 18),
  17.         (3,'Monitor',300),
  18.         (4,'Parlantes',100)
  19. )
  20.  
  21. for cod,nombre,precio in productos:
  22.     cursor.execute(f"INSERT INTO productos VALUES(?,?,?)",(cod,nombre,precio))
  23.    
  24. conn.commit()
  25.  
  26. cursor.execute("SELECT * FROM productos")
  27. datos = cursor.fetchall()
  28. print(datos)
  29.  
  30. # cerrar la base
  31. conn.close()
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement