-(void)defaultValueAddingIntoDatabase
{
dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
documentDir = [dirPath objectAtIndex:0];
databasePath=[[NSString alloc]initWithString:[documentDir stringByAppendingPathComponent:@"drumloops.db"]];
// NSLog(@"datbase path :%@",databasePath);
filemanager= [NSFileManager defaultManager];
if([filemanager fileExistsAtPath:databasePath] == YES)
{
const char *dbPath=[databasePath UTF8String];
if(sqlite3_open(dbPath,&_myDatabase)==SQLITE_OK)
{
NSLog(@"DATABASE IS OPEN");
}
}
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &_myDatabase) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt="CREATE TABLE IF NOT EXISTS drum(ID INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,speed TEXT,track TEXT,audio TEXT)";
if(sqlite3_exec(_myDatabase, sql_stmt, NULL, NULL, &errMsg)!= SQLITE_OK)
{
// NSLog(@"drum TABLE id failed to create");
}
else
{
NSLog(@"drum table is created");
}
}
sqlite3_stmt *statement;
if(sqlite3_open(dbpath,&_myDatabase)==SQLITE_OK)
{
NSString *insertQuery;
insertQuery=[NSString stringWithFormat:@"Insert into drum (name,speed,track,audio) SELECT 'JAZZ','1','1','JAZZ1' UNION SELECT 'JAZZ','1','2','JAZZ2' UNION SELECT 'JAZZ','1','3','JAZZ3' UNION SELECT 'JAZZ','1','4','JAZZ4' UNION SELECT 'JAZZ','1','5','JAZZ5' UNION SELECT 'JAZZ','1','6','JAZZ6' UNION SELECT 'JAZZ','1','7','JAZZ7' UNION SELECT 'JAZZ','1','8','JAZZ8' UNION SELECT 'JAZZ','1','9','JAZZ9' UNION SELECT 'JAZZ','1','10','JAZZ10' UNION SELECT 'JAZZ','1','11','JAZZ11' UNION SELECT 'JAZZ','1','12','JAZZ12' UNION SELECT 'JAZZ','1','13','JAZZ13' UNION SELECT 'JAZZ','1','14','JAZZ14' UNION SELECT 'ROCK','1','1','ROCK1' UNION SELECT 'ROCK','1','2','ROCK2' UNION SELECT 'ROCK','1','3','ROCK3' UNION SELECT 'ROCK','1','4','ROCK4' UNION SELECT 'ROCK','1','5','ROCK5' UNION SELECT 'ROCK','1','6','ROCK6' UNION SELECT 'ROCK','1','7','ROCK7' UNION SELECT 'ROCK','1','8','ROCK8' UNION SELECT 'ROCK','1','9','ROCK9' UNION SELECT 'ROCK','1','10','ROCK10' UNION SELECT 'ROCK','1','11','ROCK11' UNION SELECT 'ROCK','1','12','ROCK12' UNION SELECT 'ROCK','1','13','ROCK13' UNION SELECT 'ROCK','1','14','ROCK14' UNION SELECT 'ROCK','1','15','ROCK15' UNION SELECT 'ROCK','1','16','ROCK16' UNION SELECT 'ROCK','1','17','ROCK17' UNION SELECT 'ROCK','1','18','ROCK18' UNION SELECT 'ROCK','1','19','ROCK19' UNION SELECT 'ROCK','1','20','ROCK20' UNION SELECT 'ROCK','1','21','ROCK21' UNION SELECT 'ROCK','1','22','ROCK22' UNION SELECT 'POP','1','1','POP1' UNION SELECT 'POP','1','2','POP2' UNION SELECT 'POP','1','3','POP3' UNION SELECT 'POP','1','4','POP4' UNION SELECT 'POP','1','5','POP5' UNION SELECT 'POP','1','6','POP6' UNION SELECT 'POP','1','7','POP7' UNION SELECT 'POP','1','8','POP8' UNION SELECT 'POP','1','9','POP9' UNION SELECT 'POP','1','10','POP10' UNION SELECT 'POP','1','11','POP11' UNION SELECT 'POP','1','12','POP12' UNION SELECT 'POP','1','13','POP13' UNION SELECT 'POP','1','14','POP14' UNION SELECT 'POP','1','15','POP15' UNION SELECT 'POP','1','16','POP16' UNION SELECT 'POP','1','17','POP17' UNION SELECT 'POP','1','18','POP18' UNION SELECT 'HIPHOP','1','1','HIPHOP1' UNION SELECT 'HIPHOP','1','2','HIPHOP2' UNION SELECT 'HIPHOP','1','3','HIPHOP3' UNION SELECT 'HIPHOP','1','4','HIPHOP4' UNION SELECT 'HIPHOP','1','5','HIPHOP5' UNION SELECT 'HIPHOP','1','6','HIPHOP6' UNION SELECT 'HIPHOP','1','7','HIPHOP7' UNION SELECT 'HIPHOP','1','8','HIPHOP8' UNION SELECT 'HIPHOP','1','9','HIPHOP9' UNION SELECT 'HIPHOP','1','10','HIPHOP10' UNION SELECT 'HIPHOP','1','11','HIPHOP11' UNION SELECT 'HIPHOP','1','12','HIPHOP12' UNION SELECT 'HIPHOP','1','13','HIPHOP13' UNION SELECT 'HIPHOP','1','14','HIPHOP14' UNION SELECT 'HIPHOP','1','15','HIPHOP15' UNION SELECT 'HIPHOP','1','16','HIPHOP16' UNION SELECT 'HIPHOP','1','17','HIPHOP17' UNION SELECT 'HIPHOP','1','18','HIPHOP18'"];
const char *insert_stmt=[insertQuery UTF8String];
BOOL sucess=sqlite3_prepare_v2(_myDatabase, insert_stmt, -1, &statement, NULL);
if(sucess==SQLITE_OK)
{
// NSLog(@"prepared");
}
if(sqlite3_step(statement)==SQLITE_DONE)
{
// NSLog(@"inserted succesfully");
[[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"HasLaunchedOnce"]; //first time app set Value Yes than always be YES
[[NSUserDefaults standardUserDefaults] synchronize];
}
else
{
NSLog(@"not insertd into drum");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error !" message:@"Relaunch application" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
sqlite3_finalize(statement);
sqlite3_close(_myDatabase);
}
}
when application launch first time the above method call and fired the database query and it create new database and table
the database value does not insert into database in sqlite the latest value of database doesnot appear into tableview the above method for insert into database
and the belove method for fetch from database
dirPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentDir=dirPath[0];
// NSLog(@"doc dir path is %@",documentDir);
databasePath=[[NSString alloc]initWithString:[documentDir stringByAppendingPathComponent:@"drumloops.db"]];
// NSLog(@"datbase path :%@",databasePath);
filemanager= [NSFileManager defaultManager];
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &_myDatabase) == SQLITE_OK)
{
// NSString *querySQL = [NSString stringWithFormat:@"SELECT audio FROM drum where name='%@' ",selectedButtonName];
NSString *querySQL = [NSString stringWithFormat:@"SELECT audio FROM drum WHERE name='%@' AND speed='%lu'",selectedButtonName, (unsigned long)selectedSpeed];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(_myDatabase, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *audio = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 0)];
// NSLog(@"audio %@",audio);
[self.fetchAudio addObject:audio];
}
sqlite3_finalize(statement);
sqlite3_close(_myDatabase);
// NSLog(@"speed :%@",fetchAudio);
sortedAudioArray = [fetchAudio sortedArrayUsingComparator:^(id a, id b) {
return [a compare:b options:NSNumericSearch];
}];
// NSLog(@"sortedAudioArray :%@",sortedAudioArray);
}
}
the above method doesnot fetch the value thank you...
Aucun commentaire:
Enregistrer un commentaire