IOS sqlite and iossqlite

Source: Internet
Author: User
Tags sqlite manager

IOS sqlite and iossqlite
IOS-SQL addition, deletion, search, and modification

IOS database operations: add, delete, search, and modify

First, you need to create a database: the database of this program is a trace database written in the plug-in of Firefox browser.

Steps for Firefox to find SQLite Manager:

Step 1: Find the additional component in the toolbar and click to enter

 

Step 2: Search SQP, find and download, and restart the browser after installation.

Step 3: Find SQLite Manager optimistically in the tool and click Open

SQLite Manager interface

Note: SQLite Manager is a trace-type database programming software, so only one sentence of code can be executed at a time !!!

  • Create a database
-- Create table team (
-- Create a table named team
Stu_id integer primary key autoincrement,
Stu_name varchar (100 ),
Stu_password varchar (100 ),
Stu_login varchar (100)
)
-- Add information
Insert into team (stu_name, stu_password, stu_login) values ('xiaming ', '123', 'xm ')
Insert into team (stu_name, stu_password, stu_login) values ('zhangsan', '123', 'zs ')
-- Query Information
Select * from team
-- Delete information
Delete from team where stu_id = 3

 

The project directory file is as follows:

 

Here we need to import a built-in file libsqlite3.0.tbd

 

 

Procedure

 

  • Implementation Project

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;@end

ViewController. h

# Import "ViewController. h "# define PATH [[NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: @" team. sqlite "] @ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // obtain the NSLog (@" % @", [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) la StObject]); [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: UIButt OnTypeSystem]; self. insertbtn. frame = CGRectMake (100,100,200, 50); [self. insertbtn setTitle: @ "add Database" 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. upd Atebtn 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: @ "delete database" forState: UIControlStateNormal]; [self. deletebtn add Target: self action: @ selector (deleteSql) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: self. deletebtn] ;}# pragma mark-show all information in the data table-(void) showSql {NSLog (@ "show database information"); // database sqlite3 * db; // open the database int result = sqlite3_open ([PATH UTF8String], & db) according to the specified database file storage PATH; // create the execution command object sqlite3_stmt * stmt; // if the database is successfully opened (result = SQLITE_ OK) {NSLog (@ "connection successful"); // execute the preprocessing command int res = sqlite3_prepare_v2 (Db, "select * from team",-1, & stmt, nil); if (res = SQLITE_ OK) {// cyclically traverse the row information of the data table while (sqlite3_step (stmt) = SQLITE_ROW) {// obtain the information of the integer column in the data table int stu_id = sqlite3_column_int (stmt, 0 ); NSLog (@ "stu_id is % d", stu_id); // obtain the NSLog (@ "% @", [NSString stringWithFormat: @ "% s", sqlite3_column_text (stmt, 1)]); NSLog (@ "% @", [NSString stringWithFormat: @ "% s", sqlite3_column_text (stmt, 2)]); NSLog (@ "% @", [NSStri Ng stringWithFormat: @ "% s", sqlite3_column_text (stmt, 3)]) ;}}# pragma mark-add information-(void) insertSql {sqlite3 * db; sqlite3_stmt * stmt; sqlite3_open ([PATH UTF8String], & db); int rst = sqlite3_prepare_v2 (db, "insert into 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); // determine whether the value is added successfully if (rst = SQLITE_ OK) {if (SQLITE_DONE = sqlite3_step (stmt) {NSLog (@ "add OK");} else {NSLog (@ "add fail ");}}} # pragma mark-modify Database-(void) updateSql {sqlite3 * db; sqlite3_stmt * stmt; sqlite3_open ([PATH UTF8String], & db); // method 1/* int Res = sqlite3_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 2 int rst = sqlite3_prepare_v2 (db, "update team setstu_name = 'zl', stu_password = 'zl123 ', stu_login = 'zhangsan' where stu_id = 4",-1, & stmt, nil ); // determine whether the modification is successful 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_v2 (db, "delete from team where stu_id = 9",-1, & stmt, nil); // determine whether the deletion is successful if (rst = SQLITE_ OK) {if (SQLITE_DONE = sqlite3_step (stmt) {NSLog (@ "delete OK");} else {NSLog (@ "delete fail ");}}}
-(Void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated.} @ end

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.