pikapython/examples/flashdb/flashdb_kvdb1.py

35 lines
981 B
Python
Raw Normal View History

2023-12-07 22:22:42 +08:00
import flashdb
import struct
DB_PATH = "test/out/fdb_kvdb"
print('test boot_count increment 1')
2024-07-01 22:43:27 +08:00
boot_count = 0
2023-12-07 22:22:42 +08:00
boot_count_blob = struct.pack('i', boot_count)
2024-07-01 22:43:27 +08:00
boot_times = [0, 1, 2, 3, 0, 0, 0, 0, 0, 0]
2023-12-07 22:22:42 +08:00
boot_time_tuple = tuple(boot_times)
2024-07-01 22:43:27 +08:00
boot_time_blob = struct.pack('@10Q', *boot_time_tuple)
default_kv = {
2023-12-07 22:22:42 +08:00
'username': 'armink', # string KV
'password': "123456", # string KV
'boot_count': boot_count_blob, # int type kv
'boot_time': boot_time_blob, # array type kv
}
2024-07-01 22:43:27 +08:00
# print(default_kv)
2023-12-07 22:22:42 +08:00
fdb = flashdb.KVDB("env", DB_PATH, default_kv, None)
res = fdb.get_blob("boot_count", len(boot_count_blob))
assert res is not None
2024-07-01 22:43:27 +08:00
boot_count = struct.unpack("i", res)[0]
boot_count = boot_count+1
2023-12-07 22:22:42 +08:00
boot_count_blob = struct.pack('i', boot_count)
2024-07-01 22:43:27 +08:00
2023-12-07 22:22:42 +08:00
fdb.set_blob("boot_count", boot_count_blob)
res = fdb.get_blob("boot_count", len(boot_count_blob))
assert res is not None
2024-07-01 22:43:27 +08:00
new_boot_count = struct.unpack("i", res)[0]
2023-12-07 22:22:42 +08:00
assert new_boot_count == boot_count
2023-12-07 23:03:01 +08:00
print('PASS')