I am currently working on a python plugin, and I would like store data in the database so that it is still available after a reboot.
So I have a variable A with Value 12345. After a reboot of the plugin I want to retrieve the value 12345 from the database.
If I am correct this is not yet possible, is this correct?
Will this be possible in the future?
Python plugin store data in database
Moderator: leecollings
-
Ewaldharmsen
- Posts: 130
- Joined: Tuesday 07 February 2017 15:00
- Target OS: Linux
- Domoticz version:
- Contact:
Re: Python plugin store data in database
What python plugin developpers usaly do is to store those data in a local file created/updated and managed by the plugin itself.
For instance you can use some serialize python module like json or pickle to store and read your data
For instance you can use some serialize python module like json or pickle to store and read your data
- Dnpwwo
- Posts: 820
- Joined: Sunday 23 March 2014 9:00
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Beta
- Location: Melbourne, Australia
- Contact:
Re: Python plugin store data in database
@Ewaldharmsen,
There is no specific support from the Plugin Framework for database access other than values in the Parameter dictionary to help you locate database itself.
Developers of C++ hardware support access the database directly using a sqlite3 library and there is nothing to prevent Python developers from doing the same using the Python sqlite3 module that ships with Python.
Creating your own table and storing data is a better user experience than writing a data file because restoring a database will give users a consistent experience of rolling your plugin data back to time of the backup along with the rest of Domoticz.
There is no specific support from the Plugin Framework for database access other than values in the Parameter dictionary to help you locate database itself.
Developers of C++ hardware support access the database directly using a sqlite3 library and there is nothing to prevent Python developers from doing the same using the Python sqlite3 module that ships with Python.
Creating your own table and storing data is a better user experience than writing a data file because restoring a database will give users a consistent experience of rolling your plugin data back to time of the backup along with the rest of Domoticz.
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
-
Ewaldharmsen
- Posts: 130
- Joined: Tuesday 07 February 2017 15:00
- Target OS: Linux
- Domoticz version:
- Contact:
Re: Python plugin store data in database
Do you have an example for me?Dnpwwo wrote: Friday 29 March 2019 1:19 @Ewaldharmsen,
There is no specific support from the Plugin Framework for database access other than values in the Parameter dictionary to help you locate database itself.
Developers of C++ hardware support access the database directly using a sqlite3 library and there is nothing to prevent Python developers from doing the same using the Python sqlite3 module that ships with Python.
Creating your own table and storing data is a better user experience than writing a data file because restoring a database will give users a consistent experience of rolling your plugin data back to time of the backup along with the rest of Domoticz.
-
Ewaldharmsen
- Posts: 130
- Joined: Tuesday 07 February 2017 15:00
- Target OS: Linux
- Domoticz version:
- Contact:
Re: Python plugin store data in database
Another question: is it possible to store data on the hardware page itself?
Simply putting Parameters["Mode2"] == "test" obviously does not work.
I would like a more dynamic hardware page.
In this case I want to have an optional field on the hardware page. If people fill it in it is ok. If they don't I want to fill it in from the plugin.
Simply putting Parameters["Mode2"] == "test" obviously does not work.
I would like a more dynamic hardware page.
In this case I want to have an optional field on the hardware page. If people fill it in it is ok. If they don't I want to fill it in from the plugin.
- Dnpwwo
- Posts: 820
- Joined: Sunday 23 March 2014 9:00
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Beta
- Location: Melbourne, Australia
- Contact:
Re: Python plugin store data in database
@Ewaldharmsen,
There is no specific example because it very straightforward. Search 'python sqlite3' on the internet and there are tutorials on what to do.
The plugin is handed the database file name in the 'Parameters["Database"]' dictionary entry.
To modify the parameters you could update 'Mode2' in the 'Hardware' table where the 'Extra' field = 'Parameters["Key"]'
There is no specific example because it very straightforward. Search 'python sqlite3' on the internet and there are tutorials on what to do.
The plugin is handed the database file name in the 'Parameters["Database"]' dictionary entry.
To modify the parameters you could update 'Mode2' in the 'Hardware' table where the 'Extra' field = 'Parameters["Key"]'
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
-
Ewaldharmsen
- Posts: 130
- Joined: Tuesday 07 February 2017 15:00
- Target OS: Linux
- Domoticz version:
- Contact:
Re: Python plugin store data in database
thank you very much, works like a charm!