IOS下SQLite的简单使用
2012-05-31 23:37
495 查看
看着国外网站的教程,写了一个小例子,一个联系人的程序,包括 (姓名、地址、电话)三项内容,通过两个按钮,可以将信息保存或者查询数据库已有的信息。
UI就不说了,比较简单。贴一下关键代码,具体的话还是去看源代码(正想办法传,我这git出点问题)。
git:https://github.com/cokecoffe/ios-demo/tree/master/SQLite%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8
[align=right]Wangkeke 2012-05-31 23:37 发表评论[/align]
UI就不说了,比较简单。贴一下关键代码,具体的话还是去看源代码(正想办法传,我这git出点问题)。
git:https://github.com/cokecoffe/ios-demo/tree/master/SQLite%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8
/*根据路径创建数据库并创建一个表contact(id nametext addresstext phonetext)*/ - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. 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, 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, -1, &statement, 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); } }
[align=right]Wangkeke 2012-05-31 23:37 发表评论[/align]
相关文章推荐
- ios sqlite3的简单使用
- iOS中Sqlite的简单使用方法
- ios sqlite 简单使用
- IOS SQLite简单数据库使用
- ios简单sqlite使用
- iOS- 简单说说iOS移动客户端SQLite3的基本使用
- ios简单sqlite使用
- IOS下SQLite的简单使用
- iOS 下Sqlite的简单使用
- Objective-C ,ios,iphone开发基础:ios数据库(The SQLite Database),使用终端进行简单的数据库操作
- IOS中数据库SQLite的简单使用
- Objective-C ,ios,iphone开发基础:ios数据库(The SQLite Database),使用终端进行简单的数据库操作
- Objective-C ,ios,iphone开发基础:使用第三方库FMDB连接sqlite3 数据库,实现简单的登录
- iOS-简单sqlite使用
- ios简单使用sqlite3
- iOS开发中SQLite简单使用(基础用法:创建表,增、删、改、查)
- 【转】iOS开发—SQLite的简单使用
- iOS- 简单说说iOS移动客户端SQLite3的基本使用
- ios数据库sqlite3简单使用示例一
- Objective-C ,ios,iphone开发基础:使用第三方库FMDB连接sqlite3 数据库,实现简单的登录