SDDatabase is a simple yet powerful wrapper over the famous FMDB. Provides fast and easy access to sqlite database operations in iOS eliminating all the boilerplate code. Written with ❤️ in Swift.
- Easy to use, provides direct methods for
create
,insert
,update
,delete
,select
, and others... No need to use raw queries for the above operations - No worries about multi threading, as each
SDDatabase
instance has its ownFMDatabaseQueue
and all the wrapper methods are called on this queue. Create one instance in a singleton and use it throughout the application. - Transaction support.
- Ability to deal with encrypted database files, uses SQLCipher
- No need to deal with complex resultSet objects,
SDDatabase
returns results in and array of dictionaries which can be iterated over. i.e.[[String : Any]]
- All the methods are fully documented, including their example usage as well. 😎
Choose SDDatabase
for your next project which uses SQLite, or migrate over your existing projects—you'll be happy you did!
SDDatabase is dependent on the FMDB/SQLCipher subspec of FMDB. The FMDB/SQLCipher subspec declares SQLCipher as a dependency, allowing FMDB to be compiled with the -DSQLITE_HAS_CODEC
flag.
SDDatabase is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SDDatabase'
let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let databasePath = documentDirectoryPath.appending("/testdb.sqlite")
guard let db = Database(withPath: databasePath) else {
print("DB could not be opened")
return
}
If the database file is encrypted, you can use the init with key method
Database(withPath: self.dbPath(), key: "testKey")
You can control whether the library should log any errors or other logs to the console using the property loggingEnabled
/* Set this to false if you do not want database to log to the console */
db.loggingEnabled = true
let studentSchema = " ("
+ "roll_number" + " INT PRIMARY KEY, "
+ "name" + " text"
+ ") "
let creationSuccess = db.create(table: "student", withSchema: studentSchema)
var studentValues = [String:Any]()
studentValues["roll_number"] = 1
studentValues["name"] = "Sagar"
let insertionSuccess = db.insert(intoTable: "student", values: studentValues)
guard let records = db.select(fromTable: "student", columns: ["roll_number", "name"], whereClause: "name = ?", whereValues: ["Sagar"]) else {
return
}
for record in records {
let rollNumber = record["roll_number"] as! Int
let name = record["name"] as! String
print("Roll number : \(rollNumber), name is \(name)")
}
You can also pass offset and limit to the select method, Refer the documentation for more detailed usage
let updateSuccess = db.update(table: "student", set: ["name" : "Other"], whereClause: "name = ?", whereValues: ["Sagar"])
let deletionSuccess = db.delete(fromtable: "student", where: "roll_number = ?", whereValues: [1])
let dropSuccess = db.drop(table: "student")
The class provides executeQuery()
and executeUpdate()
methods for executing raw SQL statements. Refer to the documentation for detailed usage.
The class provides beginTransaction()
, commitTransaction
, rollback()
and isInTransaction()
for handling transaction management. Refer to the documentation for detailed usage.
To run the example project, clone the repo, and run pod install
from the Example directory first. The example project includes the usage of all the methods provided by the library.
Sagar Dagdu, shags032@gmail.com
SDDatabase is available under the MIT license. See the LICENSE file for more info.
All contributions are welcome. Please fork the project to add functionalities and submit a pull request to merge them in next releases.