Description
Sqift is a fast and lightweight SQL query library for iOS and macOS apps. It provides a simple and convenient way to execute SQL queries and interact with SQLite databases.
Features
- Support for standard SQL syntax and SQLite extensions
- Easy integration with existing projects
- Efficient query execution
- Automatic mapping of query results to Swift types
- Support for transactions
- Thread-safe API for concurrent database access
Installation
To use Sqift in your iOS or macOS project, follow these steps:
- Open your project in Xcode.
- Go to “File” > “Swift Packages” > “Add Package Dependency…”
- Enter the URL for Sqift package: https://github.com/sqift/Sqift.git
- Select the latest version of Sqift and click “Next”.
- Choose the target where you want to add Sqift and click “Finish”.
Getting Started
Once you have installed Sqift in your project, you need to import the Sqift module in your Swift files where you want to use it:
// Import Sqift
import Sqift
Now you can start executing SQL queries:
// Create a database connection
let db = try Connection(.inMemory)
// Execute a query
try db.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)")
// Execute a query with parameter binding
try db.execute("INSERT INTO users (name, age) VALUES (?, ?)", parameters: ["John Doe", 35])
Advanced Usage
Sqift provides advanced features to handle complex scenarios:
Transactions
Sqift offers transaction support to manage multiple queries as a single unit of work. Transactions ensure atomicity and consistency of data modifications. Here’s an example:
// Begin a transaction
try db.transaction { transaction in
// Execute multiple queries within the transaction
try transaction.execute("INSERT INTO users (name, age) VALUES (?, ?)", parameters: ["Alice", 25])
try transaction.execute("INSERT INTO users (name, age) VALUES (?, ?)", parameters: ["Bob", 30])
}
Query Results
To retrieve query results, Sqift provides various methods:
query(_:parameters:)
: Executes a SELECT query and returns a cursor for iterating over the resulting rows.querySingle(_:parameters:)
: Executes a SELECT query and returns a single row.queryValue(_:parameters:)
: Executes a SELECT query and returns a single value.queryDictionary(_:parameters:)
: Executes a SELECT query and returns a dictionary mapping column names to field values for a single row.
Advanced Topics
For more detailed information about Sqift, you can refer to the following topics:
Conclusion
Sqift is a powerful and efficient SQL query library for iOS and macOS apps. It provides a straightforward way to interact with SQLite databases and perform various database operations. With its easy integration and rich feature set, Sqift is a valuable tool for any iOS or macOS developer working with SQL databases.