GithubHelp home page GithubHelp logo

joshwidrick / jdb Goto Github PK

View Code? Open in Web Editor NEW
1.0 0.0 0.0 14 KB

jdatabase Python package. Designed for ease of database control and interaction within Python. The jdatabase package supports MySQL based database systems.

Home Page: https://pypi.org/project/jdatabase/

License: MIT License

Python 100.00%
database database-management database-interaction mysql postgresql python python3 database-control

jdb's Introduction

JDatabase (JDB)

jdatabase package by Joshua Widrick

Documentation: docs.jwid.co
GitHub (source code): GitHub.com/JoshWIdrick/jdb
Version: 1.1.1-alpha3
License: MIT

Function / Overview:

The function of the jdatabase package is to allow easy and fluid connection, control, and use of MySQL, and PostgreSQL database systems through one easy to use, concurrent format. The package also allows for logging of data transactions, allowing for database roll-back.
The development of this package has been solely for use in many of my other projects. This package has a lot of default functionality that a normal user will not need. Any feature that you do not need, you can ignore (however understanding it is recommended).

Installation:

The jdatabase package is available publicly through PyPi / pip, so all you need to do is sudo pip install jdatabase. The package can be updated with sudo pip install jdatabase --upgrade.
From source, run sudo python setup.py install.

Instantiation:

The instantiation of the Jdatabase object requires a host, user, password(passwd), and database name(db). The optional arguments are charset, which defaults to "utf8"; port, which defaults to 3306; ssl, which defaults to True; and autocommit, which defaults to True.

from jdatabase import Jdatabase
jdb = Jdatabase(host="db_hostname", user="db_username", passwd="db_password", db="db_name" )

Table Methods:

get_table_names()

Method to get the names of all the tables in the connected database.
Returns a list of str table names.

jdb.get_table_names()

Use of this method also updates self.table_names and self.stable_names for self use.

output:
['SYSTEM_TABLE', 'table_name', 'table_name2', ...]

get_cleaned_table_names()

Method to get the names of all non-system tables in the connected database.
Returns a list of str table names.

jdb.get_cleaned_table_names()

This method DOES NOT update self.table_names and self.stable_names.

output:
['table_name', 'table_name2', ...]

check_for_table(name)

Method to check for a table, named name, in the database.
Returns True if table found, False if not.

jdb.check_for_table("table_name")

create_table(name, {column_name:[parms]})

create_table_if_false_check() is recommended for all table creation.

Creates a table in the database.
Returns the rowcount for the query, should be 0.

jdb.create_table("table_name", {"jd":["VARCHAR(128)", "PRIMARY KEY"], "column_name":["DATATYPE", "DEFAULT VALUE"]})
# with auto primary key insertion
jdb.create_table("table_name", {"column_name":["DATATYPE", "DEFAULT VALUE"], "column2_name":["DATATYPE", "DEFAULT VALUE"]})

The jdatabase package automatically adds a jd column as the primary key column (if a primary key column is not included).

The recommend DEFAULT VALUE is NOT NULL.

create_table_if_not_exists(name, {column_name:[parms]})

create_table_if_false_check() is recommended for all table creation.

Creates a table in the database, if the table name is not present in the database, with database level existence check.
Returns the rowcount for the query, should be 0.

jdb.create_table_if_not_exists("table_name", {"jd":["VARCHAR(128)", "PRIMARY KEY"], "column_name":["DATATYPE", "DEFAULT VALUE"]})
# with auto primary key insertion
jdb.create_table_if_not_exists("table_name", {"column_name":["DATATYPE", "DEFAULT VALUE"], "column2_name":["DATATYPE", "DEFAULT VALUE"]})

The jdatabase package automatically adds a jd column as the primary key column (if a primary key column is not included).

The recommend DEFAULT VALUE is NOT NULL.

create_table_if_false_check(name, {column_name:[parms]})

Creates a table in the database, if the table name is not present in the database, with a query call existence check.
Returns the rowcount for the query, should be 0.

jdb.create_table_if_false_check("table_name", {"jd":["VARCHAR(128)", "PRIMARY KEY"], "column_name":["DATATYPE", "DEFAULT VALUE"]})
# with auto primary key insertion
jdb.create_table_if_false_check("table_name", {"column_name":["DATATYPE", "DEFAULT VALUE"], "column2_name":["DATATYPE", "DEFAULT VALUE"]})

The jdatabase package automatically adds a jd column as the primary key column (if a primary key column is not included).

The recommend DEFAULT VALUE is NOT NULL.

Data Methods:

get_one(name, [fields], (where, [parms]), (order, parms))

Gets one row of data from the table in the connected database, named name.
Returns the row of data.

row = jdb.get_one("table_name", ["field1", "field2"])
# hard-coded condition
row = jdb.get_one("table_name", where=("jd=a1"))
# condition
row = jdb.get_one("table_name", where=("jd=%s", ["jd_val"]))
# extended condition
row = jdb.get_one("table_name", where=("jd=%s and column1=%s", ["jd_val", "column1_val"]))
# ordered by DESC
row = jdb.get_one("table_name", order=("field", "DESC"))

