Use SQLite database in Swift Lane

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:

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。

In Xcode project to add the library to link SQLite FMDB

In Xcode project to add the library to link SQLite FMDB

  1. Click on your file tree on the left top of the Xcode project files;
  2. In the middle column select your build target;
  3. On the right select "Build Phases" tab;
  4. In the "Link Binary With Libaraies" option, click the plus sign;
  5. 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。

We need to open the database before using,Use the following statement:

If successful,It explained that it had successfully opened the database,Then you can execute the SQL:

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:

>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.sqlite.org

github.com/ccgus/fmdb

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

About the Author

R0uter

The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。

Leave a Reply

Your email address will not be published. Required fields are marked *