|
1 | | -# Python SDK for SQLite Cloud |
| 1 | +# Driver for SQLite Cloud |
2 | 2 |
|
3 | 3 | <p align="center"> |
4 | 4 | <img src="https://sqlitecloud.io/social/logo.png" height="300" alt="SQLite Cloud logo"> |
|
11 | 11 |  |
12 | 12 |
|
13 | 13 |
|
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) |
15 | 18 |
|
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 |
19 | 25 |
|
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. |
21 | 27 |
|
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 |
23 | 33 |
|
24 | 34 | ```bash |
25 | 35 | $ pip install sqlitecloud |
26 | 36 | ``` |
27 | 37 |
|
28 | | -## Usage |
29 | | -<hr> |
30 | | - |
31 | 38 | ```python |
32 | | -from sqlitecloud.client import SqliteCloudClient |
33 | | -from sqlitecloud.types import SqliteCloudAccount |
34 | | -``` |
| 39 | +import sqlitecloud |
35 | 40 |
|
36 | | -### _Init a connection_ |
| 41 | +# Open the connection to SQLite Cloud |
| 42 | +conn = sqlitecloud.connect("sqlitecloud://myhost.sqlite.cloud:8860?apikey=myapikey") |
37 | 43 |
|
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}") |
39 | 50 |
|
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() |
45 | 53 |
|
46 | | -#### _Using string configuration_ |
| 54 | +print(result) |
47 | 55 |
|
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() |
52 | 57 | ``` |
53 | 58 |
|
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 | + |
56 | 67 | ```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)], |
60 | 82 | ) |
61 | | -``` |
62 | 83 |
|
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: |
67 | 87 | print(row) |
68 | 88 | ``` |
69 | 89 |
|
70 | | -### _Specific value_ |
71 | | -```python |
72 | | -result.get_value(0, 0) |
73 | | -``` |
| 90 | +## SQLite Cloud for Pandas DataFrame |
74 | 91 |
|
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. |
79 | 97 |
|
80 | | -### _Close connection_ |
| 98 | +Example: |
81 | 99 |
|
82 | 100 | ```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) |
84 | 134 | ``` |
0 commit comments