SQlite入门
发布时间:2020-12-12 23:55:46 所属栏目:百科 来源:网络整理
导读:#import UIKit/UIKit.h#import sqlite3.h@interface ViewController : UIViewController{ sqlite3 *contactDB; UITextField *name; UITextField *address; UITextField *phone; UILabel *status; NSString *databasePath;} 一 创建数据库 /*根据路径创建数据
#import <UIKit/UIKit.h> #import <sqlite3.h> @interface ViewController : UIViewController { sqlite3 *contactDB; UITextField *name; UITextField *address; UITextField *phone; UILabel *status; NSString *databasePath; } 一 创建数据库
/*根据路径创建数据库并创建一个表contact(id nametext addresstext phonetext)*/ NSString *docsDir; NSArray *dirPaths; // Get the documents directory dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); docsDir = [dirPaths objectAtIndex:0]; // Build the path to the database file databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"contacts.db"]]; NSFileManager *filemgr = [NSFileManager defaultManager]; if ([filemgr fileExistsAtPath:databasePath] == NO) { const char *dbpath = [databasePath UTF8String]; if (sqlite3_open(dbpath,&contactDB)==SQLITE_OK) { char *errMsg; const char *sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,ADDRESS TEXT,PHONE TEXT)"; if (sqlite3_exec(contactDB,sql_stmt,NULL,&errMsg)!=SQLITE_OK) { status.text = @"创建表失败n"; } } else { status.text = @"创建/打开数据库失败"; } } 二 增- (IBAction)SaveToDataBase:(id)sender { sqlite3_stmt *statement; const char *dbpath = [databasePath UTF8String]; if (sqlite3_open(dbpath,&contactDB)==SQLITE_OK) { NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO CONTACTS (name,address,phone) VALUES("%@","%@","%@")",name.text,address.text,phone.text]; const char *insert_stmt = [insertSQL UTF8String]; sqlite3_prepare_v2(contactDB,insert_stmt,-1,&statement,NULL); if (sqlite3_step(statement)==SQLITE_DONE) { status.text = @"已存储到数据库"; name.text = @""; address.text = @""; phone.text = @""; } else { status.text = @"保存失败"; } sqlite3_finalize(statement); sqlite3_close(contactDB); } } 三 查- (IBAction)SearchFromDataBase:(id)sender { const char *dbpath = [databasePath UTF8String]; sqlite3_stmt *statement; if (sqlite3_open(dbpath,&contactDB) == SQLITE_OK) { NSString *querySQL = [NSString stringWithFormat:@"SELECT address,phone from contacts where name="%@"",name.text]; const char *query_stmt = [querySQL UTF8String]; if (sqlite3_prepare_v2(contactDB,query_stmt,NULL) == SQLITE_OK) { if (sqlite3_step(statement) == SQLITE_ROW) { NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement,0)]; address.text = addressField; NSString *phoneField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement,1 )]; phone.text = phoneField; status.text = @"已查到结果"; [addressField release]; [phoneField release]; } else { status.text = @"未查到结果"; address.text = @""; phone.text = @""; } sqlite3_finalize(statement); } sqlite3_close(contactDB); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |