Skip to content

Commit 3c29523

Browse files
authored
Merge pull request #77 from sqlitecloud/feat/swift-intro
Added Swift Introduction
2 parents 6681662 + 0109658 commit 3c29523

2 files changed

Lines changed: 101 additions & 0 deletions

File tree

sqlite-cloud/_nav.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ const sidebarNav: SidebarNavStruct = [
163163
{ title: 'Introduction', type: "inner", filePath: "sdk-php-introduction", level: 1 },
164164
{ title: "Methods", filePath: "sdk-php-methods", type: "inner", level: 1 },
165165

166+
{ title: "Swift", type: "inner", level: 0 },
167+
{ title: 'Introduction', type: "inner", filePath: "sdk-swift-introduction", level: 1 },
168+
166169
{ title: "Reference", type: "secondary", icon: "docs-ref" },
167170
{ title: "Server-side Commands", type: "inner", level: 0 },
168171
{ title: "Introduction", filePath: "server-side-commands", type: "inner", level: 1 },
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
title: Swift SDK Introduction
3+
description: Get started with SQLite Cloud using Swift.
4+
category: sdks
5+
status: publish
6+
slug: sdk-swift-introduction
7+
---
8+
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).
10+
11+
## Install
12+
13+
- In `Package.swift`, add the `swift` package to `dependencies`.
14+
- The following example snippet is from a Swift app using the Vapor framework.
15+
16+
```swift
17+
let package = Package(
18+
...
19+
dependencies: [
20+
...
21+
.package(url: "https://github.com/sqlitecloud/swift.git", from: "0.2.1")
22+
],
23+
targets: [
24+
.executableTarget(
25+
...
26+
dependencies: [
27+
...
28+
.product(name: "SQLiteCloud", package: "swift")
29+
],
30+
...
31+
),
32+
]
33+
)
34+
```
35+
36+
## Basic Usage
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}`.
44+
45+
```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")
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+
}
66+
```
67+
68+
- 2. Using a connection string: `SQLiteCloudConfig(connectionString: "sqlitecloud://{username}:{password}@{hostname}:{port}/{database})`
69+
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")
80+
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;"
82+
83+
let result: SQLiteCloudResult = try await sqliteCloud.execute(query: sqlQuery)
84+
85+
try await sqliteCloud.disconnect()
86+
87+
return result.stringValue!
88+
} catch {
89+
return "Connection error: \(error)"
90+
}
91+
```
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)