sqlite_micro_logger_arduino
sqlite_micro_logger_arduino copied to clipboard
Is it possible to have both a read and write context active for a db?
If I use fp = fopen("dbfile.db", "r+b");
Could I init both a dblog_write_context
and dblog_read_context
at the same time? If possible, what are the possible consequences?
Please see here for an example.
There is no issue of using both for the same handle because both read and write functions that are defined are expected to seek
the position before writing or reading. So opening using "r+b" is alright for such operations.
presumably if one would like to read back what has just been written the writes should be flushed and then the entry should be finalized??
Read and write contexts are to be used independently. The functions that take read context as parameter do not know about any write context and vice versa. So any pending writes are to be flushed by the caller before any reads can be performed.
I ask because I'm having an issue where if I call dblog_init_for_append()
, insert a couple of entries with dblog_append_row_with_values()
write an entry the call dblog_finalize()
, I can't read back that entry. If I restart the ESP all those entries are also not present.
However If I close the file after finalizing with fclose(), I can both read them and they're retained on reboots.
When dblog_finalize()
is called, it writes to disk the page that is being written to and calls the flush function defined. So it should be possible to read it back.
I can't find it now, but I vaguely remember some such issue where flush()
was not working as intended for SPIFFS or something like that. Please try calling flush() more than once.
After I call finalize, do I need to init the db again?
After I call finalize, do I need to init the db again?
Yes dblog_read_init() for the read context. However, the file need not be closed as all the File IO are being done in the callback functions. The callback functions can re-use the open file handle.