Sqlitedb is a handy third-party encapsulation library for manipulating SQLite databases. Specific configuration and use methods can see my other article: Swift-Operations SQLite database (referencing SQLite3 library)
This is the main explanation for the new Sqltable.swift in the latest version.
What is 1,sqltable for?
Sometimes we query the data from the database, in order to map to the entity class object, we need to traverse the query result set, assign values to the entity class objects.
Also, when the entity object attribute is modified, if you want to save it to the database, you need to take the object property out and spell it out as an SQL statement.
This is more troublesome.
The role of sqltable is to make entity classes inherit sqltable, and it will map entity classes to database tables. As long as the operation of entity class objects can be directly data reading, modification, new operations.
The principle of 2,sqltable
See Sqltable.swift source code can see that its internal implementation using Swift's reflection, by traversing all the attributes within the object, so that automatically stitching into the corresponding SQL statements to execute.
The use of 3,sqltable
For example, we have a Contact table:
The code is as follows |
Copy Code |
T_user ( UID Integer PRIMARY key, uname varchar (20), Mobile varchar (20) ) |
(1) First create a contact entity class, Inherit sqltable
The code is as follows |
Copy Code |
Class User:sqltable { var uid =-1 var uname = "" var mobile = ""
Init () { The corresponding database table Super.init (tablename: "T_user") }
Set Primary key (this can be omitted without overwriting if the primary key field name is ID) Override Func PrimaryKey ()-> String { return "UID"; }
Required convenience init (tablename:string) { Self.init () } } |
(2) query all the data
In ascending order by ID (you can follow multiple sorted fields later)
The code is as follows |
Copy Code |
Let Data:[user] = User (). Allrows ("UID ASC") For item in data { Print ("\ (item.uid): \ (item.uname): \ (item.mobile)") } |
(3) Save the data
New if the entity class primary key cannot find a corresponding record in the database. Otherwise it will be modified.
The code is as follows |
Copy Code |
Let User1 = User () User1.uname = "Hangge.com" User1.mobile = "123" If User1.save (). Success { Print ("Data insert succeeded") }
|
4, complete sample code
The code is as follows |
Copy Code |
Import Uikit
Class Viewcontroller:uiviewcontroller { var db:sqlitedb!
Override Func Viewdidload () { Super.viewdidload ()
Get database instance db = Sqlitedb.sharedinstance () Create a table if the table does not yet exist Db.execute ("CREATE table if not exists t_user (UID integer primary key,uname varchar (), mobile varchar (20))") Emptying the database Db.execute ("Delete from T_user")
Insert three piece of data Print (------start inserting data------) Let User1 = User () User1.uname = "John" User1.mobile = "123" If User1.save (). Success { Print ("Data insert succeeded") } Let User2 = User () User2.uname = "Dick" User2.mobile = "456" If User2.save (). Success { Print ("Data insert succeeded") }
Let User3 = User () User3.uname = "Harry" User3.mobile = "110" If User3.save (). Success { Print ("Data insert succeeded") }
All users are queried (sorted by ID) Print ("\ n------start querying all data------") Let Data:[user] = User (). Allrows ("UID ASC") For item in data { Print ("\ (item.uid): \ (item.uname): \ (item.mobile)") }
modifying data Print ("\ n------Modify the second data------") Data[1].mobile = "Hangge.com" If Data[1].save (). Success { Print ("Data modification succeeded") }
Check out all users again (sorted by ID) Print ("\ n------query all data------again") Let Data2:[user] = User (). Allrows ("UID ASC") For item in Data2 { Print ("\ (item.uid): \ (item.uname): \ (item.mobile)") } }
Override Func didreceivememorywarning () { Super.didreceivememorywarning ()
} } |
The console output is as follows: