Advertisement
wiley_pastebin

1.3 SQL1

Nov 18th, 2019
1,069
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 0.85 KB | None | 0 0
  1. CONNECT()
  2. import sqlite3
  3. # CREATE a DATABASE IN RAM
  4. db = sqlite3.CONNECT(':memory:')
  5. # CREATE/OPEN a file called mydb.
  6. db = sqlite3.CONNECT('data/mydb')
  7.  
  8. cursor AND commit()
  9. # GET a cursor object
  10. cursor = db.cursor()
  11. #execute the query
  12. cursor.EXECUTE('''CREATE TABLE users(id INTEGER PRIMARY KEY, name TEXT, email TEXT)''')
  13. #commit it TO the DATABASE
  14. db.commit()
  15.  
  16. ROLLBACK()
  17. #rollback the execution
  18. db.ROLLBACK()
  19.  
  20. fetchone()
  21. # obtains SOME ROWS FROM a TABLE
  22. cursor.EXECUTE('''SELECT name,email FROM users''')
  23. #retrieve the FIRST ROW
  24. user1 = cursor.fetchone()
  25. #Print the FIRST COLUMN retrieved(USER's name)
  26. print(user1[0])
  27.  
  28. fetchall()
  29. # obtains some rows from a table
  30. cursor.execute('''SELECT name, email FROM users''')
  31. #retrieve all the rows
  32. all_rows = cursor.fetchall()
  33. for row in all_rows:
  34. print('{0} : {1}'.format(row[0], row[1]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement