Skip to content

Instantly share code, notes, and snippets.

@gordthompson
Last active May 3, 2024 14:48
Show Gist options
  • Save gordthompson/09ef4113a1c681ce2290499052c6d1a6 to your computer and use it in GitHub Desktop.
Save gordthompson/09ef4113a1c681ce2290499052c6d1a6 to your computer and use it in GitHub Desktop.
using .to_sql() with run_transaction()
import pandas as pd
import sqlalchemy as sa
from sqlalchemy_cockroachdb.transaction import run_transaction
engine = sa.create_engine(
"cockroachdb+psycopg2://root@localhost:26257/defaultdb"
)
def crdb_insert_trans(table, eng, keys, data_iter):
# to_sql() "method=" adapted from the example at
# https://pandas.pydata.org/docs/user_guide/io.html#io-sql-method
def callback(conn):
tbl = sa.Table(
table.name,
sa.MetaData(),
schema=table.schema,
autoload_with=eng,
)
data = [dict(zip(keys, row)) for row in data_iter]
conn.execute(sa.insert(tbl), data)
run_transaction(eng, callback)
df = pd.DataFrame([(1, "foo"), (2, "bar")], columns=["id", "txt"])
df.to_sql(
"table1", engine, if_exists="append", index=False, method=crdb_insert_trans
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment