Swift SDK Introduction

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.

Install

  • In Package.swift, add the swift package to dependencies.
let package = Package(
  ...,
  dependencies: [
    ...,
    .package(url: "https://github.com/sqlitecloud/swift.git", from: "0.2.1")
  ],
  ...
)

3 ways to configure your database connection

  1. RECOMMENDED: Use the apikey connection string.
  • In your SQLite Cloud account dashboard, click on Show connection strings, copy the Connection String, and replace <your-connection-string> below.
let configuration = SQLiteCloudConfig(connectionString: "<your-connection-string>")
  • You can modify the connection string to include the name of the database to query.
let configuration = SQLiteCloudConfig(connectionString: "sqlitecloud://{hostname}:8860/{database}?apikey={apikey}")
  1. Use a parameterized connection string.

    • In your SQLite Cloud account dashboard, click on Show connection strings, copy the Deployment string, and replace {hostname} below.
    • In your dashboard left nav, select Settings, then Users. Copy your username and replace {username}.
    • In your User’s row, click the down chevron, then Edit. Enter a Password and Save. Replace {password}.
    • Here, the provided port (8860) and database (chinook.sqlite) will query the sample dataset that comes pre-loaded with SQLite Cloud. Replace to query your own datasets.
let configuration = SQLiteCloudConfig(connectionString: "sqlitecloud://{username}:{password}@{hostname}:8860/chinook.sqlite")
  1. Pass each connection string parameter explicitly.
let configuration = SQLiteCloudConfig(hostname: {hostname}, username: {username}, password: {password}, port: .default)

Connect and query

  • The following snippet includes variable types, which may be optional for your app.
  • NOTE: USE DATABASE chinook.sqlite; is only necessary in the query if your configuration does not specify the name of the database to query.
  • Once you’ve incorporated the following, build and run your app!
import SQLiteCloud

let configuration: SQLiteCloudConfig? = SQLiteCloudConfig(connectionString: "<your-connection-string>")

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

do {
    try await sqliteCloud.connect()
    debugPrint("connected")

    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;"

    let result: SQLiteCloudResult = try await sqliteCloud.execute(query: sqlQuery)

    try await sqliteCloud.disconnect()

    return result.stringValue!
} catch {
    return "Connection error"
}

Troubleshooting

  • 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.
    • Confirm https://github.com/sqlitecloud/swift package is listed in Package.resolved.