An Example SQLite based iPhone Application
来源:互联网 发布:淘宝海淘上传身份证 编辑:程序博客网 时间:2024/05/18 04:09
Link:http://www.techotopia.com/index.php/An_Example_SQLite_based_iPhone_Application
1. Creating the Database and Table
- (void)viewDidLoad { 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 = @"Failed to create table"; } sqlite3_close(contactDB); } else { status.text = @"Failed to open/create database"; } } [filemgr release]; [super viewDidLoad];}
2. Implementing the Code to Save Data to the SQLite Database
- (void) saveData{ 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 = @"Contact added"; name.text = @""; address.text = @""; phone.text = @""; } else { status.text = @"Failed to add contact"; } sqlite3_finalize(statement); sqlite3_close(contactDB); }}
3. Implementing Code to Extract Data from the SQLite Database
- (void) findContact{ 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 = @"Match found"; [addressField release]; [phoneField release]; } else { status.text = @"Match not found"; address.text = @""; phone.text = @""; } sqlite3_finalize(statement); } sqlite3_close(contactDB); }}
0 0
- An Example SQLite based iPhone Application
- An Example SQLite based iOS 4 iPhone Application (Xcode 4)
- An IMS Application Example Based on SIP Servlets and VoiceXML
- Launching Other Apps within an iPhone Application
- Launching Other Apps within an iPhone Application
- Using Core Plot in an iPhone Application
- An Example: Executing Linux Shell Script from a Java Application
- An Example of Software Application made by Delphi XE
- An example
- an example
- An application to fetch the release sources from Visual SouceSafe based on an Excel migration plan
- How to Build Android Applications Based on FFmpeg by An Example
- How to Build Android Applications Based on FFmpeg by An Example
- How to Build Android Applications Based on FFmpeg by An Example
- Spring 3 MVC Framework Based Hello World Web Application Example Using Maven, Eclipse IDE And Tomcat
- Using iCloud Storage in an iOS 5 iPhone Application
- 关于105 application.iPhone.Entitlements contains an invalid value
- Launching the App Store from an iPhone application
- gcc编译静态库、动态库
- the c++ programming language
- spring的LoginAction
- 运行地址和加载地址
- 红黑树
- An Example SQLite based iPhone Application
- 《linux性能及调优指南》 3.4 硬盘瓶颈
- 多线程还是多进程的选择及区别
- 理解嵌入式开发中的一些硬件相关的概念
- Smart Card
- LayoutInflater的使用
- CPU卡中T=0通讯协议的分析与实现
- iReport利用Print Repeated Values做分组报表以及对重复值做distinct运算
- Leetcode: Unique Binary Search Trees II