저는 iPhone 개발을 처음 사용합니다. 내 데이터베이스에 특정 데이터를 삽입하고 을 검색하여 테이블에 표시하고 싶습니다. 테이블 'user'로 Database data.sqlite
을 만들었습니다. 이 테이블에는 id (varchar)와 name (varchar)의 두 가지 값이 있습니다. "사용자 값 ('1', 'xxxx')에 삽입하여 테이블에 3 행의 데이터를 삽입했습니다." 터미널 창에서. "testDb"라는 탐색 기반 응용 프로그램을 만들었습니다. AppDelegate에서 나는이 두 가지 방법의 내용을 표시하려면 rootviewcontrolleriPhone의 SQLite 데이터베이스에 데이터를 입력하는 방법
-(void)initializeTableData{
sqlite3 *db=[DatabaseTestAppDelegate getNewDBConnection];
sqlite3_stmt *statement=nil;
const char *sql="select * from user";
if (sqlite3_prepare_v2(db, sql, -1, &statement, NULL)!=SQLITE_OK)
NSAssert1(0,@"error in preparing staement",sqlite3_errmsg(db));
else {
while(sqlite3_step(statement)==SQLITE_ROW)
[tableData addObject:[NSString stringWithFormat:@"%s",(char*)sqlite3_column_text(statement,1)]];
}
sqlite3_finalize(statement);
}
- (void)viewDidLoad {
[super viewDidLoad];
tableData=[[NSMutableArray alloc]init];
[self addMyObjectIntoDatabase];
[self initializeTableData];
[email protected]"Database test";
}
에서
- (void)createEditableCopyOfDatabaseIfNeeded {
NSLog(@"Creating editable copy of database");
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"data.sqlite"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success) return;
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"data.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
및
+(sqlite3 *) getNewDBConnection{
sqlite3 *newDBconnection;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.sqlite"];
// Open the database. The database was prepared outside the application.
if (sqlite3_open([path UTF8String], &newDBconnection) == SQLITE_OK) {
NSLog(@"Database Successfully Opened :)");
}
else {
NSLog(@"Error in opening database :(");
}
return newDBconnection;
}
이것은 계속을 표시 제대로 작동
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSLog(@"before displaying data");
cell.textLabel.text=[tableData objectAtIndex:indexPath.row];
NSLog(@"after displaying data");
// Configure the cell.
return cell;
}
테이블의 엔트. 하지만 이제는 테이블에 다른 행을 삽입하고 삽입 된 내용을 이전 내용과 함께 표시하려고합니다. 나는 iPhone을 처음 사용하기 때문에 자습서에서 데이터베이스를 읽고 그 내용을 표시하는 방법을 배웠습니다. 다시 삽입하고 다시 표시하는 작업을 어떻게 더 진행합니까?
C API를 직접 사용하는 대신 SQLite를 처리하기 위해 래퍼를 사용하십시오. 그것은 삶을 그렇게 쉽게 만듭니다 * http://cocoaheads.byu.edu/resources/sqlite –
다음은 유용한 튜토리얼입니다. [여기] (http://dblog.com.au/iphone-development-tutorials/iphone-sdk-tutorial-reading-data-from-a-sqlite-database/) 링크를 클릭하십시오. –