About hzorm
hzorm is a lightweight and easy-to-use ORM (Object Relational Mapping) library for working with SQLite databases in Cocoa and Cocoa Touch applications. It provides a convenient way to interact with a SQLite database by abstracting the underlying SQL operations.
Features:
- Provides a simple and intuitive set of APIs for database operations
- Supports creating, updating, and querying SQLite databases
- Offers automatic mapping between objects and database tables
- Handles relationships between tables using simple and easy-to-understand conventions
- Includes support for basic database operations like inserts, updates, and deletes
Getting Started
To start using hzorm in your project, follow these steps:
- Add hzorm to your project by manually copying the source code files or using a dependency manager like CocoaPods
- Import the hzorm framework into your target
- Set up your SQLite database and define your database schema
Creating a Database
To create a new SQLite database, use the following code:
let database = HZDatabase() // Create a new database instance
try database.createDatabase() // Create the SQLite database file
This will create a new SQLite database file at the default path.
Defining Tables and Mapping Objects
To define a table and map it to an object, follow these steps:
- Define a Swift class for your object, conforming to the HZORMObject protocol
- Add properties to your class, representing the columns in the database table
- Add mappings between the properties and the corresponding database columns
- Set up any relationships between tables using foreign key mappings
Here’s an example:
class Person: HZORMObject {
@HZORMProperty(columnName: "person_id", primaryKey: true)
var personId: Int = 0
@HZORMProperty(columnName: "name")
var name: String = ""
// ... other properties and relationships
}
In the example above, the Person class represents a database table with the “person_id” and “name” columns. The personId property is marked as the primary key column, and the name property maps to the “name” column in the database.
Performing Database Operations
hzorm provides a set of convenient APIs for performing database operations. Here are a few examples:
Inserting Data
To insert data into a table, create an instance of your object and save it:
let person = Person()
person.name = "John Doe"
try database.insertObject(person)
The above code will insert a new record into the “Person” table with the name “John Doe”.
Querying Data
To query data from a table, use the following code:
let results: [Person] = try database.queryObjects(Person.self)
The above code will retrieve all the records from the “Person” table and return an array of Person objects.
Updating Data
To update existing data, modify the properties of an object and call the update method:
let personToUpdate = results.first
personToUpdate?.name = "New Name"
try database.updateObject(personToUpdate)
The above code will update the “name” column for the first record returned in the query.
Deleting Data
To delete data, simply pass the object to be deleted to the delete method:
try database.deleteObject(personToDelete)
The above code will delete the specified object from the database.
Conclusion
hzorm is a powerful and easy-to-use ORM library for SQLite databases in Cocoa and Cocoa Touch applications. With its intuitive APIs, effortless database operations, and support for mapping objects to tables, hzorm simplifies SQLite database management and allows developers to focus on their app’s functionality.