Skip to content

Commit 0baa180

Browse files
authored
Merge pull request #78 from sqlitecloud/feat/swift-intro
Edited Swift Introduction
2 parents 3c29523 + 05962dc commit 0baa180

1 file changed

Lines changed: 28 additions & 42 deletions

File tree

sqlite-cloud/sdks/swift/introduction.mdx

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,71 +6,59 @@ status: publish
66
slug: sdk-swift-introduction
77
---
88

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).
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`.
14-
- The following example snippet is from a Swift app using the Vapor framework.
13+
- In `Package.swift`, add the `swift` package to `dependencies`.
1514

1615
```swift
1716
let package = Package(
18-
...
17+
...,
1918
dependencies: [
20-
...
19+
...,
2120
.package(url: "https://github.com/sqlitecloud/swift.git", from: "0.2.1")
2221
],
23-
targets: [
24-
.executableTarget(
25-
...
26-
dependencies: [
27-
...
28-
.product(name: "SQLiteCloud", package: "swift")
29-
],
30-
...
31-
),
32-
]
22+
...
3323
)
3424
```
3525

36-
## Basic Usage
26+
## 3 ways to configure your database connection
3727

38-
- Currently, there are 2 ways to configure your database connection:
28+
1. **RECOMMENDED**: Use the `apikey` connection string.
3929

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}`.
30+
In your SQLite Cloud account dashboard, click on `Show connection strings`, copy the Connection String, and replace `<your-connection-string>` below.
4431

4532
```swift
46-
import SQLiteCloud
47-
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")
33+
let configuration = SQLiteCloudConfig(connectionString: "<your-connection-string>")
34+
```
5535

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;"
36+
2. Use a parameterized connection string.
5737

58-
let result: SQLiteCloudResult = try await sqliteCloud.execute(query: sqlQuery)
38+
- In your SQLite Cloud account dashboard, click on a Node, copy the Deployment string, and replace `{hostname}` below.
39+
- In your dashboard left nav, select Settings, then Users. Copy your username and replace `{username}`.
40+
- In your User's row, click the down chevron, then Edit. Enter a Password and Save. Replace `{password}`.
41+
- The provided port and database will query the sample dataset that comes pre-loaded with SQLite Cloud. Replace both for your own datasets.
5942

60-
try await sqliteCloud.disconnect()
43+
```swift
44+
let configuration = SQLiteCloudConfig(connectionString: "sqlitecloud://{username}:{password}@{hostname}:8860/chinook.sqlite")
45+
```
46+
47+
3. Pass each connection string parameter.
6148

62-
return result.stringValue!
63-
} catch {
64-
return "Connection error: \(error)"
65-
}
49+
```swift
50+
let configuration = SQLiteCloudConfig(hostname: {hostname}, username: {username}, password: {password}, port: .default)
6651
```
6752

68-
- 2. Using a connection string: `SQLiteCloudConfig(connectionString: "sqlitecloud://{username}:{password}@{hostname}:{port}/{database})`
53+
## Connect and query
54+
55+
- The following snippet includes variable types, which may be optional for your app.
56+
- Once you've incorporated the following, build and run your app!
6957

7058
```swift
7159
import SQLiteCloud
7260

73-
let configuration: SQLiteCloudConfig? = SQLiteCloudConfig(connectionString: "sqlitecloud://{username}:{password}@{hostname}:8860/chinook.sqlite")
61+
let configuration: SQLiteCloudConfig? = SQLiteCloudConfig(connectionString: "<your-connection-string>")
7462

7563
let sqliteCloud: SQLiteCloud = SQLiteCloud(config: configuration!)
7664

@@ -86,12 +74,10 @@ do {
8674

8775
return result.stringValue!
8876
} catch {
89-
return "Connection error: \(error)"
77+
return "Connection error"
9078
}
9179
```
9280

93-
- The above snippets include variable types, which may be optional for your app.
94-
9581
## Troubleshooting
9682

9783
- 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.

0 commit comments

Comments
 (0)