Right now I am inserting into the db on the basis of count. If count is greater than zero than "SELECT" will run and my data will be shown, else "INSERT" will take place. Right now my db is empty so the condition goes to "INSERT" section and in that I am getting "Database is Locked:ERROR". i have gone through some examples and it said like we have to finalize the curent query before starting the new one. I have done that thing too but still my problem is same. Posting my code here :
if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:
@"SELECT count(*) FROM tables WHERE table_id='%d'",
table_id];
NSLog(@"fetch query is.... %@",querySQL);
const char *query_stmt = [querySQL UTF8String];
sqlite3_stmt *countstmt;
if(sqlite3_prepare_v2(_contactDB, query_stmt, -1, &countstmt, NULL) == SQLITE_OK)
{
NSLog(@"FEtch");
sqlite3_step(countstmt);
rows = sqlite3_column_int(countstmt, 0);
NSLog(@"no of rows is %d",rows);
}
sqlite3_finalize(statement);
}
if(rows>0)
{
NSLog(@"NO of rows is : %d",rows);
//if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
// {
NSString *querySQQL = [NSString stringWithFormat:
@"SELECT * FROM tables WHERE table_id='%d'",
table_id];
NSLog(@"fetch query is.... %@",querySQQL);
const char *query_stmmt = [querySQQL UTF8String];
if (sqlite3_prepare_v2(_contactDB,
query_stmmt, -1, &statement, NULL) == SQLITE_OK)
{
NSLog(@"yes...");
}
while (sqlite3_step(statement) == SQLITE_ROW)
{
//int i=0;
NSLog(@"yesssss...");
// NSInteger count = sqlite3_column_int(statement, 2);
//NSLog(@"Rowcount is %d",(long)count);
table_data= [[NSString alloc]
initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
status= [[NSString alloc]
initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
row_id= [[NSString alloc]
initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
table_idd= [[NSString alloc]
initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)];
//searchResults = [NSMutableArray arrayWithObject:namee];
NSString *finalarrayy;
//for(loop=1;loop<=z;loop++)
//{
finalarrayy=[NSString stringWithFormat:@"%@,%@,%@,%@",row_id,table_data,status,table_idd];
[stringArray addObject:table_data];
NSLog(@"Array....is...%@",stringArray);
}
[defaults setObject:stringArray forKey:@"stringarray"];
[defaults synchronize];
//}
sqlite3_finalize(statement);
}
else{
NSLog(@"INsert");
//if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
// {
for(i=mn; i<=o ; i++)
{
for(j=1;j<=19;j++)
{
k=i*j;
NSString *val=[NSString stringWithFormat:@"%d %@ %d %@ %d",i,@"x",j,@"=",k];
//tab_data.value=val;
[stringArray addObject:val];
NSString *insertSQLL = [NSString stringWithFormat:
@"INSERT INTO tables(table_data,status,table_id) VALUES ('%@', '%@', '%d')",
val,@"NO",table_id];
NSLog(@"sql stm. is ..%@",insertSQLL);
const char *insert_stmtt = [insertSQLL UTF8String];
sqlite3_prepare_v2(_contactDB, insert_stmtt,
-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"yes");
}
else
{
NSLog(@"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(_contactDB));
}
}
}
// }
sqlite3_finalize(statement);
sqlite3_close(_contactDB);
}
PLease tell me what is wrong with my code.
Aucun commentaire:
Enregistrer un commentaire