Skip to content

Commit 94bb910

Browse files
Merge pull request #27 from sqlitecloud/#8-compatibility-sqlite3-dbapi2
#8 compatibility dbapi2/sqlite3
2 parents 8c06bac + e703f10 commit 94bb910

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+4020
-785
lines changed

.devcontainer/py3.6-dev/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ ADD https://dl.yarnpkg.com/debian/pubkey.gpg /etc/apt/trusted.gpg.d/yarn.asc
44

55
RUN chmod +r /etc/apt/trusted.gpg.d/*.asc && \
66
echo "deb http://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list
7+
8+
COPY install-legacy-extensions.sh /usr/local/bin/install-legacy-extensions.sh
9+
RUN chmod +x /usr/local/bin/install-legacy-extensions.sh

.devcontainer/py3.6-dev/devcontainer.json

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,24 @@
1717
// "forwardPorts": [],
1818

1919
// Use 'postCreateCommand' to run commands after the container is created.
20-
// "postCreateCommand": "pip3 install --user -r requirements.txt",
20+
"postCreateCommand": "install-legacy-extensions.sh",
2121

2222
// Configure tool-specific properties.
2323
// Py3.6 support (switch extensions to `pre-release` and `install another version`):
2424
// Pylance v2022.6.30
2525
// Python v2022.8.1
26-
// Python Debugger v2023.1.XXX (pre-release version | debugpy v1.5.1)
26+
// Python Debugger v2023.1.12492010 (pre-release version | debugpy v1.5.1)
2727
// Black Formatter v2022.2.0
2828
// Isort v2022.1.11601002 (pre-release)
2929
"customizations": {
3030
"vscode": {
3131
"extensions": [
3232
"littlefoxteam.vscode-python-test-adapter",
3333
"jkillian.custom-local-formatters",
34-
"ms-python.vscode-pylance",
35-
"ms-python.python",
36-
"ms-python.debugpy",
37-
"ms-python.black-formatter",
38-
"ms-python.isort",
3934
"ms-toolsai.jupyter"
4035
]
4136
}
42-
}
37+
},
4338

4439
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
4540
// "remoteUser": "root"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
code --install-extension "ms-python.python@2022.8.1"
4+
code --install-extension "ms-python.vscode-pylance@2022.6.30"
5+
code --install-extension "ms-python.debugpy@2023.1.12492010"
6+
code --install-extension "ms-python.black-formatter@2022.2.0"
7+
code --install-extension "ms-python.isort@2022.1.11601002"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
2+
// README at: https://github.com/devcontainers/templates/tree/main/src/python
3+
{
4+
"name": "Python 3.7",
5+
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
6+
"image": "mcr.microsoft.com/devcontainers/python:3.7",
7+
"features": {
8+
"ghcr.io/warrenbuckley/codespace-features/sqlite:1": {}
9+
},
10+
11+
// Features to add to the dev container. More info: https://containers.dev/features.
12+
// "features": {},
13+
14+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
15+
// "forwardPorts": [],
16+
17+
// Use 'postCreateCommand' to run commands after the container is created.
18+
// "postCreateCommand": "pip3 install --user -r requirements.txt",
19+
20+
// Configure tool-specific properties.
21+
"customizations": {
22+
"vscode": {
23+
"extensions": [
24+
"littlefoxteam.vscode-python-test-adapter",
25+
"jkillian.custom-local-formatters",
26+
"ms-python.vscode-pylance",
27+
"ms-python.python",
28+
"ms-python.debugpy",
29+
"ms-python.black-formatter",
30+
"ms-python.isort",
31+
"ms-toolsai.jupyter"
32+
]
33+
}
34+
}
35+
36+
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
37+
// "remoteUser": "root"
38+
}

.github/workflows/test.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ jobs:
1313
strategy:
1414
fail-fast: false
1515
matrix:
16-
python-version: ["3.6", "3.12"]
16+
# last supported for sqlitecloud, last security maintained, last release
17+
python-version: ["3.6", "3.8", "3.12"]
1718

1819
steps:
1920
- uses: actions/checkout@v4

.gitignore

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
build/
33
experiments/
44
sdk/
5-
venv/
5+
.venv*/
66
main.dSYM/
77
.env
88
*.pyc
@@ -12,6 +12,11 @@ main.dSYM/
1212
.DS_Store
1313

