iOS Development learning Note-Data Persistence database (Sqlite.swift)

Source: Internet
Author: User
Tags scalar stmt

Data Persistence SQLite database (Sqlite.swift use )

First, introduction

SQLite is a lightweight embedded database, it occupies a very low resource, in the embedded device, may only need hundreds of K of memory is sufficient. Its processing speed is faster than the two famous databases of MySQL and PostgreSQL. SQLite provides some C function interfaces that you can use to manipulate the database. By using these interfaces to pass some standard SQL statements (in char * types) to the SQLite function, SQLite will manipulate the database for you

A table in a database is a file that is typically placed in a sandbox document directory with a file suffix of db (database)

Sqlite.swift: He's the third-party library for Swift's professional-built, SQLite-based Swift package, GitHub address: Https://github.com/stephencelis/SQLite.swift

Second, the use of Sqlite.swift

The first thing is to import the Sqlite.swift library (import method self-Baidu), import the response of the binary file, but also import the database sqlite[focus], the settings after the completion of the interface should be like the Red line part

Using Sqlite.swift basic Features


: Playground-noun:a place where people can play//use SQLite before you remember to add the project properties build phases option, add SQLite iOS to target Dependencies In//used to introduce the basic use of Sqlite.swift address https://github.com/stephencelis/SQLite.swift//detailed use address: https://github.com/stephencelis /sqlite.swift/blob/master/documentation/index.md#sqliteswift-documentationimport sqlite//** link Database Let db = Try Connection ("Path/to/db.sqlite3")//Connect with database (emphasis)//** define table properties let users = Table ("Users")//Create data table name Let ID = expres Sion<int64> ("id")//define table properties Let name = Expression<string?> ("name") Let email = expression<string> ("email")//** Build Table Try Db.run (users.create {T in//CREATE table only needs to be generated once, multiple runs will error T.column (ID, primarykey:true) t.col Umn (name) t.column (email, unique:true)})//CREATE TABLE "Users" (//"id" INTEGER PRIMARY KEY not null,//" Name "text,//" email "TEXT not NULL unique//)//* insert Let Insert = Users.insert (name <-" Alice ", email <-" [EMAIL&N Bsp;protected]//insert information let rowID = Try Db.run (insert)//return the inserted ID//execute Insert//INSERT INTO "users" ("name", "email") VALUES (' Alice ', ' [EMAIL&NBSP;PR Otected] ') for the user in try Db.prepare (users) {//is used for cyclic use (. Prepare: Indicates that an action is being prepared to perform a table)//Returns the table row print ("ID: \ ( User[id]), name: \ (User[name]), email: \ (User[email]) ")//Id:1, name:optional (" Alice "), email: [email protected ]}//* Choose//select * from "users" let Alice = users.filter (id = = ROWID)//[filter: Filter]//query (return a table is not a row, cannot output directly) can manipulate it like a data table ( and sync to the initial table) if it fails, the number of tables is 0 (db.scalar (alice.count)//0)//* update try Db.run (alice.update (email <-email.replace ("mac.com  ", with:" me.com "))//Perform update//update" users "SET" email "= replace (" email ", ' mac.com ', ' me.com ')//This is still part of the update//WHERE (" id "= 1)//* Delete try Db.run (Alice.delete ())//delete/delete from "Users" WHERE ("id" = 1) Db.scalar (users.count)//0//"Scalar: Quantity"//Statistics rows//SELECT COUNT (*) from "Users"////recommendations, actually used, will be part of the generation Code (such as a built-in table) to another place, to quantify some other data//SQlite.swift also works as a lightweight, swift-friendly wrapper over the C api.//can execute statements directly???? Let stmt = Try Db.prepare ("INSERT into Users" VALUES (?) ") For mail in ["[Email protected]", "[email protected]"] {try Stmt.run (email)}db.totalchanges//3db.chang ES//1db.lastinsertrowid//3for row in Try Db.prepare ("Select ID, e-mail from users") {print ("ID: \ (row[0]), Email: \ (row[1])//use subscript instead of column//id:optional (2), email:optional ("[email protected]")//Id:optional (3), email: Optional ("[email protected]")}db.scalar ("SELECT count (*) from users")//2

Tips: SQLite database can be opened using visualizer Mesasqlite software

Sqlite.swift's use of GitHub introduction (detail): https://github.com/stephencelis/sqlite.swift/blob/master/documentation/index.md- Sqliteswift-documentation

Extended Links: SQLite common statements

iOS Development learning Note-Data Persistence database (Sqlite.swift)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.