@@ -36,7 +36,13 @@ DuckDB Engine also has a conda feedstock available, the instructions for the use
3636Once you've installed this package, you should be able to just use it, as SQLAlchemy does a python path search
3737
3838``` python
39- from sqlalchemy import Column, Integer, Sequence, String, create_engine
39+ from sqlalchemy import (
40+ Column,
41+ Integer,
42+ Sequence,
43+ String,
44+ create_engine,
45+ )
4046from sqlalchemy.ext.declarative import declarative_base
4147from sqlalchemy.orm.session import Session
4248
@@ -46,7 +52,11 @@ Base = declarative_base()
4652class FakeModel (Base ): # type: ignore
4753 __tablename__ = " fake"
4854
49- id = Column(Integer, Sequence(" fakemodel_id_sequence" ), primary_key = True )
55+ id = Column(
56+ Integer,
57+ Sequence(" fakemodel_id_sequence" ),
58+ primary_key = True ,
59+ )
5060 name = Column(String)
5161
5262
@@ -74,13 +84,11 @@ You can configure DuckDB by passing `connect_args` to the create_engine function
7484from sqlalchemy.engine import create_engine
7585
7686create_engine(
77- ' duckdb:///:memory:' ,
87+ " duckdb:///:memory:" ,
7888 connect_args = {
79- ' read_only' : False ,
80- ' config' : {
81- ' memory_limit' : ' 500mb'
82- }
83- }
89+ " read_only" : False ,
90+ " config" : {" memory_limit" : " 500mb" },
91+ },
8492)
8593```
8694
@@ -89,21 +97,23 @@ The supported configuration parameters are listed in the [DuckDB docs](https://d
8997## How to register a pandas DataFrame
9098
9199``` python
92- breakpoint ()
93100import pandas as pd
94101from sqlalchemy import text, __version__ as sqla_version
95102from sqlalchemy.engine import create_engine
96103
97104conn = create_engine(" duckdb:///:memory:" ).connect()
98105
99- df = pd.DataFrame([{' id ' : 0 }])
106+ df = pd.DataFrame([{" id " : 0 }])
100107
101- if sqla_version.startswith(' 1.3.' ):
108+ if sqla_version.startswith(" 1.3." ):
102109 # with SQLAlchemy 1.3
103110 conn.execute(" register" , (" dataframe_name" , df))
104111else :
105112 # with SQLAlchemy 1.4+
106- conn.execute(text(" register(:name, :df)" ), {" name" : " dataframe_name" , " df" : df})
113+ conn.execute(
114+ text(" register(:name, :df)" ),
115+ {" name" : " dataframe_name" , " df" : df},
116+ )
107117
108118conn.execute(text(" select * from dataframe_name" ))
109119```
@@ -118,19 +128,20 @@ The following example demonstrates how to create an auto-incrementing ID column
118128
119129``` python
120130import sqlalchemy
121- engine = sqlalchemy.create_engine(' duckdb:////path/to/duck.db' )
122- metadata = sqlalchemy.MetaData()
123- user_id_seq = sqlalchemy.Sequence(' user_id_seq' )
131+
132+ engine = sqlalchemy.create_engine(" duckdb:///:memory:" )
133+ metadata = sqlalchemy.MetaData(engine)
134+ user_id_seq = sqlalchemy.Sequence(" user_id_seq" )
124135users_table = sqlalchemy.Table(
125- ' users' ,
126- metadata,
127- sqlalchemy.Column(
128- ' id ' ,
129- sqlalchemy.Integer,
130- user_id_seq,
131- server_default = user_id_seq.next_value(),
132- primary_key = True ,
133- ),
136+ " users" ,
137+ metadata,
138+ sqlalchemy.Column(
139+ " id " ,
140+ sqlalchemy.Integer,
141+ user_id_seq,
142+ server_default = user_id_seq.next_value(),
143+ primary_key = True ,
144+ ),
134145)
135146metadata.create_all(bind = engine)
136147```
@@ -144,9 +155,10 @@ The `pandas.read_sql()` method can read tables from `duckdb_engine` into DataFra
144155``` python notest
145156import pandas as pd
146157import sqlalchemy
147- engine = sqlalchemy.create_engine(' duckdb:////path/to/duck.db' )
148- df = pd.read_sql(' users' , engine) # ## Works as expected
149- df = pd.read_sql(' users' , engine, chunksize = 25 ) # ## Throws an exception
158+
159+ engine = sqlalchemy.create_engine(" duckdb:////path/to/duck.db" )
160+ df = pd.read_sql(" users" , engine) # ## Works as expected
161+ df = pd.read_sql(" users" , engine, chunksize = 25 ) # ## Throws an exception
150162```
151163
152164### Unsigned integer support
@@ -162,6 +174,7 @@ This support can be enabling by adding an Alembic implementation class for the `
162174``` python notest
163175from alembic.ddl.impl import DefaultImpl
164176
177+
165178class AlembicDuckDBImpl (DefaultImpl ):
166179 """ Alembic implementation for DuckDB."""
167180
@@ -180,13 +193,11 @@ Until the DuckDB python client allows you to natively preload extensions, I've a
180193from sqlalchemy import create_engine
181194
182195create_engine(
183- ' duckdb:///:memory:' ,
196+ " duckdb:///:memory:" ,
184197 connect_args = {
185- ' preload_extensions' : [' https' ],
186- ' config' : {
187- ' s3_region' : ' ap-southeast-1'
188- }
189- }
198+ " preload_extensions" : [" https" ],
199+ " config" : {" s3_region" : " ap-southeast-1" },
200+ },
190201)
191202```
192203
0 commit comments