Skip to content

Instantly share code, notes, and snippets.

@cjbj
Last active August 18, 2025 23:56
Show Gist options
  • Select an option

  • Save cjbj/57602464062ee554c4801cb223ba8e39 to your computer and use it in GitHub Desktop.

Select an option

Save cjbj/57602464062ee554c4801cb223ba8e39 to your computer and use it in GitHub Desktop.
Querying an Oracle Database 23ai VECTOR column directly into a DataFrame using python-oracledb from the video "Python DataFrames with Oracle Database for Analysis and AI" https://youtu.be/lC07FNCzJ5w For other examples, see https://github.com/oracle/python-oracledb/tree/main/samples
# vector-query.py
#
# Run this after running vector-insert.py, https://gist.github.com/cjbj/ee7152b223928b5b7a5959fe87fa2ab3
#
# For other python-oracledb DataFrame examples, see https://github.com/oracle/python-oracledb/tree/main/samples
# Related blog: https://medium.com/oracledevs/the-best-way-to-fetch-and-insert-python-dataframes-and-tensors-with-oracle-database-70a1b24a3d99
# Documentation: https://python-oracledb.readthedocs.io/en/latest/user_guide/dataframes.html
import os
import array
import pyarrow
import oracledb
un = os.environ.get('ORACLE_USERNAME')
pw = os.environ.get('ORACLE_PASSWORD')
cs = os.environ.get('ORACLE_DSN')
connection = oracledb.connect(user=un, password=pw, dsn=cs)
#-------------------------------------------------------------------------------
odf = connection.fetch_df_all("select col1, col2 from myvectab")
df = pyarrow.table(odf).to_pandas()
print("\nDataFrame:")
print(df)
print("\nSum:")
row1 = df.iloc[0]
row2 = df.iloc[1]
print(row1 + row2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment