Advertisement
AtEchoOff

SQL for people table

Jul 27th, 2024 (edited)
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. -- Create the 'people' table
  2. CREATE TABLE people (
  3. ID INT PRIMARY KEY AUTO_INCREMENT,
  4. first_name VARCHAR(50),
  5. last_name VARCHAR(50),
  6. age INT
  7. );
  8.  
  9. -- Insert random data into the 'people' table
  10. INSERT INTO people (first_name, last_name, age) VALUES
  11. ('John', 'Doe', 28),
  12. ('Jane', 'Smith', 34),
  13. ('Michael', 'Johnson', 45),
  14. ('Emily', 'Davis', 22),
  15. ('Daniel', 'Brown', 30),
  16. ('Sophia', 'Wilson', 27),
  17. ('James', 'Jones', 40),
  18. ('Olivia', 'Garcia', 25),
  19. ('Matthew', 'Martinez', 35),
  20. ('Emma', 'Anderson', 29);
  21.  
  22.  
  23. python version:
  24.  
  25. import sqlite3
  26.  
  27. # Connect to the SQLite database (or create it if it doesn't exist)
  28. conn = sqlite3.connect('example.db')
  29. cursor = conn.cursor()
  30.  
  31. # Create the 'people' table
  32. cursor.execute('''
  33. CREATE TABLE IF NOT EXISTS people (
  34. ID INTEGER PRIMARY KEY AUTOINCREMENT,
  35. first_name TEXT,
  36. last_name TEXT,
  37. age INTEGER
  38. )
  39. ''')
  40.  
  41. # Insert random data into the 'people' table
  42. people_data = [
  43. ('John', 'Doe', 28),
  44. ('Jane', 'Smith', 34),
  45. ('Michael', 'Johnson', 45),
  46. ('Emily', 'Davis', 22),
  47. ('Daniel', 'Brown', 30),
  48. ('Sophia', 'Wilson', 27),
  49. ('James', 'Jones', 40),
  50. ('Olivia', 'Garcia', 25),
  51. ('Matthew', 'Martinez', 35),
  52. ('Emma', 'Anderson', 29)
  53. ]
  54.  
  55. cursor.executemany('''
  56. INSERT INTO people (first_name, last_name, age) VALUES (?, ?, ?)
  57. ''', people_data)
  58.  
  59. # Commit the transaction
  60. conn.commit()
  61.  
  62. # Select all data from the 'people' table to verify the inserts
  63. cursor.execute('SELECT * FROM people')
  64. rows = cursor.fetchall()
  65. for row in rows:
  66. print(row)
  67.  
  68. # Close the connection
  69. conn.close()
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement