Skip to content

Commit 0109658

Browse files
committed
Completed Swift Introduction
1 parent f854c20 commit 0109658

1 file changed

Lines changed: 52 additions & 19 deletions

File tree

sqlite-cloud/sdks/swift/introduction.mdx

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ status: publish
66
slug: sdk-swift-introduction
77
---
88

9-
This powerful package provides methods that perform database operations and enable real-time notifications in Swift apps, making it easier than ever to work with SQLite in the cloud.
9+
This powerful package provides methods that perform DB operations and enables real-time notifications in Swift apps, making it easier than ever to work with SQLite in the cloud. We encourage all users to log encountered issues in the [SDK's issues backlog](https://github.com/sqlitecloud/sqlitecloud-swift/issues).
1010

1111
## Install
1212

13-
- In `Package.swift`, add the `swift` package to `dependencies`. The following example snippet is from a project using the Vapor framework.
13+
- In `Package.swift`, add the `swift` package to `dependencies`.
14+
- The following example snippet is from a Swift app using the Vapor framework.
1415

1516
```swift
1617
let package = Package(
@@ -34,32 +35,64 @@ let package = Package(
3435

3536
## Basic Usage
3637

37-
-
38+
- Currently, there are 2 ways to configure your database connection:
39+
40+
- 1. Explicitly passing string params: `SQLiteCloudConfig(hostname: {hostname}, username: {username}, password: {password}, port: .default)`
41+
- In your SQLite Cloud account dashboard, click on a Node, copy the Deployment string, and replace `{hostname}` in `configuration` below.
42+
- In your dashboard left nav, select Settings, then Users. Copy your username and replace `{username}`.
43+
- In your User's row, click the down chevron, then Edit. Enter a Password and Save. Replace `{password}`.
3844

3945
```swift
4046
import SQLiteCloud
4147

42-
# explicit
43-
let configuration = SQLiteCloudConfig(hostname: "<see-project-deployment>", username: "<see-settings-users>", password: "<edit-your-user>", port: .default)
48+
let configuration: SQLiteCloudConfig = SQLiteCloudConfig(hostname: "<see-project-deployment>", username: "<see-settings-users>", password: "<edit-your-user>", port: .default)
49+
50+
let sqliteCloud: SQLiteCloud = SQLiteCloud(config: configuration)
51+
52+
do {
53+
try await sqliteCloud.connect()
54+
debugPrint("connected")
55+
56+
let sqlQuery: String = "USE DATABASE chinook.sqlite; SELECT albums.AlbumId as id, albums.Title as title, artists.name as artist FROM albums INNER JOIN artists WHERE artists.ArtistId = albums.ArtistId LIMIT 20;"
57+
58+
let result: SQLiteCloudResult = try await sqliteCloud.execute(query: sqlQuery)
59+
60+
try await sqliteCloud.disconnect()
61+
62+
return result.stringValue!
63+
} catch {
64+
return "Connection error: \(error)"
65+
}
4466
```
4567

46-
```python
47-
import sqlitecloud
68+
- 2. Using a connection string: `SQLiteCloudConfig(connectionString: "sqlitecloud://{username}:{password}@{hostname}:{port}/{database})`
4869

49-
# Open the connection to SQLite Cloud
50-
conn = sqlitecloud.connect("sqlitecloud://myhost.sqlite.cloud:8860?apikey=myapikey")
70+
```swift
71+
import SQLiteCloud
72+
73+
let configuration: SQLiteCloudConfig? = SQLiteCloudConfig(connectionString: "sqlitecloud://{username}:{password}@{hostname}:8860/chinook.sqlite")
74+
75+
let sqliteCloud: SQLiteCloud = SQLiteCloud(config: configuration!)
76+
77+
do {
78+
try await sqliteCloud.connect()
79+
debugPrint("connected")
5180

52-
# You can autoselect the database during the connect call
53-
# by adding the database name as path of the SQLite Cloud
54-
# connection string, eg:
55-
# conn = sqlitecloud.connect("sqlitecloud://myhost.sqlite.cloud:8860/mydatabase?apikey=myapikey")
56-
db_name = "chinook.sqlite"
57-
conn.execute(f"USE DATABASE {db_name}")
81+
let sqlQuery: String = "USE DATABASE chinook.sqlite; SELECT albums.AlbumId as id, albums.Title as title, artists.name as artist FROM albums INNER JOIN artists WHERE artists.ArtistId = albums.ArtistId LIMIT 20;"
5882

59-
cursor = conn.execute("SELECT * FROM albums WHERE AlbumId = ?", (1, ))
60-
result = cursor.fetchone()
83+
let result: SQLiteCloudResult = try await sqliteCloud.execute(query: sqlQuery)
6184

62-
print(result)
85+
try await sqliteCloud.disconnect()
6386

64-
conn.close()
87+
return result.stringValue!
88+
} catch {
89+
return "Connection error: \(error)"
90+
}
6591
```
92+
93+
- The above snippets include variable types, which may be optional for your app.
94+
95+
## Troubleshooting
96+
97+
- If you get errors indicating SQLite Cloud-specific constructs are out of scope (i.e. `error: cannot find 'SQLiteCloudConfig' in scope`), verify the `SQLiteCloud` package is correctly imported.
98+
- Confirm `https://github.com/sqlitecloud/swift` package is listed in `Package.resolved`.

0 commit comments

Comments
 (0)