You can use this library in Swift for SQLite https://github.com/pmurphyjam/SQLiteDemo
SQLiteDemo
SQLite Demo using Swift with SQLDataAccess class written in Swift
Adding to the project
You only need three files to add to your project * SQLDataAccess.swift * DataConstants.swift * Bridges-Header.h Bridging-Header must be installed in your Xcode project 'Objective-C Bridging Header' in the section 'Swift Compiler - General'
Examples of using
Just follow the code in ViewController.swift to learn how to write simple SQL using SQLDataAccess.swift First you need to open the SQLite database, which deals with
```swift let db = SQLDataAccess.shared db.setDBName(name:"SQLite.db") let opened = db.openConnection(copyFile:true) ```
If openConnection succeeded, now you can do a simple insertion into the AppInfo table
```swift //Insert into Table AppInfo let status = db.executeStatement("insert into AppInfo (name,value,descrip,date) values(?,?,?,?)", "SQLiteDemo","1.0.2","unencrypted",Date()) if(status) { //Read Table AppInfo into an Array of Dictionaries let results = db.getRecordsForQuery("select * from AppInfo ") NSLog("Results = \(results)") } ```
See how it was!
The first term in db.executeStatement is your SQL as a String, all of the following terms are a list of variational arguments of type Any and are your parameters in the array. All of these terms are separated by commas in your list of SQL arguments. You can enter strings, integers, dates and flags immediately after the sequel is approved, since all these terms are considered parameters for the sequel. An array of variational arguments just makes it convenient to enter your entire sequel with just one call to executeStatement or getRecordsForQuery. If you do not have any parameters, do not enter anything after your SQL.
The result array is an array of dictionaries, where "key" is the name of the table column and "value is your data retrieved from SQLite." You can easily iterate over this array using the for loop or print it directly or assign these dictionary elements to custom data classes that you use in View controllers to consume the model.
```swift for dic in results as! [[String:AnyObject]] { print("result = \(dic)") } ```
SQLDataAccess will store text, double, float, blob, Date, integer and long long integers. For blobs you can store binary, varbinary, blob.
For text, you can save char, character, clob, national distinguished character, native character, nchar, nvarchar, varchar, variant, variable character, text.
In the "Dates" field you can store the date and time, time, time stamp, date.
For integers, you can store bigint, bit, bool, boolean, int2, int8, integer, mediumint, smallint, tinyint, int.
For doubles, you can store decimal, double precision, float, numeric, real, double. The double has the greatest accuracy.
You can even store Nulls like Null.
ViewController.swift runs a more sophisticated example, showing how to insert a dictionary as a "Blob". In addition, SQLDataAccess understands the native Swift Date (), so you can insert these objects without conversion and convert them to text and save them, and when extracted, convert them back from text to Date.
Of course, the real power of SQLite is the ability of a transaction. Here you can literally queue 400 SQL statements with parameters and insert them all at once, which is very effective, since it is so fast. ViewController.swift also shows you an example of how to do this. All you really do is create an array of dictionaries called "sqlAndParams", this array stores dictionaries with two "SQL" keys for a statement or query for String and "PARAMS" syntax, which is just an array of native objects that SQLite understands for this request. "sqlParams", , "sqlAndParams". , .
```swift let status = db.executeTransaction(sqlAndParams) if(status) { //Read Table AppInfo into an Array of Dictionaries for the above Transactions let results = db.getRecordsForQuery("select * from AppInfo ") NSLog("Results = \(results)") } ```
, executeStatement getRecordsForQuery String SQL , .
```swift let sql : String = "insert into AppInfo (name,value,descrip) values(?,?,?)" let params : Array = ["SQLiteDemo","1.0.0","unencrypted"] let status = db.executeStatement(sql, withParameters: params) if(status) { ```
Objective-C SQLDataAccess, Objective-C Swift. , SQLDataAccess SQLCipher, , , , , Objective-C SQLDataAccess.
SQLDataAccess CoreData, SQLite CoreData, CoreData.