Only the name value is required for get_one().

output:
("jd", "col1val", "col2val", ...)

get_all(name, [fields], (where, [parms]), (order, parms))

Gets all of the data from the table in the connected database, named name.
Returns the data.

row = jdb.get_all("table_name", ["field1", "field2"])
# hard-coded condition
row = jdb.get_all("table_name", where=("jd=a1"))
# condition
row = jdb.get_all("table_name", where=("jd=%s", ["jd_val"]))
# extended condition
row = jdb.get_all("table_name", where=("jd=%s and column1=%s", ["jd_val", "column1_val"]))
# ordered by DESC
row = jdb.get_all("table_name", order=("field", "DESC"))

Only the name value is required for get_all().

output:
(("jd", "col1val", "col2val", ...),
 ("jd", "col1val", "col2val", ...), 
 ...)

insert(name, {data})

Inserts a row of data into the table, named name, in the connected database.
Returns the rowcount for query.

jdb.insert("table_name", {"column1name": val, "column2name": xval})

vals should be the same type as the column in the table.

insert_batch(name, [{data1}, {data2}])

Inserts a batch of data into the table, named name, in the connected database.
Returns rowcount for query.

jdb.insert("table_name", [{"column1name": val, "column2name": xval}, {"column1name": val2, "column2name": xval2}])

vals should be the same type as the column in the table.

update(name, {data}, (where))

Updates data in the table, named name, in the connected database.
Returns rowcount for query.

jdb.update("table_name", {"column1name": val, "column2name": xval}, where=("column1name=%s", ["row_val"]))

vals should be the same type as the column in the table.

insert_or_update(name, {data}, key)

Insert data into or updates the data in the table, named name, in the connected database using a column, key, as a key for the comparision check between the parameter data and the data in the table.
Returns rowcount for query.

jdb.insert_or_update("table_name", {"column1name": val, "column2name": xval}, "column1name")

vals should be the same type as the column in the table.

delete(name, (where))

Delete row(s) in the table, named name, in the connected database, based on where condition.
Returns rowcount for query.

# delete entire table
jdb.delete("table_name")
# delete with where condition
jdb.delete("table_name", where=("jd=%s", ["val"]))

last_id()

Gets the last insert id.
Returns the last insert id.

jdb.last_id()

last_query()

Gets the last executed query.
Returns the last executed query.

jdb.last_query()

Class Methods:

connect()

Method to establish a connection to the database. Automatically run on instantiation.
Returns the database type of either MySQL or PostgreSQL in the form of a str.

jdb.connect()

is_open()

Method to check if the connection object's connection to the database is open.
Returns True if the connection is open, False if not.

jdb.is_open()

is_connected()

Method to check if the database is open and if not if there is a connection error.
Returns True if the connection is open, or if it was reestablished, or the connection error.

jdb.is_connected()

query(sql, [parms])

Method to execute a raw SQL query, with parms replacing %ss in the sql.
Returns the cursor object.

jdb.query("SELECT * FROM table_name WHERE %s=%s;", ['col1','select_me'])

parms are NOT required.

parms are required for any variable use, f"" strings DO NOT work.

commit()

Method to commit all current pending changes to the database. This method is only needed when autocommit is set to False in instantiation.

jdb.commit()

close()

Method to close the connection to the database.

jdb.close()

reconnect()

Method to close the connection to the database, if it is open, and then reopen the connection.

jdb.reconnect()

__str__

Returns the name of the database that the jdatabase object is connected to.

str(jdb)
output:
"database_name"

Footnote:

This package was inspired by my need for an easier way to interact with databases in Python, and the simplemysql package.

jdb's People

Contributors

joshwidrick avatar

Stargazers

 avatar

jdb's Issues

PIP install error on Elementary OS / Ubuntu

After running $ python3 -m pip install jdatabase in Terminal, the following error is shown and the package is not installed.

Collecting jdatabase
  Downloading https://files.pythonhosted.org/packages/6d/b7/50a53da29fd253859d77209246f1361537093d8d385e455798290ff0f634/jdatabase-1.2.0a4.tar.gz
Collecting mysqlclient (from jdatabase)
  Downloading https://files.pythonhosted.org/packages/f4/f1/3bb6f64ca7a429729413e6556b7ba5976df06019a5245a43d36032f1061e/mysqlclient-1.4.2.post1.tar.gz (85kB)
    100% |████████████████████████████████| 92kB 1.1MB/s 
    Complete output from command python setup.py egg_info:
    /bin/sh: 1: mysql_config: not found
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-build-7mztr2gf/mysqlclient/setup.py", line 16, in <module>
        metadata, options = get_config()
      File "/tmp/pip-build-7mztr2gf/mysqlclient/setup_posix.py", line 51, in get_config
        libs = mysql_config("libs")
      File "/tmp/pip-build-7mztr2gf/mysqlclient/setup_posix.py", line 29, in mysql_config
        raise EnvironmentError("%s not found" % (_mysql_config_path,))
    OSError: mysql_config not found
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-7mztr2gf/mysqlclient/

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.