sqlite3 数据库读取
(void)StartTutorial:(int)idInt{
NSLog(@"start %d",idInt);
//indicator指示器
[activityIndicator startAnimating];
[nextToneArray removeAllObjects];
[rhythmTutorialArray removeAllObjects];
//database coding
NSString *path=[NSString stringWithFormat:@"%@%@",[[NSBundle mainBundle] resourcePath],@"/database_paigu.sqlite3"];
if(sqlite3_open([path UTF8String], &database) != SQLITE_OK) //SQLITE_OK 宏定义为0
{
sqlite3_close(database);
NSAssert(0,@"Failed to open database");
}
////数据库访问
NSString *query =[[NSString alloc] init];
query=@"select next_tone1,next_tone2,delay,times,beat,songId from next where songId=?";
NSLog(@"select next_tone1,next_tone2,delay,times,beat,songId from next where songId=%d",idInt);
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK)
{
sqlite3_bind_int(statement, 1, idInt);
while(sqlite3_step(statement) == SQLITE_ROW)
{
////赋值语句 ,注意要按上面query定义的属性顺序,而且从0开始取
int nextTone1=sqlite3_column_int(statement, 0);
int nextTone2=sqlite3_column_int(statement, 1);
int delay=sqlite3_column_int(statement, 2);
int times=sqlite3_column_int(statement, 3);
int beat=sqlite3_column_int(statement, 4);
int songIdInt=sqlite3_column_int(statement, 5);
int tone1=[self GetToneInt:nextTone1];
int tone2=[self GetToneInt:nextTone2];
NSString *tune=[self GetToneString:nextTone1];
[nextToneArray addObject:[NextToneInTutorial initWithNextToneInt1:tone1 NextToneInt2:tone2
Delay:delay Times:times]];
[rhythmTutorialArray addObject:[NoteClass initWithBeatInt:beat TuneString:tune]];
if (nextTone2>0) {
NSString *tune2=[self GetToneString:nextTone2];
[rhythmTutorialArray addObject:[NoteClass initWithBeatInt:beat TuneString:tune2]];
}
NSLog(@"%d %d %d %d times %d",nextTone1,nextTone2,beat,songIdInt,times);
}
sqlite3_finalize(statement);
}
[activityIndicator stopAnimating];
}