Coder Social home page Coder Social logo

infmysql's Introduction

infmysql

CI Python versions supported

Wrapper for the mysqlclient library using Python 3.

Development

Setup

Create a virtual environment, install dependencies and load environment variables.

mkvirtualenv infmysql -p $(which python3)
dev/setup_venv.sh
source dev/env_develop

Run a MySQL Docker container.

dev/start_local_dependencies.sh

Running tests, linter & formatter and configure git hooks

Note that project uses Alea's pydevlib, so take a look at its README to see the available commands.

infmysql client API

Below is described the public API that this library provides.

The client can be initialized using the factory with a database URI and an optional parameter which determines if we want to use a dict cursor (false by default).

mysql_client = factory.mysql_client(database_uri, use_dict_cursor=False)

execute()

Executes a SQL query and returns the result. Passing parameters is possible by using %s placeholders in the SQL query, and passing a sequence of values as the second argument of the function.

mysql_client.execute(query, params)

โžก๏ธ Parameters

  • query: str
  • params (optional): tuple<any>. Defaults to None.

โฌ…๏ธ Returns a tuple of tuples or dictionaries, each containing a row of results.

tuple<tuple<any>>

๐Ÿ’ฅ Throws the same exceptions than the mysqlclient library.

Usage example

from infmysql import factory

mysql_client = factory.mysql_client('mysql://username:password@host:port/databasename')

sql_query = 'SELECT (name, surname, age) FROM users WHERE age < %s AND active = %s;'
params = (30, True, )

result = mysql_client.execute(sql_query, params)

# (
#   ('Ann', 'White', 18, ),
#   ('Axel', 'Schwarz', 21, ),
#   ('Camille', 'Rouge', '27', ),
# )

Usage example with dict cursor

from infmysql import factory

mysql_client = factory.mysql_client('mysql://username:password@host:port/databasename', use_dict_cursor=True)

sql_query = 'SELECT (name, surname, age) FROM users WHERE age < %s AND active = %s;'
params = (30, True, )

result = mysql_client.execute(sql_query, params)

# (
#   {'name': 'Ann', 'surname': 'White', 'age': 18},
#   {'name': 'Axel', 'surname': 'Schwarz', 'age': 21},
#   {'name': 'Camille', 'surname': 'Rouge', 'age': 27},
# )

infmysql's People

Contributors

alex-arce avatar fmpalacios avatar luisddm avatar mateoalea avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.