Giter VIP home page Giter VIP logo

bootstrapdb's Introduction

This library create Model classes based on database structure (it reads all the tables and columns)

Those Model classes allow an easy manipulation of the database with almost none SQL.

Installation

python -m pip install --index-url https://test.pypi.org/simple/ --no-deps BootstrapDB-armandokeller

Generating the classes

First you need to import the Bootstrap and Base classes and your connector for the database, then you can call Bootstrap() using the cursor as parameter, the same cursor must be set to Base.cursor.

Example:

import mysql.connector
from BootstrapDB import Base, Bootstrap

db = mysql.connector.connect(
            host = "127.0.0.1",
            user = "root",
            database = "sample",
            auth_plugin = 'mysql_native_password',
            passwd = "supersecretpassword"
        )

Base.cursor = db.cursor()
Bootstrap(Base.cursor)  

The files will be at "out" folder

Using the generated files

Getting all the rows of "example" table

import mysql.connector
from BootstrapDB import Base
from ExampleBase import ExampleBase
 
db = mysql.connector.connect(
            host = "127.0.0.1",
            user = "root",
            database = "sample",
            auth_plugin = 'mysql_native_password',
            passwd = "supersecretpassword"
        )
        
all_examples = ExampleBase().query()
print(all_examples)

Getting all the rows of "example" table where the value of column size is equal to 22

Equivalent of the folowing SQL

SELECT * FROM example WHERE size = 22
import mysql.connector
from BootstrapDB import Base
from ExampleBase import ExampleBase
 
db = mysql.connector.connect(
            host = "127.0.0.1",
            user = "root",
            database = "sample",
            auth_plugin = 'mysql_native_password',
            passwd = "supersecretpassword"
        )
        
size22 = ExampleBase().query("size = 22")
print(size22)

Quering for single element by his private key value (like index)

import mysql.connector
from BootstrapDB import Base
from ExampleBase import ExampleBase
 
db = mysql.connector.connect(
            host = "127.0.0.1",
            user = "root",
            database = "sample",
            auth_plugin = 'mysql_native_password',
            passwd = "supersecretpassword"
        )
        
desired_object = ExampleBase(102)
print(desired_object)

Creating a new row in "example" table

import mysql.connector
from BootstrapDB import Base
from ExampleBase import ExampleBase
 
db = mysql.connector.connect(
            host = "127.0.0.1",
            user = "root",
            database = "sample",
            auth_plugin = 'mysql_native_password',
            passwd = "supersecretpassword"
        )
new_item_obj = {"name":"Test object", "size":30}
new_item_row = ExampleBase(obj = new_item_obj)
new_item_row.save()

bootstrapdb's People

Contributors

armandokeller avatar

Watchers

 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.