1414
.idea
15-
SqliteCloud.egg-info
15+
sqlitecloud.egg-info
1616

1717
playground.ipynb
18+
19+
src/tests/assets/*-shm
20+
src/tests/assets/*-wal
21+
22+
chinook.sqlite

README.md

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,43 @@
44
<img src="https://sqlitecloud.io/social/logo.png" height="300" alt="SQLite Cloud logo">
55
</p>
66

7-
![Build Status](https://github.com/sqlitecloud/sqlitecloud-py/actions/workflows/deploy.yaml/badge.svg "Build Status")
8-
[![codecov](https://codecov.io/github/sqlitecloud/python/graph/badge.svg?token=38G6FGOWKP)](https://codecov.io/github/sqlitecloud/python)
7+
![Build Status](https://github.com/sqlitecloud/sqlitecloud-py/actions/workflows/test.yaml/badge.svg "Build Status")
8+
[![codecov](https://codecov.io/gh/sqlitecloud/sqlitecloud-py/graph/badge.svg?token=38G6FGOWKP)](https://codecov.io/gh/sqlitecloud/sqlitecloud-py)
99
![PyPI - Version](https://img.shields.io/pypi/v/sqlitecloud?link=https%3A%2F%2Fpypi.org%2Fproject%2FSqliteCloud%2F)
1010
![PyPI - Downloads](https://img.shields.io/pypi/dm/sqlitecloud?link=https%3A%2F%2Fpypi.org%2Fproject%2FSqliteCloud%2F)
1111
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/sqlitecloud?link=https%3A%2F%2Fpypi.org%2Fproject%2FSqliteCloud%2F)
1212

1313

14-
- [Driver for SQLite Cloud](#driver-for-sqlite-cloud)
15-
- [Example](#example)
14+
- [SQLite Cloud](#)
15+
- [Compatibility with sqlite3 API](#compatibility-with-sqlite3-api)
16+
- [Autocommit transactions: Difference between sqlitecloud and sqlite3](#autocommit-transactions-difference-between-sqlitecloud-and-sqlite3)
17+
- [Installation and Usage](#installation-and-usage)
1618
- [SQLite Cloud loves sqlite3](#sqlite-cloud-loves-sqlite3)
19+
- [SQLite Cloud for SQLAlchemy (beta)](#sqlite-cloud-for-sqlalchemy-beta)
1720
- [SQLite Cloud for Pandas DataFrame](#sqlite-cloud-for-pandas-dataframe)
1821

1922
---
2023

2124
[SQLite Cloud](https://sqlitecloud.io) is a powerful Python package that allows you to interact with the SQLite Cloud database seamlessly. It provides methods for various database operations. This package is designed to simplify database operations in Python applications, making it easier than ever to work with SQLite Cloud.
2225

2326

24-
#### Compatibility with sqlite3 API
27+
## Compatibility with sqlite3 API
2528

26-
We aim for full compatibility with the Python built-in [sqlite3](https://docs.python.org/3.6/library/sqlite3.html) API (based on Python [PEP 249](https://peps.python.org/pep-0249)), with the primary distinction being that our driver connects to SQLite Cloud databases. This allows you to migrate your local SQLite databases to SQLite Cloud without needing to modify your existing Python code that uses the sqlite3 API.
29+
We aim for full compatibility with the Python built-in [sqlite3](https://docs.python.org/3.6/library/sqlite3.html) API (based on Python DBAPI 2.0 [PEP 249](https://peps.python.org/pep-0249)), with the primary distinction being that our driver connects to SQLite Cloud databases. This allows you to migrate your local SQLite databases to SQLite Cloud without needing to modify your existing Python code that uses the sqlite3 API.
2730

2831
- Documentation: Our API closely follows the sqlite3 API. You can refer to the sqlite3 documentation for most functionality. The list of implemented features are documented [here](https://github.com/sqlitecloud/sqlitecloud-py/issues/8).
2932
- Source: [https://github.com/sqlitecloud/sqlitecloud-py](https://github.com/sqlitecloud/sqlitecloud-py)
3033
- Site: [https://sqlitecloud.io](https://sqlitecloud.io/developers)
3134

32-
## Example
35+
### Autocommit transactions: Difference between sqlitecloud and sqlite3
36+
37+
In `sqlitecloud`, autocommit is **always enabled**, and we currently do not support disabling it. This means that the `isolation_level` is always set to `None`, resulting in autocommit being permanently on.
38+
39+
This behavior differs from the sqlite3 Python module, where autocommit can be controlled (see details in the section [Controlling Transactions](https://docs.python.org/3.6/library/sqlite3.html#controlling-transactions) in the official documentation).
40+
41+
To manage transactions in sqlitecloud, you should explicitly use the `BEGIN`, `ROLLBACK`, `SAVEPOINT`, and `RELEASE` commands as needed.
42+
43+
## Installation and Usage
3344

3445
```bash
3546
$ pip install sqlitecloud
@@ -87,6 +98,81 @@ for row in cursor:
8798
print(row)
8899
```
89100

101+
## SQLite Cloud for SQLAlchemy (beta)
102+
103+
_This is an initial release, features and stability may not be guaranteed in all scenarios._
104+
105+
_If you encounter any bugs or issues, please feel free to open an issue on our GitHub repository._
106+
107+
We’ve implemented the initial support for `sqlitecloud` with [SQLAlchemy](https://www.sqlalchemy.org/), allowing you to utilize all standard SQLAlchemy operations and queries.
108+
For further information, please see the dedicated [REDAME](https://github.com/sqlitecloud/sqlitecloud-py/tree/%238-compatibility-sqlite3-dbapi2/sqlalchemy-sqlitecloud).
109+
110+
### Example
111+
112+
_The example is based on `chinook.sqlite` databse on SQLite Cloud_
113+
114+
Install the package:
115+
116+
```bash
117+
$ pip install sqlalchemy-sqlitecloud
118+
```
119+
120+
121+
```python
122+
import sqlalchemy
123+
from sqlalchemy import Column, ForeignKey, Integer, String
124+
from sqlalchemy.dialects import registry
125+
from sqlalchemy.orm import backref, declarative_base, relationship, sessionmaker
126+
127+
Base = declarative_base()
128+
129+
130+
class Artist(Base):
131+
__tablename__ = "artists"
132+
133+
ArtistId = Column("ArtistId", Integer, primary_key=True)
134+
Name = Column("Name", String)
135+
Albums = relationship("Album", backref=backref("artist"))
136+
137+
138+
class Album(Base):
139+
__tablename__ = "albums"
140+
141+
AlbumId = Column("AlbumId", Integer, primary_key=True)
142+
ArtistId = Column("ArtistId", Integer, ForeignKey("artists.ArtistId"))
143+
Title = Column("Title", String)
144+
145+
# SQLite Cloud connection string
146+
connection_string = "sqlitecloud://myhost.sqlite.cloud:8860/mydatabase.sqlite?apikey=myapikey"
147+
148+
engine = sqlalchemy.create_engine(connection_string)
149+
Session = sessionmaker(bind=engine)
150+
session = Session()
151+
152+
name = "John Doe"
153+
query = sqlalchemy.insert(Artist).values(Name=name)
154+
result_insert = session.execute(query)
155+
156+
title = "The Album"
157+
query = sqlalchemy.insert(Album).values(
158+
ArtistId=result_insert.lastrowid, Title=title
159+
)
160+
session.execute(query)
161+
162+
query = (
163+
sqlalchemy.select(Artist, Album)
164+
.join(Album, Artist.ArtistId == Album.ArtistId)
165+
.where(Artist.ArtistId == result_insert.lastrowid)
166+
)
167+
168+
result = session.execute(query).fetchone()
169+
170+
print("Artist Name: " + result[0].Name)
171+
print("Album Title: " + result[1].Title)
172+
173+
```
174+
175+
90176
## SQLite Cloud for Pandas DataFrame
91177

92178
[Pandas](https://pypi.org/project/pandas/) is a Python package for data manipulation and analysis. It provides high-performance, easy-to-use data structures, such as DataFrame.

0 commit comments

Comments
 (0)