Jdatabase Python package. Designed for ease of database control and interaction within Python. The Jdatabase package supports MySQL and PostgreSQL database systems.
Project description
JDatabase (JDB)
jdatabase package by Joshua Widrick
Documentation: Docs.JWid.co
PyPi (pip): PyPi.org/project/jdatabase
GitHub (source code): GitHub.com/JoshWIdrick/jdb
Version: 2.0.0a2
License: MIT
Content:
- Overview
- Installation
- Instantiation
- Database Methods
- create_database() TODO
- TODO
- Database User Methods
- create_user()
- TODO
- Table Methods
- Data Methods
- Class Methods
- Processors
- copy_database() TODO
- copy_table() TODO
- rollback() TODO
- TODO
- Footnote
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.Jdatabase(host="db_hostname", user="db_username", passwd="db_password", db="db_name" )
Database Methods:
TODO
Database User Methods:
TODO
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, or None
if the row is not found.
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, or None
if the data is not found.
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 True
when successfully connected, False
or the Error otherwise.
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 %s
s 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"
Processors:
Footnote:
This package was inspired by my need for an easier way to interact with databases in Python, and the simplemysql package.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file Jdatabase-2.0.0a3.tar.gz
.
File metadata
- Download URL: Jdatabase-2.0.0a3.tar.gz
- Upload date:
- Size: 16.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e8830bac55a04034484ad963060d18ead0d58a8bc2640e3d11dec3b76e03b818 |
|
MD5 | e5b0af11e3f2f39c7e36639bd223674f |
|
BLAKE2b-256 | 1299b45783bba013ad903689e4284e71ed310df876d7efec3e23b91208743922 |