Skip to content

Commit 1cdd76f

Browse files
Merge pull request #12 from sqlitecloud/#8-initial-compatibility-sqlite3-lib
#8 - Initial support of SQLite Cloud to API-DB 2.0/SQLite3.
2 parents 063fad1 + 4b8f619 commit 1cdd76f

31 files changed

+2704
-611
lines changed

.devcontainer/devcontainer.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,22 @@
2020
// "postCreateCommand": "pip3 install --user -r requirements.txt",
2121

2222
// Configure tool-specific properties.
23+
// Py3.6 support (switch extensions to `pre-release` and `install another version`):
24+
// Pylance v2022.6.30
25+
// Python v2022.8.1
26+
// Python Debugger v2023.1.XXX (pre-release version | debugpy v1.5.1)
27+
// Black Formatter v2022.2.0
28+
// Isort v2022.1.11601002 (pre-release)
2329
"customizations": {
2430
"vscode": {
2531
"extensions": [
2632
"littlefoxteam.vscode-python-test-adapter",
27-
"jkillian.custom-local-formatters"
33+
"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"
2839
]
2940
}
3041
}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ main.dSYM/
1313

1414
.idea
1515
SqliteCloud.egg-info
16-
src/sqlitecloud/libsqcloud.so
16+
17+
playground.ipynb

.pre-commit-config.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ repos:
1010
- id: detect-private-key
1111
- id: check-merge-conflict
1212
# Using this mirror lets us use mypyc-compiled black, which is about 2x faster
13+
- repo: https://github.com/pycqa/isort
14+
rev: 5.10.1
15+
hooks:
16+
- id: isort
17+
name: isort
1318
- repo: https://github.com/psf/black-pre-commit-mirror
1419
rev: 22.8.0
1520
hooks:
@@ -19,11 +24,6 @@ repos:
1924
# pre-commit's default_language_version, see
2025
# https://pre-commit.com/#top_level-default_language_version
2126
language_version: python3.6
22-
- repo: https://github.com/pycqa/isort
23-
rev: 5.10.1
24-
hooks:
25-
- id: isort
26-
name: isort
2727
- repo: https://github.com/PyCQA/autoflake
2828
rev: v1.4
2929
hooks:
@@ -34,7 +34,7 @@ repos:
3434
- "--expand-star-imports"
3535
- "--remove-duplicate-keys"
3636
- "--remove-unused-variables"
37-
- "--remove-unused-variables"
37+
- "--remove-all-unused-imports"
3838
- repo: https://github.com/pycqa/flake8
3939
rev: 5.0.4
4040
hooks:

README.md

Lines changed: 95 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Python SDK for SQLite Cloud
1+
# Driver for SQLite Cloud
22

33
<p align="center">
44
<img src="https://sqlitecloud.io/social/logo.png" height="300" alt="SQLite Cloud logo">
@@ -11,74 +11,124 @@
1111
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/sqlitecloud?link=https%3A%2F%2Fpypi.org%2Fproject%2FSqliteCloud%2F)
1212

1313

