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/ee7152b223928b5b7a5959fe87fa2ab3 to your computer and use it in GitHub Desktop.

Select an option

Save cjbj/ee7152b223928b5b7a5959fe87fa2ab3 to your computer and use it in GitHub Desktop.
Inserting a DataFrame into a VECTOR column 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-insert.py
#
# 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 pandas
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)
cursor = connection.cursor()
#-------------------------------------------------------------------------------
# A table with two VECTOR columns for float64, each with a dimension of three
cursor.execute("""drop table if exists myvectab""")
cursor.execute("""create table myvectab (col1 vector(3, float64),
col2 vector(3, float64))""")
#-------------------------------------------------------------------------------
# Insert a DataFrame
d = {
"v1": [
[1.0, 1.3, 5.0],
[2.0, 1.0, 2.0],
],
"v2": [
[3.0, 1.3, 7.0],
[4.0, 5.3, 2.0],
],
}
df = pandas.DataFrame(data=d)
cursor.executemany("insert into myvectab (col1, col2) values (:1, :2)", df)
connection.commit()
#-------------------------------------------------------------------------------
# A normal query fetches as Python array() types
print()
cursor.execute("select * from myvectab")
print(*[col.name for col in cursor.description], sep='\t\t\t | ')
print("-" * 59)
for (c1, c2) in cursor.fetchall():
print(c1," | ", c2)
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment