├── README.md ├── Our_data.db ├── Python SQLite3 Tutorial 1 - Introduction and Creating a Database.py ├── Python SQLite3 Tutorial 2 - Creating a table.py ├── Python SQLite3 Tutorial 3 - Inserting records to our database.py ├── .github └── FUNDING.yml ├── Python SQLite3 Tutorial 4 - Using Variables to insert data.py ├── Python SQLite3 Tutorial 5 - Reading Data from a Database.py ├── Python SQLite3 Tutorial 6 - Updating a record.py ├── Python SQLite3 Tutorial8 - Bonus Conclusion.py └── Python SQLite3 Tutorial 7- Deleting a record.py /README.md: -------------------------------------------------------------------------------- 1 | # Python-SQLite-Tutorials 2 | -------------------------------------------------------------------------------- /Our_data.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codewithlennylen/Python-SQLite-Tutorials/HEAD/Our_data.db -------------------------------------------------------------------------------- /Python SQLite3 Tutorial 1 - Introduction and Creating a Database.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | 3 | dbase = sqlite3.connect('Our_data.db') # Open a database File 4 | print 'Database opened' 5 | 6 | 7 | dbase.close() 8 | print ' Database Closed' 9 | -------------------------------------------------------------------------------- /Python SQLite3 Tutorial 2 - Creating a table.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | 3 | dbase = sqlite3.connect('Our_data.db') # Open a database File 4 | print 'Database opened' 5 | 6 | dbase.execute(''' CREATE TABLE IF NOT EXISTS employee_records( 7 | ID INT PRIMARY KEY NOT NULL, 8 | NAME TEXT NOT NULL, 9 | DIVISION TEXT NOT NULL, 10 | STARS INT NOT NULL) ''') 11 | 12 | print 'Table created' 13 | 14 | 15 | dbase.close() 16 | print ' Database Closed' 17 | -------------------------------------------------------------------------------- /Python SQLite3 Tutorial 3 - Inserting records to our database.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | 3 | dbase = sqlite3.connect('Our_data.db') # Open a database File 4 | print 'Database opened' 5 | 6 | dbase.execute(''' CREATE TABLE IF NOT EXISTS employee_records( 7 | ID INT PRIMARY KEY NOT NULL, 8 | NAME TEXT NOT NULL, 9 | DIVISION TEXT NOT NULL, 10 | STARS INT NOT NULL) ''') 11 | 12 | print 'Table created' 13 | 14 | dbase.execute(''' INSERT INTO employee_records(ID,NAME,DIVISION,STARS) 15 | VALUES(5,'James','Maintenance',4) 16 | ''') 17 | 18 | dbase.commit() 19 | print 'REcord inserted' 20 | 21 | dbase.close() 22 | print ' Database Closed' 23 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: codewithlennylen 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: codewithlennylen 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /Python SQLite3 Tutorial 4 - Using Variables to insert data.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | 3 | dbase = sqlite3.connect('Our_data.db') # Open a database File 4 | print 'Database opened' 5 | 6 | dbase.execute(''' CREATE TABLE IF NOT EXISTS employee_records( 7 | ID INT PRIMARY KEY NOT NULL, 8 | NAME TEXT NOT NULL, 9 | DIVISION TEXT NOT NULL, 10 | STARS INT NOT NULL) ''') 11 | 12 | print 'Table created' 13 | 14 | def insert_record(ID,NAME,DIVISION,STARS): 15 | dbase.execute(''' INSERT INTO employee_records(ID,NAME,DIVISION,STARS) 16 | VALUES(?,?,?,?)''',(ID,NAME,DIVISION,STARS)) 17 | 18 | dbase.commit() 19 | print 'REcord inserted' 20 | 21 | insert_record(6,'Bob','Hardware',4) 22 | 23 | dbase.close() 24 | print ' Database Closed' 25 | -------------------------------------------------------------------------------- /Python SQLite3 Tutorial 5 - Reading Data from a Database.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | 3 | dbase = sqlite3.connect('Our_data.db') # Open a database File 4 | print 'Database opened' 5 | 6 | dbase.execute(''' CREATE TABLE IF NOT EXISTS employee_records( 7 | ID INT PRIMARY KEY NOT NULL, 8 | NAME TEXT NOT NULL, 9 | DIVISION TEXT NOT NULL, 10 | STARS INT NOT NULL) ''') 11 | 12 | print 'Table created' 13 | 14 | def insert_record(ID,NAME,DIVISION,STARS): 15 | dbase.execute(''' INSERT INTO employee_records(ID,NAME,DIVISION,STARS) 16 | VALUES(?,?,?,?)''',(ID,NAME,DIVISION,STARS)) 17 | 18 | dbase.commit() 19 | print 'REcord inserted' 20 | 21 | ##insert_record(6,'Bob','Hardware',4) 22 | 23 | def read_Data(): 24 | # from math import * 25 | data = dbase.execute(''' SELECT * FROM employee_records ORDER BY NAME''') 26 | for record in data: 27 | print 'ID : '+str(record[0]) 28 | print 'NAME : '+str(record[1]) 29 | print 'DIVISION : '+str(record[2]) 30 | print 'STARS : '+str(record[3])+'\n' 31 | 32 | 33 | read_Data() 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | dbase.close() 44 | print ' Database Closed' 45 | -------------------------------------------------------------------------------- /Python SQLite3 Tutorial 6 - Updating a record.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | 3 | dbase = sqlite3.connect('Our_data.db') # Open a database File 4 | print 'Database opened' 5 | 6 | dbase.execute(''' CREATE TABLE IF NOT EXISTS employee_records( 7 | ID INT PRIMARY KEY NOT NULL, 8 | NAME TEXT NOT NULL, 9 | DIVISION TEXT NOT NULL, 10 | STARS INT NOT NULL) ''') 11 | 12 | print 'Table created' 13 | 14 | def insert_record(ID,NAME,DIVISION,STARS): 15 | dbase.execute(''' INSERT INTO employee_records(ID,NAME,DIVISION,STARS) 16 | VALUES(?,?,?,?)''',(ID,NAME,DIVISION,STARS)) 17 | 18 | dbase.commit() 19 | print 'REcord inserted' 20 | 21 | ##insert_record(6,'Bob','Hardware',4) 22 | 23 | def read_Data(): 24 | # from math import * 25 | data = dbase.execute(''' SELECT * FROM employee_records ORDER BY NAME''') 26 | for record in data: 27 | print 'ID : '+str(record[0]) 28 | print 'NAME : '+str(record[1]) 29 | print 'DIVISION : '+str(record[2]) 30 | print 'STARS : '+str(record[3])+'\n' 31 | 32 | 33 | read_Data() 34 | 35 | 36 | def update_record(): 37 | dbase.execute(''' UPDATE employee_records set STARS=3 WHERE ID=2 ''') 38 | dbase.commit() 39 | print 'Updated' 40 | 41 | update_record() 42 | print '----------------------' 43 | read_Data() 44 | 45 | 46 | 47 | 48 | 49 | dbase.close() 50 | print ' Database Closed' 51 | -------------------------------------------------------------------------------- /Python SQLite3 Tutorial8 - Bonus Conclusion.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | 3 | dbase = sqlite3.connect('Our_data.db') # Open a database File 4 | cursor = dbase.cursor() 5 | print 'Database opened' 6 | print 'Cursor crwated' 7 | 8 | dbase.execute(''' CREATE TABLE IF NOT EXISTS employee_records( 9 | ID INT PRIMARY KEY NOT NULL, 10 | NAME TEXT NOT NULL, 11 | DIVISION TEXT NOT NULL, 12 | STARS INT NOT NULL) ''') 13 | 14 | print 'Table created' 15 | 16 | def read_Data(): 17 | # from math import * 18 | data = cursor.execute(''' SELECT * FROM employee_records ORDER BY NAME''') 19 | for record in data: 20 | print 'ID : '+str(record[0]) 21 | print 'NAME : '+str(record[1]) 22 | print 'DIVISION : '+str(record[2]) 23 | print 'STARS : '+str(record[3])+'\n' 24 | 25 | 26 | ##read_Data() 27 | 28 | def check_Data(): 29 | # from math import * 30 | data = cursor.execute(''' SELECT NAME FROM employee_records WHERE ID =2''') 31 | x = data.fetchall() 32 | if x == []: 33 | print 'Doesnt exist' 34 | else: 35 | print (x) 36 | 37 | ## for record in data: 38 | ## print 'NAME : '+str(record[0]) 39 | #### print 'NAME : '+str(record[1]) 40 | #### print 'DIVISION : '+str(record[2]) 41 | #### print 'STARS : '+str(record[3])+'\n' 42 | 43 | 44 | check_Data() 45 | 46 | 47 | def update_record(): 48 | dbase.execute(''' UPDATE employee_records set STARS=3 WHERE ID=2 ''') 49 | dbase.commit() 50 | print 'Updated' 51 | 52 | 53 | dbase.close() 54 | print ' Database Closed' 55 | -------------------------------------------------------------------------------- /Python SQLite3 Tutorial 7- Deleting a record.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | 3 | dbase = sqlite3.connect('Our_data.db') # Open a database File 4 | print 'Database opened' 5 | 6 | dbase.execute(''' CREATE TABLE IF NOT EXISTS employee_records( 7 | ID INT PRIMARY KEY NOT NULL, 8 | NAME TEXT NOT NULL, 9 | DIVISION TEXT NOT NULL, 10 | STARS INT NOT NULL) ''') 11 | 12 | print 'Table created' 13 | 14 | def insert_record(ID,NAME,DIVISION,STARS): 15 | dbase.execute(''' INSERT INTO employee_records(ID,NAME,DIVISION,STARS) 16 | VALUES(?,?,?,?)''',(ID,NAME,DIVISION,STARS)) 17 | 18 | dbase.commit() 19 | print 'REcord inserted' 20 | 21 | ##insert_record(6,'Bob','Hardware',4) 22 | 23 | def read_Data(): 24 | # from math import * 25 | data = dbase.execute(''' SELECT * FROM employee_records ORDER BY NAME''') 26 | for record in data: 27 | print 'ID : '+str(record[0]) 28 | print 'NAME : '+str(record[1]) 29 | print 'DIVISION : '+str(record[2]) 30 | print 'STARS : '+str(record[3])+'\n' 31 | 32 | 33 | read_Data() 34 | 35 | 36 | def update_record(): 37 | dbase.execute(''' UPDATE employee_records set STARS=3 WHERE ID=2 ''') 38 | dbase.commit() 39 | print 'Updated' 40 | 41 | ##update_record() 42 | ##print '----------------------' 43 | ##read_Data() 44 | 45 | 46 | def delete_record(): 47 | dbase.execute(''' DELETE from employee_records WHERE ID = 1 ''') 48 | dbase.commit() 49 | print 'Deleted' 50 | 51 | delete_record() 52 | print '----------------------' 53 | read_Data() 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | dbase.close() 69 | print ' Database Closed' 70 | --------------------------------------------------------------------------------