0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

python sqlite ベクトル

Posted at

sqliteでベクトルを保存管理する方法

    text = "こんにちは"
    vector = ai.getTextVector(text)
    print(len(vector))

    # ------書き込み
    # 配列データ

    # データをバイナリ形式に変換する
    blob_data = pickle.dumps(vector)

    # SQLite データベースに接続(データベースが存在しない場合は新しく作成される)
    conn = sqlite3.connect('example.db')

    # カーソルを作成
    cursor = conn.cursor()

    # テーブルを作成(すでに存在している場合は先に削除)
    cursor.execute("DROP TABLE IF EXISTS my_table")
    cursor.execute("CREATE TABLE my_table (id INTEGER PRIMARY KEY, data BLOB)")

    # データを挿入
    cursor.execute("INSERT INTO my_table (data) VALUES (?)", (blob_data,))

    # 変更をコミット
    conn.commit()

    # データベースの接続を閉じる
    conn.close()

読み取り方法

    # --------読み取り
    import sqlite3
    import pickle

    # SQLite データベースに接続
    conn = sqlite3.connect('example.db')

    # カーソルを作成
    cursor = conn.cursor()

    # データを取得
    cursor.execute("SELECT data FROM my_table WHERE id = 1")
    blob_data = cursor.fetchone()[0]

    # バイナリデータを配列に変換
    data = pickle.loads(blob_data)

    # 配列を表示
    print(data)

    # データ比較
    print(vector == data)

あとは類似度なり計算なりに活用

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?