IOS SQLite a variety of operations on the database (daily collation complete) _ios

Source: Internet
Author: User
Tags sqlite sqlite database stmt sqlite manager uikit

Use SQLite in iOS to process data. If you already know about SQL, you can easily master the operation of the SQLite database. iOS for database operations: Add, delete, find, modify the details are as follows:

First you need to create a database: The database for this program is a micro-database written in a plugin in the Firefox browser.

Firefox find SQLite manager steps:

The first step: Find additional components in the toolbar, click to enter


Step Two: Search SQP, find and download, and restart the browser after installation is complete


Step three: In the tool only optimistic to find SQLite Manager, click to open


The SQLite Manager interface is shown in the picture


Note: SQLite Manager is a micro-type database programming software, so you can only execute one code at a time!!!

• Create a database

--Establishment of database
CREATE TABLE Team

The project directory file is as follows:

Here you need to import a system from the file Libsqlite3.0.tbd

Steps as shown:


• Achieve Engineering

ViewController.h

#import <UIKit/UIKit.h>
#import <sqlite3.h>
@interface viewcontroller:uiviewcontroller
@ Property (strong,nonatomic) UIButton *showbtn;
@property (strong,nonatomic) UIButton *insertbtn;
@property (strong,nonatomic) UIButton *updatebtn;
@property (strong,nonatomic) UIButton *deletebtn;

ViewController.h

#import "ViewController.h" #define PATH [Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask , YES) Lastobject] stringbyappendingpathcomponent:@ "Team.sqlite"] @interface Viewcontroller () @end @implementation Viewcontroller-(void) viewdidload {[Super viewdidload];//Get Sandbox documents path NSLog (@ "%@", [
Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject]);
[Self button]; }-(void) button {Self.showbtn=[uibutton Buttonwithtype:uibuttontypesystem]; Self.showbtn.frame=cgrectmake (100, 50,
200, 50);
[SELF.SHOWBTN settitle:@ "database display" forstate:uicontrolstatenormal];
[Self.showbtn addtarget:self Action: @selector (Showsql) forcontrolevents:uicontroleventtouchupinside];
[Self.view ADDSUBVIEW:SELF.SHOWBTN];
Self.insertbtn=[uibutton Buttonwithtype:uibuttontypesystem];
Self.insertbtn.frame=cgrectmake (100, 100, 200, 50);
[SELF.INSERTBTN settitle:@ "Database Add" forstate:uicontrolstatenormal]; [Self.insertbtn addtarget:self Action: @selector (Insertsql) Forcontrolevents:uicontroleventtouchupinside];
[Self.view ADDSUBVIEW:SELF.INSERTBTN];
Self.updatebtn=[uibutton Buttonwithtype:uibuttontypesystem];
Self.updatebtn.frame=cgrectmake (100, 150, 200, 50);
[SELF.UPDATEBTN settitle:@ "Database modification" forstate:uicontrolstatenormal];
[Self.updatebtn addtarget:self Action: @selector (Updatesql) forcontrolevents:uicontroleventtouchupinside];
[Self.view ADDSUBVIEW:SELF.UPDATEBTN];
Self.deletebtn=[uibutton Buttonwithtype:uibuttontypesystem];
Self.deletebtn.frame=cgrectmake (100, 200, 200, 50);
[self.deletebtn settitle:@ "Database deletion" forstate:uicontrolstatenormal];
[Self.deletebtn addtarget:self Action: @selector (Deletesql) forcontrolevents:uicontroleventtouchupinside];
[Self.view ADDSUBVIEW:SELF.DELETEBTN]; #pragma mark-Displays all information in the datasheet-(void) Showsql {NSLog (@ "Display database Information");//Database sqlite3 *db;//Open database int result=sql based on the specified database file storage path
Ite3_open ([PATH utf8string], &db);
Create execute Command object sqlite3_stmt *stmt; Open Database Success if (RESULT==SQLITE_OK) {NSLog (@ "connected successfully");//execute preprocessing command int res=sQLITE3_PREPARE_V2 (db, "Select *from Team",-1, &stmt, nil); if (RES==SQLITE_OK) {//loop traversing the row information of the datasheet while (Sqlite3_step (stmt) ==sqlite_row) {//Get information for an integral column in a datasheet int stu_id=sqlite3_column_i
NT (stmt, 0);
NSLog (@ "stu_id is%d", stu_id);
Gets the information for the character-type column in the datasheet NSLog (@ "%@", [NSString stringwithformat:@ "%s", Sqlite3_column_text (stmt, 1)]);
NSLog (@ "%@", [NSString stringwithformat:@ "%s", Sqlite3_column_text (stmt, 2)]); 
NSLog (@ "%@", [NSString stringwithformat:@ "%s", Sqlite3_column_text (stmt, 3)]); #pragma mark-Add information-(void) Insertsql {sqlite3 *db; sqlite3_stmt *stmt; Sqlite3_open ([PATH utf8string], &db);
NT RST=SQLITE3_PREPARE_V2 (DB, insert into the team (Stu_name,stu_password,stu_login) VALUES (?,?,?) ",-1, &stmt, nil);
Sqlite3_bind_text (stmt, 1, "Wangwu",-1, nil);
Sqlite3_bind_text (stmt, 2, "123456",-1, nil);
Sqlite3_bind_text (stmt, 3, "WW",-1, nil); Determines whether to increase success if (RST==SQLITE_OK) {if (Sqlite_done==sqlite3_step (stmt)) {NSLog (@ "add OK");}
else{NSLog (@ "add fail");} #pragma mark-Number of revisionsAccording to the library-(void) Updatesql {sqlite3 *db; sqlite3_stmt *stmt; Sqlite3_open ([PATH utf8string], &db);//Method One/* int res = Sqlit E3_PREPARE_V2 (DB, "update team set Stu_name= (?), stu_password= (?), stu_login= (?)
Where stu_id=2 ",-1, &stmt, nil);
Sqlite3_bind_text (stmt, 1, "Xiaoming",-1, nil);
Sqlite3_bind_text (stmt, 2, "123456",-1, nil);
Sqlite3_bind_text (stmt, 3, "XM",-1, nil); *//Method two int rst=sqlite3_prepare_v2 (DB, "Update Team Setstu_name= ' ZL ', Stu_password= ' zl123 ', stu_login= ' Zhangsan ' where
Stu_id=4 ",-1, &stmt, nil); Determines whether or not to modify success if (RST==SQLITE_OK) {if (Sqlite_done = = Sqlite3_step (stmt)) {NSLog (@ "Update OK");}
else{NSLog (@ "update fail");} }-(void) Deletesql {sqlite3 *db; sqlite3_stmt *stmt; Sqlite3_open ([PATH utf8string], &db); int Rst=sqlite3_prepare_v
2 (db, "delete from team where stu_id=9",-1, &stmt, nil); Determines whether to delete success if (RST==SQLITE_OK) {if (Sqlite_done==sqlite3_step (stmt)) {NSLog (@ "delete ok");}
else{NSLog (@ "delete fail");} }-(void) didreceivememorywarning {[Super didreceivememorywarning];//Dispose of any of the can is recreated.} @end 

The above content is small make up to everyone of the daily collection of iOS SQLite database operations, I hope to help you, if you want to know more about the iOS SQLite relevant knowledge, please login Cloud Habitat Community website to learn more, but also thank you all the time has been on the cloud Habitat Community website support!

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.