Python MySQL class helps the python developers to connect with MySQL and then made the useful queries like: SELECT (one and more conditions), INSERT, UPDATE, DELETE
To start using the Python MySQL class you need to import and initialize using the 4 default parameters: host, user, password, database
from mysql-python import MysqlPython
connect_mysql = MysqlPython('host.ip.address', 'user', 'password', 'database')If you want to obtain information from one table and use one condition, you could use select function where args argument is for referencing the columns you need to obtain.
conditional_query = 'car_make = %s '
result = connect_mysql.select('car', conditional_query, 'id_car', 'car_text', car_make='nissan')Result: The function return a list or empty list in case of not find nothing.
In case you have need to obtain information with more than one condition, you could use the select_advancedfunction, where the columns you need to obtain are referenced by args and the conditionals are referenced by tuples
sql_query = 'SELECT C.cylinder FROM car C WHERE C.car_make = %s AND C.car_model = %s'
result = connect_mysql.select_advanced(sql_query, ('car_make', 'nissan'),('car_model','altima'))Note: Inside the sql_advanced function the order of the parameters matters, so the tuples should have the order of the query.
Result: The function return a list or empty list in case of not find nothing.
Inserting data is really simple and intuitive, where we are going to reference the column and the values
result = connect_msyql.insert('car', car_make='ford', car_model='escort', car_year='2005')Result: The function return the last row id was inserted.
#### Delete data
Delete data is really simple like insert, just reference the column as condition and table.
conditional_query = 'car_make = %s'
result = connect_mysql.delete('car', conditional_query, 'nissan')MySQL-python >= 1.2.5