|
5 | 5 | </p> |
6 | 6 |
|
7 | 7 |  |
8 | | -[](https://codecov.io/github/sqlitecloud/python) |
| 8 | +[](https://codecov.io/gh/sqlitecloud/sqlitecloud-py) |
9 | 9 |  |
10 | 10 |  |
11 | 11 |  |
12 | 12 |
|
13 | 13 |
|
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) |
16 | 18 | - [SQLite Cloud loves sqlite3](#sqlite-cloud-loves-sqlite3) |
| 19 | +- [SQLite Cloud for SQLAlchemy (beta)](#sqlite-cloud-for-sqlalchemy-beta) |
17 | 20 | - [SQLite Cloud for Pandas DataFrame](#sqlite-cloud-for-pandas-dataframe) |
18 | 21 |
|
19 | 22 | --- |
20 | 23 |
|
21 | 24 | [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. |
22 | 25 |
|
23 | 26 |
|
24 | | -#### Compatibility with sqlite3 API |
| 27 | +## Compatibility with sqlite3 API |
25 | 28 |
|
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. |
27 | 30 |
|
28 | 31 | - 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). |
29 | 32 | - Source: [https://github.com/sqlitecloud/sqlitecloud-py](https://github.com/sqlitecloud/sqlitecloud-py) |
30 | 33 | - Site: [https://sqlitecloud.io](https://sqlitecloud.io/developers) |
31 | 34 |
|
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 |
33 | 44 |
|
34 | 45 | ```bash |
35 | 46 | $ pip install sqlitecloud |
@@ -87,6 +98,81 @@ for row in cursor: |
87 | 98 | print(row) |
88 | 99 | ``` |
89 | 100 |
|
| 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 | + |
90 | 176 | ## SQLite Cloud for Pandas DataFrame |
91 | 177 |
|
92 | 178 | [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