14-
[SQLiteCloud](https://sqlitecloud.io) is a powerful Python package that allows you to interact with the SQLite Cloud backend server 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.
14+
- [Driver for SQLite Cloud](#driver-for-sqlite-cloud)
15+
- [Example](#example)
16+
- [SQLite Cloud loves sqlite3](#sqlite-cloud-loves-sqlite3)
17+
- [SQLite Cloud for Pandas DataFrame](#sqlite-cloud-for-pandas-dataframe)
1518

16-
- Site: [https://sqlitecloud.io](https://sqlitecloud.io/developers)
17-
- Documentation: https://..._coming!_
18-
- Source: [https://github.com/sqlitecloud/python](https://github.com/sqlitecloud/python)
19+
---
20+
21+
[SQLiteCloud](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+
23+
24+
#### Compatibility with sqlite3 API
1925

20-
## Installation
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.
2127

22-
You can install SqliteCloud Package using Python Package Index (PYPI):
28+
- 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/python/issues/8).
29+
- Source: [https://github.com/sqlitecloud/python](https://github.com/sqlitecloud/python)
30+
- Site: [https://sqlitecloud.io](https://sqlitecloud.io/developers)
31+
32+
## Example
2333

2434
```bash
2535
$ pip install sqlitecloud
2636
```
2737

28-
## Usage
29-
<hr>
30-
3138
```python
32-
from sqlitecloud.client import SqliteCloudClient
33-
from sqlitecloud.types import SqliteCloudAccount
34-
```
39+
import sqlitecloud
3540

36-
### _Init a connection_
41+
# Open the connection to SQLite Cloud
42+
conn = sqlitecloud.connect("sqlitecloud://myhost.sqlite.cloud:8860?apikey=myapikey")
3743

38-
#### Using explicit configuration
44+
# You can autoselect the database during the connect call
45+
# by adding the database name as path of the SQLite Cloud
46+
# connection string, eg:
47+
# conn = sqlitecloud.connect("sqlitecloud://myhost.sqlite.cloud:8860/mydatabase?apikey=myapikey")
48+
db_name = "chinook.sqlite"
49+
conn.execute(f"USE DATABASE {db_name}")
3950

40-
```python
41-
account = SqliteCloudAccount(user, password, host, db_name, port)
42-
client = SqliteCloudClient(cloud_account=account)
43-
conn = client.open_connection()
44-
```
51+
cursor = conn.execute("SELECT * FROM albums WHERE AlbumId = ?", (1, ))
52+
result = cursor.fetchone()
4553

46-
#### _Using string configuration_
54+
print(result)
4755

48-
```python
49-
account = SqliteCloudAccount("sqlitecloud://user:pass@host.com:port/dbname?apikey=myapikey")
50-
client = SqliteCloudClient(cloud_account=account)
51-
conn = client.open_connection()
56+
conn.close()
5257
```
5358

54-
### _Execute a query_
55-
You can bind values to parametric queries: you can pass parameters as positional values in an array
59+
## sqlitecloud loves sqlite3
60+
61+
Is your project based on the `sqlite3` library to interact with a SQLite database?
62+
63+
Just install `sqlitecloud` package from `pip` and change the module name! That's it!
64+
65+
Try it yourself:
66+
5667
```python
57-
result = client.exec_query(
58-
"SELECT * FROM table_name WHERE id = 1"
59-
conn=conn
68+
# import sqlitecloud
69+
import sqlite3
70+
71+
# comment out the following line...
72+
conn = sqlite3.connect(":memory:")
73+
74+
# ... and uncomment this line and import the sqlitecloud package
75+
# (add the database name like in this connection string)
76+
# conn = sqlitecloud.connect("sqlitecloud://myhost.sqlite.cloud:8860/mydatabase.sqlite?apikey=myapikey")
77+
78+
conn.execute("CREATE TABLE IF NOT EXISTS producers (ProducerId INTEGER PRIMARY KEY, name TEXT, year INTEGER)")
79+
conn.executemany(
80+
"INSERT INTO producers (name, year) VALUES (?, ?)",
81+
[("Sony Music Entertainment", 2020), ("EMI Music Publishing", 2021)],
6082
)
61-
```
6283

63-
### _Iterate result_
64-
result is an iterable object
65-
```python
66-
for row in result:
84+
cursor = conn.execute("SELECT * FROM cars")
85+
86+
for row in cursor:
6787
print(row)
6888
```
6989

70-
### _Specific value_
71-
```python
72-
result.get_value(0, 0)
73-
```
90+
## SQLite Cloud for Pandas DataFrame
7491

75-
### _Column name_
76-
```python
77-
result.get_name(0)
78-
```
92+
[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.
93+
94+
Use the connection to SQLite Cloud to:
95+
- Insert data from a DataFrame into a SQLite Cloud database.
96+
- Query SQLite Cloud and fetch the results into a DataFrame for further analysis.
7997

80-
### _Close connection_
98+
Example:
8199

82100
```python
83-
client.disconnect(conn)
101+
import io
102+
103+
import pandas as pd
104+
105+
import sqlitecloud
106+
107+
dfprices = pd.read_csv(
108+
io.StringIO(
109+
"""DATE,CURRENCY,PRICE
110+
20230504,USD,201.23456
111+
20230503,USD,12.34567
112+
20230502,USD,23.45678
113+
20230501,USD,34.56789"""
114+
)
115+
)
116+
117+
conn = sqlitecloud.connect("sqlitecloud://myhost.sqlite.cloud:8860/mydatabase.sqlite?apikey=myapikey")
118+
119+
conn.executemany("DROP TABLE IF EXISTS ?", [("PRICES",)])
120+
121+
# Write the dataframe to the SQLite Cloud database as a table PRICES
122+
dfprices.to_sql("PRICES", conn, index=False)
123+
124+
# Create the dataframe from the table PRICES on the SQLite Cloud database
125+
df_actual_prices = pd.read_sql("SELECT * FROM PRICES", conn)
126+
127+
# Inspect the dataframe
128+
print(df_actual_prices.head())
129+
130+
# Perform a simple query on the dataframe
131+
query_result = df_actual_prices.query("PRICE > 50.00")
132+
133+
print(query_result)
84134
```

0 commit comments

Comments
 (0)