PyDictDatabase is a Python Dictionary based database for tiny databases.
useful for
-
matrix or very small database
-
static database structure (more or less)
useless for
-
collecting data
-
growing databases
comment:
I am using this type of database in two of my projects:
-
for my plant watering system to collect my last status data
-
for my SmartPi as manageable & clear structure for matrixes
The SmartPi also uses a MySQL database to collect data. As there are many read/write cycles for some attributes. It makes more sense to leave them in memory which is also gentler on the memory card (Raspberry Pi).
-
use SELECT and UPDATE like MySQL
-
existing data types are retained
-
SELECT methods: fetchone & fetchall
-
UPDATE methods: commit
-
optional: export the last status data to a file and reload it during initialization
You have to save the file PyDictDatabase.py in your project directory.
from PyDictDatabase import *
data = {
'table1': {
1: {'id':1,'key':"value"},
2: {'id':2,'key':"value"},
},
'table2': {
1: {'id':1,'name':"name1",'status':True},
2: {'id':2,'name':"name2",'status':False},
},
}
DictDB = PyDictDatabase(database=data)
DictDB = PyDictDatabase(database=data,settings={"ERROR_OUTPUT":True})
options:
- "EXPORT_DATABASE":True for export the last status data to a file and reload it during initialization (default: False)
- "ERROR_OUTPUT":True for error output, DEBUG-MODE (default: False)
- "ERROR_EXIT":True to stop script with exit(1), if an error occurs with select/update (default: False)
result = DictDB.fetchone("SELECT status FROM table2 WHERE id = name1")
result = DictDB.fetchall("SELECT id,status FROM table2 WHERE id = name1")
result = DictDB.commit("UPDATE table2 SET status = False WHERE id = name1")
-
dict must start with 1, instead of 0
-
use the same keys per table (so it is also common with MySQL)
None - no entry found
20 SUCCESS - nothing amiss
40 ERROR - for further information activate ERROR_OUTPUT
44 NOT FOUND - name of the table/key not found
SYNTAX fetchone:
SELECT `KEY` FROM `TABLE` WHERE `KEY` = `VALUE`
SYNTAX fetchall:
SELECT `KEY` FROM `TABLE` WHERE `KEY` = `VALUE`
SELECT `KEY`,`KEY`,`KEY` FROM `TABLE` WHERE `KEY` = `VALUE`
SYNTAX commit:
UPDATE `TABLE` SET `KEY` = `VALUE` WHERE `KEY` = `VALUE`
UPDATE `TABLE` SET `KEY` = `VALUE`,`KEY` = `VALUE`,`KEY` = `VALUE` WHERE `KEY` = `VALUE`
-
implement a findall method for chaining
-
INSERT option to add a new entry
- new feature: Export-Database
- initial release