Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

why after serveral iteration it clear all database content without warning #5

Open
wanghaisheng opened this issue Sep 9, 2021 · 2 comments

Comments

@wanghaisheng
Copy link

wanghaisheng commented Sep 9, 2021

    db_path = "data/aww_reddit.db"
    print('add image process task',f)
    filename = os.path.basename(f).split('.')[0]
    videoid = b64e(filename)
    nowtime = time.time()
    update_time = int(nowtime)    
    data = {"videoid": videoid,
            "status": 0,
            "update_time": update_time}
    # data = json.dumps(data)
    with JsonDatabase("aww_reddit", db_path) as db:
        db.add_item(data)

update after creation

        data = {"videoid": videoid,
                "status": 1,
                "title": formattitle,
                "update_time": update_time,
                "des": des,
                'videopath': upload_video,
                'rapidtags': rapidtags,
                "tags": tags}

        with JsonDatabase("aww_reddit", db_path) as db:
            results = db.search_by_value("videoid", videoid)

            olditem =results[0]
            print(olditem)
            item_id = db.get_item_id(olditem)

            if item_id >= 0:
                db.update_item(item_id, data)

            else:
                print("item not found in database")

@kevinelgan

  File "D:\Program Files\anaconda3\lib\site-packages\json_database\__init__.py", line 189, in add_item
    if allow_duplicates or value not in self:   
  File "D:\Program Files\anaconda3\lib\site-packages\json_database\__init__.py", line 163, in __contains__
    return item in self.db[self.name]
KeyError: 'aww_reddit'
@wanghaisheng
Copy link
Author

Error loading json 'data/aww_reddit.db'
UnicodeDecodeError('gbk', b'{\r\n    "aww_reddit": [\r\n        {\r\n      

@ChanceNCounter
Copy link
Contributor

I'm just driving by, but do you exit that context between those snippets? If not, do you commit the database someplace?

As I understand it, when invoked with a context manager, the database should save itself on exit. It behaves much like sqlite3 in that regard. But if you stay in context, that automatic commit doesn't happen (also like sqlite3, come to think of it)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants