SQLite.swift is a type-safe, Swift-friendly wrapper over SQLite database that provides an expressive syntax for interacting with SQLite databases. It allows you to perform database operations using strongly-typed SQL statements, providing safety and convenience.
Installation
To use SQLite.swift in your project, you can install it using CocoaPods, or manually by adding the framework to your project.
Installation using CocoaPods
- Open your project and navigate to the project directory in the terminal.
- Create a new file named
Podfile
by running the commandpod init
. - Open the
Podfile
and add the following line:
pod 'SQLite.swift'
- Save the
Podfile
and run the commandpod install
in the terminal. - Open the newly generated
.xcworkspace
file.
Manual Installation
- Download the latest version of SQLite.swift framework from the GitHub releases page.
- Drag and drop the downloaded framework into your Xcode project.
- Make sure the framework is added to your target.
Getting Started
Before you start using SQLite.swift in your project, you need to import the framework into your Swift files.
import SQLite
To create and connect to a SQLite database, you can use the following code:
let db = try Connection(pathToDatabase)
Replace pathToDatabase
with the path to your SQLite database file.
Executing SQL Statements
To execute an SQL statement, you can use the run
method on the database connection, as shown below:
try db.run(table.create())
The above code creates a new table in the database using the provided SQL statement.
Querying Data
To query data from a table, you can use the prepare
method on the database connection. For example:
let query = table.select(column1, column2).filter(condition)
for row in try db.prepare(query) {
let value1 = try row.get(column1)
let value2 = try row.get(column2)
// Process the retrieved values
}
The above code selects two columns from the table, applies a filter condition, and retrieves the results row by row. You can access the column values using the get
method on the row object.
Updating Data
To update data in a table, you can use the update
method on the database connection, as shown below:
let query = table.update(column1 < value1, column2 < value2)
try db.run(query)
The above code updates rows in the table based on the provided conditions. Replace column1
, value1
, column2
, and value2
with your actual column names and values.
Deleting Data
To delete data from a table, you can use the delete
method on the database connection, as shown below:
let query = table.delete()
try db.run(query)
The above code deletes all rows from the table. If you want to delete specific rows, you can apply conditions to the delete
query.
Conclusion
With SQLite.swift, you can easily work with SQLite databases in your Swift projects. It provides a convenient and type-safe way to execute SQL statements, retrieve data, and perform database operations.
For more details and advanced usage of SQLite.swift, you can refer to the official documentation on GitHub.