Name | dataBased JSON |
Version |
0.1.1
JSON |
| download |
home_page | |
Summary | Wrapper for the standard library Sqlite3 module to make setting up and using a database quicker and easier. |
upload_time | 2023-01-18 22:57:30 |
maintainer | |
docs_url | None |
author | Matt Manes |
requires_python | >=3.10 |
license | |
keywords |
database
sqlite
sqlite3
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# dataBased
dataBased is a package that wraps the standard library Sqlite3 module to make setting up and using a database quicker and easier.<br>
Install with:
<pre>pip install dataBased</pre>
dataBased is a package that wraps the standard library Sqlite3 module to largely avoid writing queries except for table definitions.<br>
It consists of the class DataBased and an additional function for displaying information in a grid called dataToString.<br>
The DataBased class contains functions for creating databases and tables; inserting, updating, and deleting rows;
as well as retrieving data and schema information.<br>
The dataToString function uses the tabulate package (https://pypi.org/project/tabulate/) to generate a grid as a string from a list of dictionaries.<br>
By default, dataToString will automatically wrap the width of columns to fit within the current terminal window.<br><br>
Member functions that require a database connection will
automatically create one when called if one isn't already open,
but a manual call to self.close() needs to be called in order to
save the database file and release the connection.<br>
If a context manager is used, like in the following example, you don't need to worry about manually opening, saving, or closing the database.<br>
<br>
Usage:
<pre>
from dataBased import DataBased, dataToString
from datetime import datetime
# if the .db file specified doesn't exist, it will be created
# a log file with the same name will be generated and stored in the same directory
with DataBased(dbPath="records.db") as db:
tables = [
"kitchenTables(numLegs int, topMaterial text, shape text, dateAdded timestamp)"
]
# A table will only be created if it doesn't exist. createTables() will not overwrite an existing table.
db.createTables(tables)
kitchenTables = [
(4, "birch", "round", datetime.now()),
(3, "oak", "round", datetime.now()),
(6, "granite", "rectangle", datetime.now()),
]
for kitchenTable in kitchenTables:
db.addToTable("kitchenTables", kitchenTable)
print(f'number of rows: {db.count("kitchenTables")}')
print(f'table names: {db.getTableNames()}')
print(f'column names: {db.getColumnNames("kitchenTables")}')
print(db.getRows("kitchenTables", [("numLegs", 6)]))
print(db.getRows("kitchenTables", [("shape", "round")], sortByColumn="numLegs"))
print(db.getRows("kitchenTables", [("shape", "round"), ("numLegs", 4)]))
db.update(
"kitchenTables",
columnToUpdate="topMaterial",
newValue="glass",
matchCriteria=[("numLegs", 3)],
)
print(db.getRows("kitchenTables", sortByColumn="numLegs"))
print(dataToString(db.getRows("kitchenTables"), sortKey="topMaterial"))
</pre>
produces:
<pre>
number of rows: 3
table names: ['kitchenTables']
column names: ['numLegs', 'topMaterial', 'shape', 'dateAdded']
[{'numLegs': 6, 'topMaterial': 'granite', 'shape': 'rectangle', 'dateAdded': datetime.datetime(2022, 12, 9, 15, 56, 56, 543549)}]
[{'numLegs': 3, 'topMaterial': 'oak', 'shape': 'round', 'dateAdded': datetime.datetime(2022, 12, 9, 15, 56, 56, 543549)}, {'numLegs': 4, 'topMaterial': 'birch', 'shape': 'round', 'dateAdded': datetime.datetime(2022, 12, 9, 15, 56, 56, 543549)}]
[{'numLegs': 4, 'topMaterial': 'birch', 'shape': 'round', 'dateAdded': datetime.datetime(2022, 12, 9, 15, 56, 56, 543549)}]
[{'numLegs': 3, 'topMaterial': 'glass', 'shape': 'round', 'dateAdded': datetime.datetime(2022, 12, 9, 15, 56, 56, 543549)}, {'numLegs': 4, 'topMaterial': 'birch', 'shape':
'round', 'dateAdded': datetime.datetime(2022, 12, 9, 15, 56, 56, 543549)}, {'numLegs': 6, 'topMaterial': 'granite', 'shape': 'rectangle', 'dateAdded': datetime.datetime(2022, 12, 9, 15, 56, 56, 543549)}]
</pre>
and the final print() call on the dataToString() function produces:
<pre>
+-----------+---------------+-----------+----------------------------+
| numLegs | topMaterial | shape | dateAdded |
+===========+===============+===========+============================+
| 4 | birch | round | 2022-12-14 18:19:31.501745 |
+-----------+---------------+-----------+----------------------------+
| 3 | glass | round | 2022-12-14 18:19:31.501745 |
+-----------+---------------+-----------+----------------------------+
| 6 | granite | rectangle | 2022-12-14 18:19:31.501745 |
+-----------+---------------+-----------+----------------------------+
</pre>
When a DataBased object is created, if there is no file named "dbManager.py"
in the same directory as the specified database file, one will be created from
a template.<br>
"dbManager.py" is a command line script that provides basic database commands,
but can be tailored to a given database(s).<br>
Instead of invoking the script over and over with new commands,
the script is invoked once and will repetedly prompt the user for commands.<br>
Invoking the script and then passing the "-h/--help" command:
<pre>
>dbManager.py
Enter command: -h
usage: dbManager.py [-h] [-db DBNAME] [-i] [-t [TABLES ...]] [-c [COLUMNS ...]] [-f FIND] [-sco] [-d [DELETE ...]] [-u UPDATE UPDATE] [-sb SORTBY]
options:
-h, --help show this help message and exit
-db DBNAME, --dbName DBNAME
Name of database file to use. Required on the first loop if no default is set, but subsequent loops will resuse the same database unless a new one is provided through this arg.
-i, --info Display table names, their respective columns, and how many records they contain. If a -t/--tables arg is passed, just the columns and row count for those tables will be shown.
-t [TABLES ...], --tables [TABLES ...]
Limits commands to a specific list of tables. Optional for some commands, required for others. If this is the only arg given (besides -db if not already set), the whole table will be printed to the terminal.
-c [COLUMNS ...], --columns [COLUMNS ...]
Limits commands to a specific list of columns. Optional for some commands, required for others. If this and -t are the only args given (besides -db if not already set), the whole table will be printed to the terminal,
but with only the columns provided with this arg.
-f FIND, --find FIND A substring to search the database for. If a -c/--columns arg(s) is not given, the values will be matched against all columns. Similarly, if a -t/--tables arg(s) is not given, the values will be searched for in all
tables.
-sco, --showCountOnly
Show the number of results returned by -f/--find, but don't print the results to the terminal.
-d [DELETE ...], --delete [DELETE ...]
A list of values to be deleted from the database. A -c/--columns arg must be supplied. A -t/--tables arg must be supplied.
-u UPDATE UPDATE, --update UPDATE UPDATE
Update a record in the database. Expects two arguments: the current value and the new value. A -c/--columns arg must be supplied. A -t/--tables arg must be supplied.
-sb SORTBY, --sortBy SORTBY
Column to sort results by.
Enter command:
</pre>
Raw data
{
"_id": null,
"home_page": "",
"name": "dataBased",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": "",
"keywords": "database,sqlite,sqlite3",
"author": "Matt Manes",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/61/5a/960e61e9f405b1c90f4960ab3e0fbdf670d52596fb28f6daebca07aed80c/databased-0.1.1.tar.gz",
"platform": null,
"description": "# dataBased\ndataBased is a package that wraps the standard library Sqlite3 module to make setting up and using a database quicker and easier.<br>\nInstall with:\n<pre>pip install dataBased</pre>\ndataBased is a package that wraps the standard library Sqlite3 module to largely avoid writing queries except for table definitions.<br>\nIt consists of the class DataBased and an additional function for displaying information in a grid called dataToString.<br>\nThe DataBased class contains functions for creating databases and tables; inserting, updating, and deleting rows; \nas well as retrieving data and schema information.<br>\nThe dataToString function uses the tabulate package (https://pypi.org/project/tabulate/) to generate a grid as a string from a list of dictionaries.<br>\nBy default, dataToString will automatically wrap the width of columns to fit within the current terminal window.<br><br>\nMember functions that require a database connection will\nautomatically create one when called if one isn't already open,\nbut a manual call to self.close() needs to be called in order to\nsave the database file and release the connection.<br>\nIf a context manager is used, like in the following example, you don't need to worry about manually opening, saving, or closing the database.<br>\n<br>\nUsage:\n<pre>\nfrom dataBased import DataBased, dataToString\nfrom datetime import datetime\n\n# if the .db file specified doesn't exist, it will be created\n# a log file with the same name will be generated and stored in the same directory\nwith DataBased(dbPath=\"records.db\") as db:\n tables = [\n \"kitchenTables(numLegs int, topMaterial text, shape text, dateAdded timestamp)\"\n ]\n # A table will only be created if it doesn't exist. createTables() will not overwrite an existing table.\n db.createTables(tables)\n kitchenTables = [\n (4, \"birch\", \"round\", datetime.now()),\n (3, \"oak\", \"round\", datetime.now()),\n (6, \"granite\", \"rectangle\", datetime.now()),\n ]\n for kitchenTable in kitchenTables:\n db.addToTable(\"kitchenTables\", kitchenTable)\n\n print(f'number of rows: {db.count(\"kitchenTables\")}')\n print(f'table names: {db.getTableNames()}')\n print(f'column names: {db.getColumnNames(\"kitchenTables\")}')\n print(db.getRows(\"kitchenTables\", [(\"numLegs\", 6)]))\n print(db.getRows(\"kitchenTables\", [(\"shape\", \"round\")], sortByColumn=\"numLegs\"))\n print(db.getRows(\"kitchenTables\", [(\"shape\", \"round\"), (\"numLegs\", 4)]))\n\n db.update(\n \"kitchenTables\",\n columnToUpdate=\"topMaterial\",\n newValue=\"glass\",\n matchCriteria=[(\"numLegs\", 3)],\n )\n print(db.getRows(\"kitchenTables\", sortByColumn=\"numLegs\"))\n print(dataToString(db.getRows(\"kitchenTables\"), sortKey=\"topMaterial\"))\n</pre>\nproduces:\n<pre>\nnumber of rows: 3\ntable names: ['kitchenTables']\ncolumn names: ['numLegs', 'topMaterial', 'shape', 'dateAdded']\n[{'numLegs': 6, 'topMaterial': 'granite', 'shape': 'rectangle', 'dateAdded': datetime.datetime(2022, 12, 9, 15, 56, 56, 543549)}]\n[{'numLegs': 3, 'topMaterial': 'oak', 'shape': 'round', 'dateAdded': datetime.datetime(2022, 12, 9, 15, 56, 56, 543549)}, {'numLegs': 4, 'topMaterial': 'birch', 'shape': 'round', 'dateAdded': datetime.datetime(2022, 12, 9, 15, 56, 56, 543549)}]\n[{'numLegs': 4, 'topMaterial': 'birch', 'shape': 'round', 'dateAdded': datetime.datetime(2022, 12, 9, 15, 56, 56, 543549)}]\n[{'numLegs': 3, 'topMaterial': 'glass', 'shape': 'round', 'dateAdded': datetime.datetime(2022, 12, 9, 15, 56, 56, 543549)}, {'numLegs': 4, 'topMaterial': 'birch', 'shape': \n'round', 'dateAdded': datetime.datetime(2022, 12, 9, 15, 56, 56, 543549)}, {'numLegs': 6, 'topMaterial': 'granite', 'shape': 'rectangle', 'dateAdded': datetime.datetime(2022, 12, 9, 15, 56, 56, 543549)}]\n</pre>\nand the final print() call on the dataToString() function produces:\n<pre>\n+-----------+---------------+-----------+----------------------------+\n| numLegs | topMaterial | shape | dateAdded |\n+===========+===============+===========+============================+\n| 4 | birch | round | 2022-12-14 18:19:31.501745 |\n+-----------+---------------+-----------+----------------------------+\n| 3 | glass | round | 2022-12-14 18:19:31.501745 |\n+-----------+---------------+-----------+----------------------------+\n| 6 | granite | rectangle | 2022-12-14 18:19:31.501745 |\n+-----------+---------------+-----------+----------------------------+\n</pre>\nWhen a DataBased object is created, if there is no file named \"dbManager.py\"\nin the same directory as the specified database file, one will be created from\na template.<br>\n\"dbManager.py\" is a command line script that provides basic database commands,\nbut can be tailored to a given database(s).<br>\nInstead of invoking the script over and over with new commands,\nthe script is invoked once and will repetedly prompt the user for commands.<br>\nInvoking the script and then passing the \"-h/--help\" command:\n<pre>\n>dbManager.py\nEnter command: -h\nusage: dbManager.py [-h] [-db DBNAME] [-i] [-t [TABLES ...]] [-c [COLUMNS ...]] [-f FIND] [-sco] [-d [DELETE ...]] [-u UPDATE UPDATE] [-sb SORTBY]\n\noptions:\n -h, --help show this help message and exit\n -db DBNAME, --dbName DBNAME\n Name of database file to use. Required on the first loop if no default is set, but subsequent loops will resuse the same database unless a new one is provided through this arg.\n -i, --info Display table names, their respective columns, and how many records they contain. If a -t/--tables arg is passed, just the columns and row count for those tables will be shown.\n -t [TABLES ...], --tables [TABLES ...]\n Limits commands to a specific list of tables. Optional for some commands, required for others. If this is the only arg given (besides -db if not already set), the whole table will be printed to the terminal.\n -c [COLUMNS ...], --columns [COLUMNS ...]\n Limits commands to a specific list of columns. Optional for some commands, required for others. If this and -t are the only args given (besides -db if not already set), the whole table will be printed to the terminal,\n but with only the columns provided with this arg.\n -f FIND, --find FIND A substring to search the database for. If a -c/--columns arg(s) is not given, the values will be matched against all columns. Similarly, if a -t/--tables arg(s) is not given, the values will be searched for in all\n tables.\n -sco, --showCountOnly\n Show the number of results returned by -f/--find, but don't print the results to the terminal.\n -d [DELETE ...], --delete [DELETE ...]\n A list of values to be deleted from the database. A -c/--columns arg must be supplied. A -t/--tables arg must be supplied.\n -u UPDATE UPDATE, --update UPDATE UPDATE\n Update a record in the database. Expects two arguments: the current value and the new value. A -c/--columns arg must be supplied. A -t/--tables arg must be supplied.\n -sb SORTBY, --sortBy SORTBY\n Column to sort results by.\nEnter command:\n</pre>",
"bugtrack_url": null,
"license": "",
"summary": "Wrapper for the standard library Sqlite3 module to make setting up and using a database quicker and easier.",
"version": "0.1.1",
"split_keywords": [
"database",
"sqlite",
"sqlite3"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "7386bbb18f0ecd079e051ba6546280da10bd1fd5d09020e87949bde1c1a02391",
"md5": "6fe2c59ef28875cbe9a92e19dc538c58",
"sha256": "6b381f30f7d8495e8d091de790cbbe5754a91ff8d06cee36b8185ed56d0b3b1c"
},
"downloads": -1,
"filename": "databased-0.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "6fe2c59ef28875cbe9a92e19dc538c58",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 11828,
"upload_time": "2023-01-18T22:57:29",
"upload_time_iso_8601": "2023-01-18T22:57:29.449017Z",
"url": "https://files.pythonhosted.org/packages/73/86/bbb18f0ecd079e051ba6546280da10bd1fd5d09020e87949bde1c1a02391/databased-0.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "615a960e61e9f405b1c90f4960ab3e0fbdf670d52596fb28f6daebca07aed80c",
"md5": "77f5c44dcb0abd574247a4fef07c175e",
"sha256": "4126b9fba6b60b8f0ab2f7259a562008964e414b41fbca7d263c02a4b82d5449"
},
"downloads": -1,
"filename": "databased-0.1.1.tar.gz",
"has_sig": false,
"md5_digest": "77f5c44dcb0abd574247a4fef07c175e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 25088,
"upload_time": "2023-01-18T22:57:30",
"upload_time_iso_8601": "2023-01-18T22:57:30.721281Z",
"url": "https://files.pythonhosted.org/packages/61/5a/960e61e9f405b1c90f4960ab3e0fbdf670d52596fb28f6daebca07aed80c/databased-0.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-01-18 22:57:30",
"github": false,
"gitlab": false,
"bitbucket": false,
"lcname": "databased"
}