<p>sqlite3 database;//sqlite3 오픈및 생성if (sqlite3_open([[self dataFilePath] UTF8String], &database) != SQLITE_OK) { sqlite3_close(database); NSAssert(0, @"Failed to open database");}else { char *errorMsg; NSString *createSQL1 = @"CREATE TABLE IF NOT EXISTS LOGS (ID INTEGER PRIMARY KEY AUTOINCREMENT, PHONENUMBER_DATA TEXT, NOW_DATE DATE, CALL_TIME TEXT);"; if (sqlite3_exec (database, [createSQL1 UTF8String], NULL, NULL, &errorMsg) != SQLITE_OK) { sqlite3_close(database); NSLog(@"Error creating table: %s", errorMsg); } NSString *createSQL2 = @"CREATE TABLE IF NOT EXISTS ADDRESS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME_DATA TEXT, PHONENUMBER_DATA TEXT, MEMO_DATA TEXT);"; if (sqlite3_exec (database, [createSQL2 UTF8String], NULL, NULL, &errorMsg) != SQLITE_OK) { sqlite3_close(database); NSLog(@"Error creating table: %s", errorMsg); }}/* 여기서 생략된 -(NSString *)dataFilePath는 sqlite3파일이 있는 주소값을 리턴해주는 함수 입니다. 지금 이 소스는 제가 실제로 썼던 소스라 두개의 테이블을 만들게 되어 있어요. ID integer primary key autoincrement는 자동으로 생성 되며 키값이 되구요, 그외 나머지는 Text, Date, Integer 등등 타입을 설정 할수 있어요 */// 값을 받아 오기.NSMutableArray *mutableLogData = [NSMutableArray array];NSString *query = @"SELECT ID ,PHONENUMBER_DATA, NOW_DATE, CALL_TIME FROM LOGS ORDER BY ID DESC";sqlite3_stmt *statement;if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK) { while (sqlite3_step(statement) == SQLITE_ROW) { int idData = sqlite3_column_int(statement, 0); char *numberData = (char *)sqlite3_column_text(statement, 1); double dateData = sqlite3_column_double(statement, 2); char *callData = (char *)sqlite3_column_text(statement, 3); if (numberData != nil && dateData != 0 && idData != 0){ NSNumber *idValue = [NSNumber numberWithInt:idData]; NSString *numberValue = [[NSString alloc] initWithUTF8String:numberData]; NSDate *dateValue = [NSDate dateWithTimeIntervalSince1970:dateData]; NSString *callTimeValue = [[NSString alloc] initWithUTF8String:callData]; NSArray *dataArray = [NSArray arrayWithObjects:idValue, numberValue, dateValue, callTimeValue, nil]; [mutableLogData addObject:dataArray]; [numberValue release]; [callTimeValue release]; } }}sqlite3_finalize(statement);/* logs라는 테이블의 id, phonenumber_data, now_date, call_time을 id의 역순으로 가져 오는 부분 입니다. 아주아주 쉬운 코드니 바로 아실듯 해요 ㅠ_ㅠ 여기서는 제가 모든줄을 받아 오기 때문에 while문을 돌려 Array에 넣어주고 있어요 */// 특정 줄의 삭제char *errorMsg;char *delete = "DELETE FROM ADDRESS WHERE ID = ?";sqlite3_stmt *stmt;if( sqlite3_prepare_v2(database, delete, -1, &stmt, nil) == SQLITE_OK) { sqlite3_bind_int(stmt, 1, [idData intValue]);}if (sqlite3_step(stmt) != SQLITE_DONE) { NSLog(@"Error deleting table: %s", errorMsg);}sqlite3_finalize(stmt);/* id값을 받아 그것과 일치하는 address테이블의 줄을 삭제 해주는 코드 입니다. id는 당연 integer값이니 int형으로 넣구요. */// 테이블에 값 입력하기char *errorMsg;char *update = "INSERT INTO LOGS (PHONENUMBER_DATA, NOW_DATE, CALL_TIME) VALUES (?,?,?);";if (sqlite3_open([[self dataFilePath] UTF8String], &database) != SQLITE_OK) { sqlite3_close(database); NSAssert(0, @"Failed to open database");}sqlite3_stmt *stmt;if( sqlite3_prepare_v2(database, update, -1, &stmt, nil) == SQLITE_OK) { sqlite3_bind_text(stmt, 1, [callNum UTF8String], -1, NULL); sqlite3_bind_double(stmt, 2, [nowDate timeIntervalSince1970]); sqlite3_bind_text(stmt, 3, [callTime UTF8String], -1, NULL);};if( sqlite3_step(stmt) != SQLITE_DONE) NSLog(@"Error updating table: %s", errorMsg);sqlite3_finalize(stmt);/* logs 테이블에 값을 넣는 코드 입니다. 넣으려고 하는 값은 ?를 이용하여 따로 넣어 줄수 있어요. 조금 아래를 보시면 bind를 이용해 값을 넣는 부분이 있어요 */// 원래 있던 값 수정하기char *errorMsg;char *update = "UPDATE ADDRESS SET NAME_DATA=? , PHONENUMBER_DATA=? , MEMO_DATA=? WHERE ID=?;";sqlite3_stmt *stmt;if( sqlite3_prepare_v2(database, update, -1, &stmt, nil) == SQLITE_OK) { sqlite3_bind_text(stmt, 1, [name UTF8String], -1, NULL); sqlite3_bind_text(stmt, 2, [phoneNum UTF8String], -1, NULL); sqlite3_bind_text(stmt, 3, [memo UTF8String], -1, NULL); sqlite3_bind_int(stmt, 4, idNum);}if( sqlite3_step(stmt) != SQLITE_DONE)NSLog(@"Error updating table: %s", errorMsg);sqlite3_finalize(stmt);/* address테이블의 id를 같은 값을 찾아 <br><span id="callbacknestceruleanbtistorycom68666" style="width:1px; height:1px; float:right"><embed allowscriptaccess="always" id="bootstrapperceruleanbtistorycom68666" src="http://ceruleanb.tistory.com/plugin/CallBack_bootstrapperSrc?nil_profile=tistory&nil_type=copied_post" width="1" height="1" wmode="transparent" type="application/x-shockwave-flash" enablecontextmenu="false" flashvars="&callbackId=ceruleanbtistorycom68666&host=http://ceruleanb.tistory.com&embedCodeSrc=http%3A%2F%2Fceruleanb.tistory.com%2Fplugin%2FCallBack_bootstrapper%3F%26src%3Dhttp%3A%2F%2Fs1.daumcdn.net%2Fcfs.tistory%2Fv%2F0%2Fblog%2Fplugins%2FCallBack%2Fcallback%26id%3D6%26callbackId%3Dceruleanbtistorycom68666%26destDocId%3Dcallbacknestceruleanbtistorycom68666%26host%3Dhttp%3A%2F%2Fceruleanb.tistory.com%26float%3Dleft" swliveconnect="true"></span> 그 줄의 name_data, phonenumber_data, memo_data를 바꾸는 코드입니다. 위랑 같이 ?를 이용해 따로 넣으실수 있어요 */</p>
'영삼이의 IT정보' 카테고리의 다른 글
| 하디디스크 복구 장비 (0) | 2012.10.09 |
|---|---|
| Remove @Override annotation (0) | 2012.06.26 |
| 테이블뷰 선택된 셀 체크하기 - UITableViewCellAccessoryCheckmark (0) | 2012.06.21 |
| iOS 5에서 UIAlertView에 추가된 것 (0) | 2012.06.20 |
| 문자열 (NSString) 비교하기 (0) | 2012.06.20 |