Add, delete, modify, and query IOS sqlite Databases

Source: Internet
Author: User

Add, delete, modify, and query IOS sqlite Databases
1. Introduction: simple encapsulation of the sqlite database operation class BaseDB is used to add, delete, modify, and query sqlite. before using it, import libsqlite3.0.dylib library 2. BaseDB. h.

//// BaseDB. h // SqliteDemo /// Created by Zhao Chao on 14-8-26. // Copyright (c) 2014 Zhao Chao. all rights reserved. // # import
 
  
# Import "sqlite3.h" @ interface BaseDB: NSObject/*** create a table * SQL: executed SQL statement * dataName: database name */-(void) createTable :( NSString *) SQL dataBaseName :( NSString *) dataName;/*** execute the SQL statement to add, modify, and delete the * SQL: executed SQL statement * params: The parameter * dataName: database name */-(BOOL) execSql :( NSString *) SQL parmas :( NSArray *) params dataBaseName :( NSString *) dataName;/*** select data * SQL: query SQL statement * params: query the parameter * dataName: query database name */-(NSMutableArray *) selectSql :( NSString *) SQL parmas :( NSArray *) params dataBaseName :( NSString *) dataName; @ end
 
The created database file is located at/Users/zhaochao/Library/Application Support/iPhone Simulator/7.1/Applications/07D17328-B63C-4D87-9B6C-03AA5CD681EA/Documents/zhaochao. sqlite is the NSString * fileName = [NSHomeDirectory () stringByAppendingFormat: @ "/Documents/% @", name]; directory. The file can be opened directly with the SQLiteManager software, or the sqlitemanager plug-in can be installed in firefox, as shown in
3. BaseDB. m
//// BaseDB. m // SqliteDemo /// Created by Zhao Chao on 14-8-26. // Copyright (c) 2014 Zhao Chao. all rights reserved. // # import "BaseDB. h "@ implementation BaseDB/** get the sandbox directory * name: append directory aa **/-(NSString *) DataBaseName :( NSString *) name {NSString * fileName = [NSHomeDirectory () stringByAppendingFormat: @ "/Documents/% @", name]; return fileName;}/*** select data * SQL: query SQL statement * params: query the parameter * dataName: query database name */-(NSMut AbleArray *) selectSql :( NSString *) SQL parmas :( NSArray *) params dataBaseName :( NSString *) dataName {sqlite3 * sqlite = nil; sqlite3_stmt * stmt = nil; // open the database NSString * fileName = [self DataBaseName: dataName]; int result = sqlite3_open ([fileName UTF8String], & sqlite); if (result! = SQLITE_ OK) {NSLog (@ "failed to open"); return nil;} const char * sqlCh = [SQL UTF8String]; // compile the SQL statement sqlite3_prepare_v2 (sqlite, sqlCh, -1, & stmt, NULL); // bind the parameter for (int I = 0; I
 
  
4. Call format
BaseDB * db = [[BaseDB alloc] init]; // create a table NSString * dbCreate = @ "create table zhaochao (username text primary key, userPasswd test )"; NSString * dbName = @ "zhaochao. sqlite "; // [db createTable: dbCreate dataBaseName: dbName]; // Add data NSString * insertTable = @" insert into zhaochao (username, userPasswd) values (?,?) "; NSArray * insertParmas = @ [@" acasdfaa ", @" bb "]; // [db execSql: insertTable parmas: insertParmas dataBaseName: @" zhaochao. sqlite "]; // modify the data NSString * updateTable = @" update zhaochao set username =? Where username =? "; NSArray * updateParams = @ [@" admin ", @" zhaochao "]; // [db execSql: updateTable parmas: updateParams dataBaseName: @" zhaochao. sqlite "]; // delete data NSString * deleteTable = @" delete from zhaochao where username =? "; NSArray * deleteParams = @ [@" aa "]; // [db execSql: deleteTable parmas: deleteParams dataBaseName: @" zhaochao. sqlite "]; // query data NSString * selectTable = @" select username, userPasswd from zhaochao where userPasswd =? "; NSString * selectParam = @ [@" bb "]; NSArray * result = [db selectSql: selectTable parmas: selectParam dataBaseName: @" zhaochao. sqlite "]; for (int I = 0; I
   
    


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.