SQLite
Speaking SQLite,It is well known to you as MySQL,It is a relational database management system,But unlike the latter is that,It does not necessarily correspond completely independent server!
Generally speaking,SQLite is in the form of a file exist,Mostly used in embedded applications in storage。SQLite library is loaded by the application code,You can easily direct access to the data in a SQLite file。
SQLite written by C,So you know,To call it a library,Must also C language,What pointers friends ......
To access the SQLite database data - after all, the database,So you only need to access other databases like,You can use SQL in the program。
FMDB
Of course,Although Swift itself can be bridged C language library to use SQLite,But this is too much trouble,In particular, C is not OOP ,You may use up all of a sudden not used,Here we describe the most popular third-party libraries:FMDB (Flying Meat Database)。
It features all of the SQLite library package with OC,And we will use them more convenient。Thanks to high-energy Xcode,We can directly put FMDB automatic bridging the OC codes for the Swift 。
Import code
After downloading the files from Github project FMDB,To it src Directory,All .h and .m File (OC code files) FMDB drag you want to use Xcode project file directory,This time Xcode detects you import the Objective-C code,You will be prompted to create a bridge file,After confirming,Bridging file is automatically created。
Then edit the resulting file bridging,Join in the inside word:
1 |
#import "FMDB.h" |
Such,To import a success。
Link SQLite library
Of course,Only imported FMDB is not enough,It can not directly manipulate SQLite file,We also need to link project FMDB depends SQLite library,If you direct the compiler now,Experience 39 Errors。
- Click on your file tree on the left top of the Xcode project files;
- In the middle column select your build target;
- On the right select "Build Phases" tab;
- In the "Link Binary With Libaraies" option, click the plus sign;
- Search for and add libsqlite3.0.dylib 。
Use FMDB
We can address database to the incoming FMDB,If the corresponding address file does not exist,The FMDB tries to create a file and then open。
You can not address the incoming file,So the database will be created in FMDB temporary location,Once disconnected,File has gone。
1 2 3 4 |
let documents = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false) let fileURL = documents.URLByAppendingPathComponent("test.sqlite") let database = FMDatabase(path: fileURL.path) |
We need to open the database before using,Use the following statement:
1 2 3 4 |
if !database.open() { print("Unable to open database") return } |
If successful,It explained that it had successfully opened the database,Then you can execute the SQL:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
do { try database.executeUpdate("create table test(x text, y text, z text)", values: nil) try database.executeUpdate("insert into test (x, y, z) values (?, ?, ?)", values: ["a", "b", "c"]) try database.executeUpdate("insert into test (x, y, z) values (?, ?, ?)", values: ["e", "f", "g"]) let rs = try database.executeQuery("select x, y, z from test", values: nil) while rs.next() { let x = rs.stringForColumn("x") let y = rs.stringForColumn("y") let z = rs.stringForColumn("z") print("x = \(x); y = \(y); z = \(z)") } } catch let error as NSError { print("failed: \(error.localizedDescription)") } |
Here execute all SQL statements can be,use ? As a placeholder,Then the value to be written into the array of parameters passed back in form as a parameter,Note The placeholder number and length of the array must be equal!
After finished,Close the database:
1 |
database.close() |
>Of course,Actual use,You should create a Model for managing database access,Rather than executing SQL directly in code,This is consistent with the design principles of OOP。 ?
Learn more
http://www.theappguruz.com/blog/use-sqlite-database-swift
Original article written by LogStudio:R0uter's Blog » Use SQLite database in Swift Lane
Reproduced Please keep the source and description link:https://www.logcg.com/archives/1842.html