├── README.md ├── 02-create_db.py ├── 01-connect_to_db.py ├── 03-show_db.py ├── 04-create_table.py ├── 09-select_with_filter.py ├── 10-select_with_wildcard.py ├── 08-select_fetchone.py ├── 18_drop_table.py ├── 16_update_with_user_inputs.py ├── 14_ulter)table.py ├── 11_select_with_user_input.py ├── 12_select_with_order.py ├── 13_SELECT_WITH_LIMIT.PY ├── 15_update_table.py ├── 05-insert_data.py ├── 07-selcet_data.py ├── 17_delete_data.py └── 06-insert_many.py /README.md: -------------------------------------------------------------------------------- 1 | # Python-MySQL-Guide 2 | python scripts for handling basic mysql operations 3 | 4 | for my arabic python with mysql course on youtube 5 | 6 | -------------------------------------------------------------------------------- /02-create_db.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | 4 | myconn = mysql.connector.connect( 5 | host="localhost" , 6 | user = "root" , 7 | passwd = "toor" , 8 | # database = 'cars' 9 | ) 10 | 11 | mycursor = myconn.cursor() 12 | 13 | 14 | mycursor.execute(" CREATE DATABASE mydatabase ") -------------------------------------------------------------------------------- /01-connect_to_db.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | 4 | myconn = mysql.connector.connect( 5 | host="localhost" , 6 | user = "root" , 7 | passwd = "toor" , 8 | # database = 'cars' 9 | ) 10 | 11 | mycursor = myconn.cursor() 12 | 13 | 14 | 15 | print('COnnected ') 16 | mycursor.execute("SHOW DATABASES") -------------------------------------------------------------------------------- /03-show_db.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | 4 | myconn = mysql.connector.connect( 5 | host="localhost" , 6 | user = "root" , 7 | passwd = "toor" , 8 | # database = 'cars2' 9 | ) 10 | 11 | mycursor = myconn.cursor() 12 | 13 | 14 | mycursor.execute(" SHOW DATABASES") 15 | 16 | for db in mycursor: 17 | print(db) -------------------------------------------------------------------------------- /04-create_table.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | 4 | myconn = mysql.connector.connect( 5 | host="localhost" , 6 | user = "root" , 7 | passwd = "toor" , 8 | database='mydatabase' 9 | ) 10 | 11 | mycursor = myconn.cursor() 12 | 13 | 14 | mycursor.execute(" CREATE TABLE movies (name VARCHAR(100) , plot VARCHAR(500) )") 15 | 16 | print('Table Created Successfully') 17 | 18 | -------------------------------------------------------------------------------- /09-select_with_filter.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | 4 | myconn = mysql.connector.connect( 5 | host="localhost" , 6 | user = "root" , 7 | passwd = "toor" , 8 | database='mydatabase' 9 | ) 10 | 11 | mycursor = myconn.cursor() 12 | 13 | mycursor.execute(" SELECT * FROM movies WHERE name='vikins' ") 14 | 15 | result = mycursor.fetchone() 16 | 17 | print(result) 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /10-select_with_wildcard.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | 4 | myconn = mysql.connector.connect( 5 | host="localhost" , 6 | user = "root" , 7 | passwd = "toor" , 8 | database='mydatabase' 9 | ) 10 | 11 | mycursor = myconn.cursor() 12 | 13 | mycursor.execute(" SELECT * FROM movies WHERE name like '%vikins%' ") 14 | 15 | result = mycursor.fetchall() 16 | 17 | print(result) 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /08-select_fetchone.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | 4 | myconn = mysql.connector.connect( 5 | host="localhost" , 6 | user = "root" , 7 | passwd = "toor" , 8 | database='mydatabase' 9 | ) 10 | 11 | mycursor = myconn.cursor() 12 | 13 | # mycursor.execute(" SELECT * FROM movies") 14 | mycursor.execute(" SELECT name , plot FROM movies") 15 | 16 | result = mycursor.fetchone() 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /18_drop_table.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | 4 | myconn = mysql.connector.connect( 5 | host="localhost" , 6 | user = "root" , 7 | passwd = "toor" , 8 | database='mydatabase' 9 | ) 10 | 11 | mycursor = myconn.cursor() 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | mycursor.execute("DROP TABLE movies") 21 | # mycursor.execute("DROP TABLE IF EXISTS movies") 22 | 23 | 24 | myconn.commit() 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /16_update_with_user_inputs.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | 4 | myconn = mysql.connector.connect( 5 | host="localhost" , 6 | user = "root" , 7 | passwd = "toor" , 8 | database='mydatabase' 9 | ) 10 | 11 | mycursor = myconn.cursor() 12 | 13 | 14 | 15 | sql = "UPDATE movies SET plot =%s WHERE name=%s" 16 | data = ('using user inputs' , 'vikins') 17 | 18 | mycursor.execute(sql,data) 19 | 20 | myconn.commit() 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /14_ulter)table.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | 4 | myconn = mysql.connector.connect( 5 | host="localhost" , 6 | user = "root" , 7 | passwd = "toor" , 8 | database='mydatabase' 9 | ) 10 | 11 | mycursor = myconn.cursor() 12 | 13 | 14 | 15 | # mycursor.execute(" ALTER TABLE movies ADD COLUMN language VARCHAR(30)") 16 | mycursor.execute(" ALTER TABLE movies CHANGE language language VARCHAR(50)") 17 | myconn.commit() 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /11_select_with_user_input.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | 4 | myconn = mysql.connector.connect( 5 | host="localhost" , 6 | user = "root" , 7 | passwd = "toor" , 8 | database='mydatabase' 9 | ) 10 | 11 | mycursor = myconn.cursor() 12 | 13 | sql = " SELECT * FROM movies WHERE name=%s " 14 | data = ('vikins',) 15 | 16 | mycursor.execute(sql , data) 17 | 18 | 19 | 20 | result = mycursor.fetchall() 21 | 22 | print(result) 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /12_select_with_order.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | 4 | myconn = mysql.connector.connect( 5 | host="localhost" , 6 | user = "root" , 7 | passwd = "toor" , 8 | database='mydatabase' 9 | ) 10 | 11 | mycursor = myconn.cursor() 12 | 13 | # mycursor.execute(" SELECT * FROM movies ORDER BY name") 14 | mycursor.execute(" SELECT * FROM movies ORDER BY name DESC") 15 | 16 | 17 | result = mycursor.fetchall() 18 | 19 | print(result) 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /13_SELECT_WITH_LIMIT.PY: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | 4 | myconn = mysql.connector.connect( 5 | host="localhost" , 6 | user = "root" , 7 | passwd = "toor" , 8 | database='mydatabase' 9 | ) 10 | 11 | mycursor = myconn.cursor() 12 | 13 | 14 | # mycursor.execute(" SELECT * FROM movies LIMIT 5") 15 | mycursor.execute(" SELECT * FROM movies LIMIT 5 OFFSET 2") 16 | 17 | 18 | result = mycursor.fetchall() 19 | 20 | print(result) 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /15_update_table.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | 4 | myconn = mysql.connector.connect( 5 | host="localhost" , 6 | user = "root" , 7 | passwd = "toor" , 8 | database='mydatabase' 9 | ) 10 | 11 | mycursor = myconn.cursor() 12 | 13 | 14 | 15 | # mycursor.execute("UPDATE movies SET plot = 'using the update statement' WHERE name='vikins'") 16 | mycursor.execute("UPDATE movies SET plot = 'using the update statement' ") 17 | 18 | myconn.commit() 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /05-insert_data.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | 4 | myconn = mysql.connector.connect( 5 | host="localhost" , 6 | user = "root" , 7 | passwd = "toor" , 8 | database='mydatabase' 9 | ) 10 | 11 | mycursor = myconn.cursor() 12 | 13 | 14 | 15 | sql = "INSERT INTO movies(name , plot) VALUES(%s , %s)" 16 | data = ("Harry Potter2" , 'magic Movie2') 17 | 18 | 19 | mycursor.execute(sql , data) 20 | 21 | myconn.commit() 22 | 23 | print('data inserted ') 24 | print(mycursor.lastrowid) 25 | 26 | -------------------------------------------------------------------------------- /07-selcet_data.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | 4 | myconn = mysql.connector.connect( 5 | host="localhost" , 6 | user = "root" , 7 | passwd = "toor" , 8 | database='mydatabase' 9 | ) 10 | 11 | mycursor = myconn.cursor() 12 | 13 | # mycursor.execute(" SELECT * FROM movies") 14 | mycursor.execute(" SELECT name , plot FROM movies") 15 | 16 | result = mycursor.fetchall() 17 | 18 | 19 | print(result[0]) 20 | 21 | for movie in result : 22 | print(movie) 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /17_delete_data.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | 4 | myconn = mysql.connector.connect( 5 | host="localhost" , 6 | user = "root" , 7 | passwd = "toor" , 8 | database='mydatabase' 9 | ) 10 | 11 | mycursor = myconn.cursor() 12 | 13 | 14 | 15 | 16 | # mycursor.execute("DELETE FROM movies WHERE name='vikins' ") 17 | 18 | 19 | sql = "DELETE FROM movies WHERE name=%s " 20 | data = ('vikins2',) 21 | 22 | mycursor.execute(sql , data) 23 | 24 | 25 | myconn.commit() 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /06-insert_many.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | 4 | myconn = mysql.connector.connect( 5 | host="localhost" , 6 | user = "root" , 7 | passwd = "toor" , 8 | database='mydatabase' 9 | ) 10 | 11 | mycursor = myconn.cursor() 12 | 13 | 14 | 15 | sql = "INSERT INTO movies(name , plot) VALUES(%s , %s)" 16 | data = [ 17 | ("Preson Break2" , 'escape plan') , 18 | ("vikins2" , 'old heros'), 19 | ] 20 | 21 | 22 | mycursor.executemany(sql , data) 23 | 24 | myconn.commit() 25 | 26 | print('data inserted ') 27 | print(mycursor.lastrowid) 28 | 29 | --------------------------------------------------------------------------------