簡體   English   中英

如何將數據推送到 Python 中的 DBHub.io

[英]How to push data to DBHub.io in Python

我想知道是否有辦法連接到 Python 中的 sqlite DB 並將該數據推送到 DBHub.io 中的雲。 原因是我已經在本地 DBBrowser 中擁有我的數據庫,現在我想將它推送到雲中。

您可以嘗試由 DBHub.io 團隊在其用於通過 ZDB974238714CA8DE634A7CE1D083A14F 訪問 DBHub.io 的部分庫中發布的庫 pydbhub

該庫在其更多示例中包含一個名為“上傳數據庫”的部分,該部分指向此 main.py 文件

對於 StackOverflow 中的歷史研究,今天的示例如下所示:

import sys
import os
import datetime
from platform import python_version

# https://github.com/willmcgugan/rich
from rich.console import Console
from rich.theme import Theme

import pydbhub.dbhub as dbhub


if __name__ == '__main__':
    custom_theme = Theme({
        "info": "green",
        "warning": "yellow",
        "error": "bold red"
    })
    console = Console(theme=custom_theme)

    if python_version()[0:3] < '3.7':
        console.print(
            "[ERROR] Make sure you have Python 3.7+ installed, quitting.\n\n", style="error")
        sys.exit(1)

    # Create a new DBHub.io API object
    db = dbhub.Dbhub(config_file=f"{os.path.join(os.path.dirname(__file__), '..', 'config.ini')}")

    # Prepare any information you want to include with the upload (eg a commit message, etc)
    info = dbhub.UploadInformation(
        commitmsg="An example upload",
        committimestamp=datetime.datetime.fromisoformat("2021-06-01 10:00:00"),
    )

    try:
        # Read the database file
        myDB = os.path.join(os.getcwd(), "examples", "upload", 'example.db')
        f = open(myDB, 'rb')
    except FileNotFoundError:
        print(f"File {myDB} not found.  Aborting")
        sys.exit(1)
    except OSError:
        print(f"OS error occurred trying to open {myDB}")
        sys.exit(1)
    except Exception as err:
        print(f"Unexpected error opening {myDB} is", repr(err))
        sys.exit(1)
    else:
        with f:
            # Upload the database
            res, err = db.Upload(db_name='somedb.sqlite', info=info, db_bytes=f)
            if err is not None:
                console.print(f"[ERROR] {err}", style="error")
                sys.exit(1)

            console.print(f"Database uploaded, commit: {res['commit']}", style="info")

高溫高壓

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM