Advertisement
Dammiejoy20

Untitled

Apr 28th, 2020
1,565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.31 KB | None | 0 0
  1.  
  2. import sqlite3
  3.  
  4. # Establishing a connection with the database
  5. con = sqlite3.connect("my_database.db")
  6.  
  7. # Cursor object is connected using the connection created
  8. cur = con.cursor()
  9.  
  10.  
  11. def create_student_table(con):
  12.     cur = con.cursor()
  13.  
  14. # Creates student table
  15.     cur.execute("CREATE TABLE IF NOT EXISTS students(roll_no INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL,"
  16.                    " age INTEGER NOT NULL, course TEXT NOT NULL, email TEXT NOT NULL, phone INTEGER)")
  17.  
  18.  
  19.  
  20. def insert_student_table(roll_no, name, age, course, email, phone):
  21.     cur = con.cursor()
  22.  
  23. # Inserts values into table created
  24.     cur.execute("INSERT OR REPLACE INTO students VALUES(?, ?, ?, ?, ?, ?)",  (roll_no, name, age, course, email, phone))
  25.  
  26. con.commit()
  27.  
  28.  
  29. create_student_table(con)
  30. insert_student_table(1, 'Wendy', 17, 'English Language', 'charles10@gmail.com', 8123456787)
  31. insert_student_table(2, 'Peter', 15, 'Chemical Engineering', 'peter20@yahoo.com', 9654676734)
  32. insert_student_table(3, 'Mercy', 19, 'Theatre Arts', 'mercy30@gmail.com', 8056765476)
  33. insert_student_table(4, 'Micheal', 16, 'Accounting', 'micheal40@gmail,com', 906742110)
  34. insert_student_table(5, 'Tina', 20, 'Statistics', 'tina50@yahoo.com', 812300780)
  35.  
  36.  
  37. def select_student_table():
  38.     cur = con.cursor()
  39.  
  40. # Select all the table
  41.     cur.execute("SELECT * FROM students")
  42.  
  43. con.commit()
  44.  
  45. def fetch_student_table():
  46.     cur = con.cursor()
  47. # This fetches all the rows in the table
  48.     cur.execute("SELECT * FROM students")
  49.  
  50.     rows = cur.fetchall()
  51.     for row in rows:
  52.         print(row)
  53.  
  54. # This returns a single row where the condition is met
  55.     cur.execute("SELECT * FROM students age = 18")
  56.     result = cur.fetchone()
  57.     print(result)
  58.  
  59. # Selecting a row with conditions
  60.     cur.execute("SELECT course,email FROM students WHERE age <= 17")
  61.     rows = cur.fetchall()
  62.  
  63.     for row in rows:
  64.         print(row)
  65.  
  66.  
  67. def update_student_table():
  68.     cur = con.cursor()
  69.  
  70. # Updating a table's info
  71.     cur.execute("UPDATE students SET age = 23 WHERE name = 'Tina'")
  72.  
  73.     con.commit()
  74.  
  75.  
  76. def delete_student_table():
  77.     cur = con.cursor()
  78.  
  79. # Deleting a table row
  80.     cur.execute("DELETE FROM students WHERE name = 'Tina'")
  81.  
  82.     con.commit()
  83.  
  84. # Deleting a table
  85.     cur.execute("DROP TABLE IF NOT EXISTS students")
  86.  
  87.     con.commit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement