Native usage of SQLite databases in IOS _ios

Source: Internet
Author: User
Tags sqlite sqlite database

In iOS, SQLite is also supported. There are many third-party libraries that encapsulate sqlite operations, such as the Sqlite.swift in swift language and the Apple website, which encapsulates a framework for us: CoreData.

They are inseparable from the support of the SQLite database.

This article describes how to use the native SQLite APIs in Swift.

Introducing the SQLite API in Xcode
After creating a new Swift project, we need to bring the project into the SQLite dynamic-link library:

1, the project configuration interface, select Build Phases

2, point open link Binary with libraries, click the + number, enter in the window Sqlite3

After completion:

3. Create a bridging file and a new header file under the project directory (h):

4, then import the SQLite library using import:

#import "Sqlite3.h"


5, the final step, in the project configuration interface, select Build Setting, enter Swift in the Search box, select Objective-c bridging Header in the results, and enter the name of the bridge file you just created:

The entire introduction has been completed, you can test whether the introduction of success, in the swift file, input sqlite3 to see if there are sqlite3-related smart tips.

To create (open) and close a database
To create or open a SQLite database, using the Sqlite3_open method, we do not need to manually create a database file, if there is no file, the Sqlite3_open method will automatically create the database file for us, and then open the database.

Database store path let
Sqlitepath = Nshomedirectory (). stringByAppendingPathComponent ("documents/sqlite3.db")
// Open the database, specify the database file path, and if the file does not exist, create the file and then open it, so you do not need to manually create the file let state
= Sqlite3_open (Sqlitepath, &db)
if state = = SQLITE _ok{
 println ("Open Database succeeded")
}else{
 println ("Open database Failed")
}

Here the second parameter of the Sqlite3_open method is a pointer, an action pointer that is returned after the database is opened, and we can do a series of operations on the database by using it. Let's first define it outside to make it easy for us to use.

var db:copaquepointer = nil
override func Viewdidload () {
 super.viewdidload () ...
}

Creating tables and Deleting tables
You can use the Sqlite3_exec method to execute a section of SQL statements, mainly the differences in SQL statements, all the same:

CREATE TABLE let
createtable = ' CREATE table if not EXISTS students (ID integer primary key autoincrement,name
Text,stuid Integer) "Let result
= sqlite3_exec (db, createtable, nil, nil, nil)
if result = = sqlite_ok{
 println (" CREATE Table succeeded ")
}
Delete Table let
removetable = "drop table studets" let
result2 = sqlite3_exec (db, removetable, nil, nil, nil)
if re SULT2 = = sqlite_ok{
 println ("delete table succeeded")
}

Insert Update Delete data
Inserting data uses placeholders, and the question mark is used in SQL statements to represent values. Use Sqlite3_bind_xxx to bind values.

Insert Data let
inserttable = "INSERT into Studets (stuid,name) VALUES (?,?)"
var statement:copaquepointer = nil let
RESULT3 = SQLITE3_PREPARE_V2 (db, Inserttable,-1, &statement, nil)
if result3 = = sqlite_ok{
 //Binding Data
 Sqlite3_bind_int (statement, 1, 1)
 sqlite3_bind_text (statement, 2, " Lijialong ",-1, nil)
 
 //Execute
 if sqlite3_step (statement) = = sqlite_done{
  println (" Data insert succeeded ")
 }
 Sqlite3_finalize (statement)
 }


Updating data also resembles several steps:

Let updatetable = "Update studets set name = ' ss ' WHERE id = 1"
var statement:copaquepointer = nil
sqlite3_prepare_ V2 (DB, Updatetable,-1, &statement, nil)
Sqlite3_step (statement)
sqlite3_finalize (statement)

Delete data:

Let deleterow = "Delete from studets where name= ' Lijialong '"
sqlite3_prepare_v2 (DB, DeleteRow,-1, &statement, ni L)
sqlite3_step (statement)
sqlite3_finalize (statement)

Querying data

Query data
Let query = ' SELECT * from Studets '
///This execution, the data is already in Sattement
sqlite3_prepare_v2 (db, Query,-1, & Amp;statement, nil)
//Cursor down one step, if return Sqlite_row enter while
sqlite3_step (statement) = = sqlite_row{let
 id = Sqlite3_column_int (statement, 0) let
 stuid = Sqlite3_column_int (statement, 2)
}

The above is about iOS in the SQLite database of the native usage of detailed introduction, I hope to help you learn.

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.