Giter Club home page Giter Club logo

mysql-python-class's Introduction

MySQL Python Class

Description

Python MySQL class helps the python developers to connect with MySQL and use queries like: SELECT (one and more conditions), INSERT, UPDATE, DELETE in an easy and fast way.

Usage

To start using the Python MySQL class you need to import the class and initialize with 4 required parameters: host, user, password, database

from mysql-python import MysqlPython

connect_mysql = MysqlPython('host.ip.address', 'user', 'password', 'database')

Select one condition

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.


Select more than one condition

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.


Insert data

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.


Update data

To update data just needs the table, conditional query and specify the columns you want update

conditional_query = 'car_make = %s'

result = connect_mysql.update('car_table', conditional_query, 'nissan', car_model='escort', car_year='2005')

Result: This function return the amount of rows updated.


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')

Result: This function return the amount of rows deleted.

Requirements

MySQL-python >= 1.2.5

mysql-python-class's People

Contributors

nestordeharo avatar elinformatico avatar

Watchers

James Cloos avatar WallaceLiu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.