Skip to content

Commit 8439c6d

Browse files
author
Alan
committed
MySQL & SQLite
1 parent 73e1687 commit 8439c6d

14 files changed

Lines changed: 114 additions & 0 deletions

Chapter18/connect_database.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import sqlite3
2+
3+
con_obj = sqlite3.connect('test.db')
4+
print('Database connected successfully!!')

Chapter18/create_insert_data.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pymysql as db
2+
3+
con_obj = db.connect('localhost', 'test_user', 'test123', 'test')
4+
with con_obj:
5+
cur_obj = con_obj.cursor()
6+
cur_obj.execute('DROP TABLE IF EXISTS books')
7+
cur_obj.execute('CREATE TABLE books(Id INT PRIMARY KEY AUTO_INCREMENT, Name VARCHAR(100))')
8+
cur_obj.execute("INSERT INTO books(Name) VALUES('Harry Potter')")
9+
cur_obj.execute("INSERT INTO books(Name) VALUES('Lord of the rings')")
10+
cur_obj.execute("INSERT INTO books(Name) VALUES('Murder on the Orient Express')")
11+
cur_obj.execute("INSERT INTO books(Name) VALUES('The adventures of Sherlock Holmes')")
12+
cur_obj.execute("INSERT INTO books(Name) VALUES('Death on the Nile')")
13+
14+
print('Table Created!!')
15+
print('Data inserted successfully!')

Chapter18/create_table.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import sqlite3
2+
3+
con_obj = sqlite3.connect('test.db')
4+
with con_obj:
5+
cur_obj = con_obj.cursor()
6+
cur_obj.execute("""CREATE TABLE books(title text, author text)""")
7+
8+
print('Table created')

Chapter18/delete_data.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# import MySQLdb as db
2+
import pymysql as db
3+
4+
con_obj = db.connect('localhost', 'test_user', 'test123', 'test')
5+
cur_obj = con_obj.cursor()
6+
cur_obj.execute('DELETE FROM books WHERE Id = 5')
7+
try:
8+
con_obj.commit()
9+
except:
10+
con_obj.rollback()

Chapter18/delete_sqlite_data.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import sqlite3
2+
3+
con_obj = sqlite3.connect('test.db')
4+
with con_obj:
5+
cur_obj = con_obj.cursor()
6+
sql = """
7+
DELETE FROM books
8+
WHERE author = 'John Smith'
9+
"""
10+
cur_obj.execute(sql)
11+
12+
print('Data deleted successfully!!')

Chapter18/get_database_version.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import MySQLdb as mdb
2+
import sys
3+
4+
con_obj = mdb.connect('localhost', 'test_user', 'test123', 'test')
5+
cur_obj = con_obj.cursor()
6+
cur_obj.execute('SELECT VERSION()')
7+
version = cur_obj.fetchone()
8+
print('Database version: %s ' % version)
9+
10+
con_obj.close()

Chapter18/insert_data.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import sqlite3
2+
3+
con_obj = sqlite3.connect('test.db')
4+
with con_obj:
5+
cur_obj = con_obj.cursor()
6+
cur_obj.execute("INSERT INTO books VALUES ('Pride and Prejudice', 'Jane Austen')")
7+
cur_obj.execute("INSERT INTO books VALUES ('Harry Potter', 'J.K Rowling')")
8+
cur_obj.execute("INSERT INTO books VALUES ('The Lord of the Rings', 'J. R. R. Tolkien')")
9+
cur_obj.execute("INSERT INTO books VALUES ('Murder on the Orient Express', 'Agatha Christie')")
10+
cur_obj.execute("INSERT INTO books VALUES ('A Study in Scarlet', 'Arthur Conan Doyle')")
11+
con_obj.commit()
12+
13+
print('Data inserted successfully!!')

0 commit comments

Comments
 (0)