Swift uses Fmdb to manipulate SQLite

Source: Internet
Author: User

We all know about SQLite. Local database, widely used on mobile devices. It's also a natural on the iOS platform. Recently I have to toss a small app to use the SQLite local database, on GitHub search under iOS to SQLite three-party package has a lot of stars Fmdb. But Obiect-c's library, I was using swift, I thought it could not be used, but after tossing the release now Swift under the use of OC Library is very simple. I'll show you how to use OC's Fmdb library under Swift.

1. Download

The first step is naturally to download the Fmdb. Https://github.com/ccgus/fmdb

2. Establish bridging

Unzip the downloaded zip. Open the project folder, locate the Fmdb folder, select all the files in it, and drag them into your Swfit project.

  

At this point Xcode will automatically prompt you if you want to set up a bridge and choose Yes. Xcode will automatically set up the OC to Swift Bridge.

  

Next we found a file called Babylog-bridging-header under the Swift Project and opened the editor. Enter in:#import "FMDB.h"

  

So we fmdb to Swift's Bridge connector.

3. Use

With the bridge joint, we can use the Fmdb happily. Here is a simple example of curd, not much to see the code bar.

  Get database

If the database does not exist, the database table is established and the database object is returned.

////Db.swift//Babylog////Created by Mj.zhou on 15/3/4.//Copyright (c) 2015 Mjstudio. All rights reserved.//Import Foundationvarlogs =[Feedlog] ()classdb{classFunc Getdb ()fmdatabase{let Filemgr=Nsfilemanager.defaultmanager () let Dirpaths=Nssearchpathfordirectoriesindomains (. Documentdirectory,. Userdomainmask,true) Let Docsdir= dirpaths[0] asStringvarDatabasePath = Docsdir.stringbyappendingpathcomponent ("feedlog.db")                if!Filemgr.fileexistsatpath (DatabasePath) {Let db=fmdatabase (Path:databasepath)ifdb = =Nil {println ("Error: \ (Db.lasterrormessage ())")            }                        ifDb.open () {Let sql_stmt="CREATE TABLE IF not EXISTS feedlogs (ID TEXT PRIMARY KEY, COUNT INTEGER, TYPE integer,logtime datetime,logday TEXT, RE MARK TEXT)"                if!db.executestatements (sql_stmt) {println ("Error: \ (Db.lasterrormessage ())")} db.close ()}Else{println ("Error: \ (Db.lasterrormessage ())")}} let Feedlogdb=fmdatabase (Path:databasepath)returnFeedlogdb}}

  Inserting data

class func Insert (log:feedlog) {let                sql="+"VALUES (?,?,?,?,?,?) "          = db.getdb ()        db.open ()        db.executeupdate (SQL, Withargumentsinarray: [Log.id, Log.count,log.type,log.logtime,log.logday,log.remark])        db.close ()    }

  Update data

    class func Update (log:feedlog) {                "update feedlogs SET count=?,type=?,logtime=?,logday=?,remark= ? WHERE id=? "          = db.getdb ()        db.open ()        db.executeupdate (SQL, Withargumentsinarray: [Log.count, Log.type,log.logtime,log.logday,log.remark,log.id])        db.close ()    }

  Delete data

    class func Remove (id:string) {        "DELETE from feedlogs WHERE id =? " "          = db.getdb ()        db.open ()        db.executeupdate (SQL, Withargumentsinarray: [id])        Db.close ()    }

Querying Data

    classFuncSelect(id:string)->feedlog?{Let SQL="SELECT * from feedlogs WHERE ID =?"Let db=Db.getdb () db.open ( ) Let RS=db.executequery (SQL, Withargumentsinarray: [id])varlog:feedlog?=Feedlog () whileRs.next () {log?. Id=rs.stringforcolumn ("ID") Log?. Count=int (Rs.intforcolumn ("COUNT")) Log?. Type=int (Rs.intforcolumn ("TYPE")) Log?. Remark=rs.stringforcolumn ("REMARK") Log?. Logtime=rs.dateforcolumn ("LogTime") Log?. Logday=rs.stringforcolumn ("Logday")} db.close ()returnlog}

Swift uses Fmdb to manipulate SQLite

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.