Related
I have table in sqlite I want to get data where userNAme and organizatiocode. problem is that it does not show any rows. If I do not pass variable and select all data then it returns rows.
Here is my code
+(void)getInitialData:(NSString *)dbPath {
MultipleDetailViewsWithNavigatorAppDelegate *appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
NSLog(#"User NAme is %#",appDelegate.userName);
// const char *sql = "select * from library";
const char *sql = "select * from library where userName=?";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
//sqlite3_bind_text(selectStmt, 1, [appDelegate.userName UTF8String], -1, SQLITE_TRANSIENT);
//sqlite3_bind_text(selectStmt, 1, [appDelegate.organizationCode UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(selectStmt , 1, [appDelegate.userName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(selectStmt , 2, [appDelegate.organizationCode UTF8String], -1, SQLITE_TRANSIENT);
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:primaryKey];
coffeeObj.userID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
coffeeObj.contentAddedDateTime = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,2)];
coffeeObj.contentType = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,3)];
coffeeObj.contentTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,4)];
coffeeObj.contentSource = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,5)];
coffeeObj.contentDescription = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,6)];
coffeeObj.categoryTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,7)];
coffeeObj.subCategoryTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,8)];
coffeeObj.organizationCode = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,9)];
coffeeObj.userName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,10)];
int count=[appDelegate.libraryArrayLocal count];
NSLog(#"count is beofore getting values %d",count);
[appDelegate.libraryArrayLocal addObject:coffeeObj];
int countone=[appDelegate.libraryArrayLocal count];
NSLog(#"count is after getting values %d",countone);
[coffeeObj release];
}
}
}
else
sqlite3_close(database);
}
}
Try to use:
NSString *query = [NSString stringWithFormat:#"select * from library where userName=%#",appDelegate.userName];
const char *sql = [query UTF8String];
Instead of:
const char *sql = "select * from library where userName=?";
also try without binding,
if(sqlite3_prepare_v2(database, sql, -1, &compiledStatement, NULL) == SQLITE_OK)
{
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
//do your stuff here
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
This question already has answers here:
sqlite3_prepare_v2 != SQLITE_OK
(2 answers)
Closed 9 years ago.
Anybody help me please.
I can't insert the data to sqlite.
the output is "News added failed".
Note :
- The database is already created.
- News is an object
I just think that the problem is in this function.
Could you tell me whats wrong with my code?
Thanks.
- (void)addNews:(NSString *)content_caption content_date:(NSString *)content_date content_desk:(NSString *)content_desk content_id:(NSString *)content_id content_img:(NSString *)content_img content_type:(NSString *)content_type content_web_url:(NSString *)content_web_url{
char *error;
if (sqlite3_open([databasePath UTF8String], &database)==SQLITE_OK) {
NSString *inserStmt = [NSString stringWithFormat:#"INSERT INTO dBase(CONTENT_CAPTION,CONTENT_DATE, CONTENT_DESK, CONTENT_ID, CONTENT_IMG, CONTENT_TYPE, CONTENT_WEB_URL) values (\'%#\',\'%#\',\'%#\',\'%#\',\'%#\',\'%#\',\'%#\')",content_caption, content_date, content_desk, content_id, content_img, content_type ,content_web_url];
const char *insert_stmt = [inserStmt UTF8String];
if (sqlite3_exec(database, insert_stmt, NULL, NULL, &error)==SQLITE_OK) {
NSLog(#"News added");
News *news = [[News alloc]init];
[news setContent_caption:content_caption];
[news setContent_date:content_date];
[news setContent_desk:content_desk];
[news setContent_id:content_id];
[news setContent_img:content_img];
[news setContent_type:content_type];
[news setContent_web_url:content_web_url];
[arrayOfNews addObject:news];
}else{
NSLog(#"News added failed");
}
sqlite3_close(database);
}
}
try with this
#define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
const char *insertSql;
insertSql = nil;
insertSql = "INSERT INTO dBase(CONTENT_CAPTION,CONTENT_DATE, CONTENT_DESK, CONTENT_ID, CONTENT_IMG, CONTENT_TYPE, CONTENT_WEB_URL) values (?,?,?,?,?,?,?)";
if (sqlite3_open([databasePath UTF8String], &database)==SQLITE_OK) {
sqlite3_bind_text(insert_statement, 1, [content_caption UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insert_statement, 2, [content_date UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insert_statement, 3, [content_desk UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insert_statement, 4, [content_id UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insert_statement, 5, [content_img UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insert_statement, 6, [content_type UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insert_statement, 7, [content_web_url UTF8String], -1, SQLITE_TRANSIENT);
}
First you need to set path of your database . Do like this
// Setting path
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
databasepath =[docsDir stringByAppendingPathComponent: #"yourDatabase"];
NSFileManager *fn=[NSFileManager defaultManager];
NSError *error;
BOOL success=[fn fileExistsAtPath:databasepath];
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"quizdb"];
success = [fn copyItemAtPath:defaultDBPath toPath:databasepath error:&error];
}
//Next you have to insert as follows
sqlite3_stmt *statement;
const char *dbpath=[databasepath UTF8String];
if(sqlite3_open(dbpath,&contactDB)==SQLITE_OK)
{
const char *sqlStatement = "insert into dBase(CONTENT_CAPTION,CONTENT_DATE, CONTENT_DESK, CONTENT_ID, CONTENT_IMG, CONTENT_TYPE, CONTENT_WEB_URL) VALUES (?,?,?,?,?,?,?)";
if (sqlite3_prepare_v2(contactDB,sqlStatement,-1,&statement,NULL) == SQLITE_OK)
{
sqlite3_bind_text(statement, 1, [content_caption UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 2, [content_date UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 3 ,[content_desk UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 4, [content_id UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 5, [content_img UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 6, [content_type UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 7, [content_web_url UTF8String], -1, SQLITE_TRANSIENT);
}
if (sqlite3_step(statement)!= SQLITE_DONE)
{
NSLog(#"Save Error:%s",sqlite3_errmsg(contactDB));
}
else {
NSLog(#"success");
sqlite3_reset(statement);
}
sqlite3_finalize(statement);
}
sqlite3_close(contactDB);
Hope it will helps you..
I am trying to use LIKE in my code in following way:
-(void)getMatchedContacts{
if(sqlite3_open([[self GetDBPath] UTF8String], &database)== SQLITE_OK) {
NSMutableArray *arrFBContacts = [self getFBContacts];
if([arrFBContacts count]>0){
sqlite3_stmt *stmt;
for (MContact *mc in arrFBContacts){
const char *sql = "select count(*) from contacts where fname LIKE '?' and lname LIKE '?'";
if (sqlite3_prepare_v2(database, sql, -1, &stmt, NULL) != SQLITE_OK) {
NSAssert1(0, #"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
else{
NSLog(#"%#",[NSString stringWithFormat:#"%#",mc.firstname]);
NSLog(#"%#",[NSString stringWithFormat:#"%#",mc.lastname]);
sqlite3_bind_text(stmt, 1, [[NSString stringWithFormat:#"%#",mc.firstname] UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, [[NSString stringWithFormat:#"%#",mc.lastname] UTF8String], -1, SQLITE_TRANSIENT);
int count =0;
if(sqlite3_step(stmt) == SQLITE_ROW) {
count = sqlite3_column_int(stmt, 0);
}
sqlite3_reset(stmt);
if(count>0){
const char *sql1 = "update fbContacts set matched = 1 where id = ?";
if (sqlite3_prepare_v2(database, sql1, -1, &stmt, NULL) != SQLITE_OK) {
NSAssert1(0, #"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
else {
sqlite3_bind_int(stmt, 1, mc.fblocalcontactid);
sqlite3_step(stmt);
}
sqlite3_reset(stmt);
}
}
}
}
}
}
But I am not able to get any value for count variable from first SQL. What is wrong in y programme?
You can check my function as below: In that, like query is working properly.
-(NSMutableArray *)getCategorySearchNoDueDate:(int)catId:(NSString *)searchWord{
searchWord = [#"%" stringByAppendingFormat:#"%#%%",searchWord];
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
oClsDBFunction = [[clsDBFunction alloc] init];
appDelegate = (WFAppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.result == SQLITE_OK) {
const char *sql;
NSString *sqlString = #"SELECT \"Project\" as objectType, \"0\" as tID, projectId as pID, \"0\" as refTID, referenceId as refPID, "
"title as pTitle, \"\" as tTitle, description as Desc, "
"startDate as StartDate, startTime as StartTime, "
"dueDate as DueDate, dueTime as DueTime, compDate as CompDate, compTime as CompTime, "
"priorityId as PriorityId, categoryId as CategoryId, "
"isCompleted as IsCompleted, status as Status, isDeleted as IsDeleted, "
"remark as Remark, repeatId as RepeatId, alertCount as AlertCount, \"0\" as TaskType, "
"(select count(*) from tblTask, tblReference "
"where tblTask.referenceId=tblReference.referenceId and tblReference.projectId = tblProject.projectId) as TotalTask, "
"(select count(*) from tblTask, tblReference "
"where tblTask.referenceId=tblReference.referenceId and tblReference.projectId = tblProject.projectId and tblTask.isCompleted=1) "
"as CompletedTask "
"FROM tblProject "
"WHERE dueDate='' and "
"IsCompleted=0 and CategoryId=? and (tblProject.title like ? or tblProject.description like ?) "
"UNION "
"SELECT \"Task\" as objectType, tblTask.taskId as tID, \"0\" as pID, tblTask.referenceId as refTID, \"0\" as refPID, "
"\"\" as pTitle, title as tTitle, description as Desc, "
"\"\" as StartDate, \"\" as StartTime, "
"dueDate as DueDate, dueTime as DueTime, compDate as CompDate, compTime as CompTime, "
"priorityId as PriorityId, categoryId as CategoryId, "
"isCompleted as IsCompleted, status as Status, isDeleted as IsDeleted, "
"remark as Remark, repeatId as RepeatId, alertCount as AlertCount, taskTypeId as TaskType, "
"\"0\" as TotalTask, "
"\"0\" as CompletedTask "
"FROM tblTask, tblReference "
"WHERE IsCompleted=0 and "
"dueDate='' and "
"CategoryId=? and "
"tblTask.referenceId = tblReference.referenceId and tblReference.projectId=0 and (tblTask.title like ? or tblTask.description like ?) "
"UNION "
"SELECT \"Task\" as objectType, tblTask.taskId as tID, tblProject.projectId as pID, tblTask.referenceId as refTID, \"0\" as refPID, "
"tblProject.title as pTitle, tblTask.title as tTitle, tblTask.description as Desc, "
"\"\" as StartDate, \"\" as StartTime, "
"tblTask.dueDate as DueDate, tblTask.dueTime as DueTime, tblTask.compDate as CompDate, tblTask.compTime as CompTime, "
"tblTask.priorityId as PriorityId, tblTask.categoryId as CategoryId, "
"tblTask.isCompleted as IsCompleted, tblTask.status as Status, tblTask.isDeleted as IsDeleted, "
"tblTask.remark as Remark, tblTask.repeatId as RepeatId, tblTask.alertCount as AlertCount, taskTypeId as TaskType, "
"\"0\" as TotalTask, "
"\"0\" as CompletedTask "
"FROM tblTask, tblReference, tblProject "
"WHERE tblTask.IsCompleted=0 and "
"tblTask.dueDate='' and "
"tblTask.CategoryId=? and "
"tblTask.referenceId = tblReference.referenceId and tblReference.projectId!=0 and "
"tblProject.projectId = tblReference.projectId and (tblTask.title like ? or tblTask.description like ?) "
"order by PriorityId DESC, IsCompleted";
sql = [sqlString UTF8String];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(appDelegate.database, sql, -1, &statement, NULL) == SQLITE_OK) {
NSString *strCatId = [NSString stringWithFormat:#"%d", catId];
sqlite3_bind_text(statement, 1, [strCatId UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 2, [searchWord UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 3, [searchWord UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 4, [strCatId UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 5, [searchWord UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 6, [searchWord UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 7, [strCatId UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 8, [searchWord UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 9, [searchWord UTF8String], -1, SQLITE_TRANSIENT);
while (sqlite3_step(statement) == SQLITE_ROW) {
NSString *strObjectType = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
NSString *strTaskId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
NSString *strProjectId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
NSString *strRefTaskId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 3)];
NSString *strRefProjectId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 4)];
NSString *strProjectTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 5)];
NSString *strTaskTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 6)];
NSString *strDescription = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 7)];
NSString *strStartDate = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 8)];
NSString *strStartTime = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 9)];
NSString *strDueDate = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 10)];
NSString *strDueTime = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 11)];
NSString *strCompDate = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 12)];
NSString *strCompTime = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 13)];
NSString *strPriorityId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 14)];
NSString *strCategoryId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 15)];
NSString *strIsCompleted = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 16)];
NSString *strStatus = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 17)];
NSString *strIsDeleted = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 18)];
NSString *strRemark = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 19)];
NSString *strRepeatId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 20)];
NSString *strAlertCount = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 21)];
NSString *strTaskType = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 22)];
NSString *strTotalTask = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 23)];
NSString *strCompletedTask = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 24)];
oMainPage = [[MainPage alloc] init];
oMainPage.objectType = strObjectType;
oMainPage.tId = strTaskId;
oMainPage.pId = strProjectId;
oMainPage.refTId = strRefTaskId;
oMainPage.refPId = strRefProjectId;
oMainPage.pTitle = strProjectTitle;
oMainPage.tTitle = strTaskTitle;
oMainPage.strDesc = strDescription;
oMainPage.strStartDate = strStartDate;
oMainPage.strStartTime = strStartTime;
oMainPage.strDueDate = strDueDate;
oMainPage.strDueTime = strDueTime;
oMainPage.strCompDate = strCompDate;
oMainPage.strCompTime = strCompTime;
oMainPage.strPriority = strPriorityId;
oMainPage.strCategory = strCategoryId;
oMainPage.strIsCompleted = strIsCompleted;
oMainPage.strStatus = strStatus;
oMainPage.strIsDeleted = strIsDeleted;
oMainPage.strRemark = strRemark;
oMainPage.strRepeatId = strRepeatId;
oMainPage.strAlertCount = strAlertCount;
oMainPage.taskType = strTaskType;
oMainPage.totalTask = strTotalTask;
oMainPage.completedTask = strCompletedTask;
[tempArray addObject:oMainPage];
[oMainPage release];
}
}
sqlite3_finalize(statement);
} else {
NSAssert1(0, #"Failed to open database with message '%s'.", sqlite3_errmsg(appDelegate.database));
}
[oClsDBFunction release];
return tempArray;
}
Cheers.
Been Trying for 8 hours, not too sure which part gone wrong. i know is the ID problem as i tried the last record. it is supposed to be ID 5, but the NSlog shows 6 instead.
i removed all the unique id, so is not reserve key problem.
thanks for looking.
Please help, i going crazy........
- (id) initWithPrimaryKey:(NSInteger)pk
{
self = [super init];
ID = pk;
isDetailViewHydrated = NO;
return self;
}
- (void)saveAllData
{
if(isDirty) {
if(updateStmt == nil) {
const char *sql = "UPDATE identifyF SET TypeName = ?, gDate = ?, Location = ?, GeoCode = ?,Note = ?, Image = ?, TypeID = ? WHERE ID = ?";
if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK)
NSAssert1(0, #"Error while creating update statement. '%s'", sqlite3_errmsg(database));
}
sqlite3_bind_text(addStmt, 1, [TypeName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(updateStmt, 2, [DateStr UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(updateStmt, 3, [Location UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(updateStmt, 4, [GeoCode UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(updateStmt, 5, [Note UTF8String], -1, SQLITE_TRANSIENT);
//image
NSData *imgData = UIImagePNGRepresentation(self.Image);
int returnValue = -1;
if(self.Image != nil)
returnValue = sqlite3_bind_blob(updateStmt, 6, [imgData bytes], [imgData length], NULL);
else
returnValue = sqlite3_bind_blob(updateStmt, 6, nil, -1, NULL);
sqlite3_bind_text(updateStmt, 7, [TypeID UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(updateStmt, 8, ID);;
NSLog(#"frog name %#",TypeName);
NSLog(#"frog date %#",DateStr);
NSLog(#"location %#",Location);
NSLog(#"Geo code %#",GeoCode);
NSLog(#"note %#",Note);
NSLog(#"Img %#",Image);
NSLog(#"Type ID %#",TypeID);
NSLog(#"Frog ID %d",ID);
if(returnValue != SQLITE_OK)
NSLog(#"Not OK");
if(SQLITE_DONE != sqlite3_step(updateStmt))
NSAssert1(0, #"Error while updating. '%s'", sqlite3_errmsg(database));
sqlite3_reset(updateStmt);
isDirty = NO;
}
isDetailViewHydrated = NO;
}
+ (void) getInitialDataToDisplay:(NSString *)dbPath
{
FinderAppDelegate * appDelegate = (FinderAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.idArray removeAllObjects];
//identifyArray = [[NSMutableArray alloc] init];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
const char *sql = "SELECT ID,TypeID,TypeName,Date,Location,GeoCode,Note,Image FROM identify";
sqlite3_stmt *selectstmt;
if (sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
while(sqlite3_step(selectstmt) == SQLITE_ROW)
{
NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
identify *my = [[identify alloc] initWithPrimaryKey:primaryKey];
// my.TypeID = sqlite3_column_int(selectstmt, 1);
my.TypeID = [NSString stringWithUTF8String:(char *) sqlite3_column_text(selectstmt,1)];
my.TypeName = [NSString stringWithUTF8String:(char *) sqlite3_column_text(selectstmt,2)];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm"];
NSString *dateStr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)];
my.Date = [formatter dateFromString:dateStr];//[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)];
my.Location = [NSString stringWithUTF8String:(char *) sqlite3_column_text(selectstmt, 4)];
my.GeoCode = [NSString stringWithUTF8String:(char *) sqlite3_column_text(selectstmt, 5)];
my.Note = [NSString stringWithUTF8String:(char *) sqlite3_column_text(selectstmt, 6)];
const char *raw = sqlite3_column_blob(selectstmt, 7);
int rawLen = sqlite3_column_bytes(selectstmt, 7);
NSData *data = [NSData dataWithBytes:raw length:rawLen];
my.Image = [[UIImage alloc] initWithData:data];
my.isDirty = NO;
[appDelegate.idArray addObject:my];
}
}
} else sqlite3_close(database); //close db to release all memory
}
*** Assertion failure in -[identify saveAllData], /Users/Desmond/Desktop/Finder desmond new design 2/Finder/identify.m:241
Current language: auto; currently objective-c
Catchpoint 2 (exception thrown).objc[58781]: EXCEPTIONS: throwing 0x130db720 (object 0x1418bab0, a NSException)
objc[58781]: EXCEPTIONS: searching through frame [ip=0x1cb3db3 sp=0xbffff4c0] for exception 0x130db700
objc[58781]: EXCEPTIONS: unwinding through frame [ip=0x1cb3db3 sp=0xbffff4c0] for exception 0x130db700
objc[58781]: EXCEPTIONS: handling exception 0x130db700 at 0x1cb3e46
objc[58781]: EXCEPTIONS: rethrowing current exception
objc[58781]: EXCEPTIONS: searching through frame [ip=0x1cb3e0f sp=0xbffff4c0] for exception 0x130db700
objc[58781]: EXCEPTIONS: terminating
objc[58781]: EXCEPTIONS: searching through frame [ip=0x1f0ef21 sp=0xbffff450] for exception 0x130db700
objc[58781]: EXCEPTIONS: catch(id)
objc[58781]: EXCEPTIONS: unwinding through frame [ip=0x1f0ef21 sp=0xbffff450] for exception 0x130db700
objc[58781]: EXCEPTIONS: handling exception 0x130db700 at 0x1f0ef33
2012-01-15 22:58:07.006 FrogFinder[58781:15b03] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error while updating. 'constraint failed''
*** First throw call stack:
(0x1d7d052 0x1f0ed0a 0x1d25a78 0x132c2db 0x93167 0x952c9 0x1d7eec9 0x9a65c2 0xbe1d54 0x1d7eec9 0x9a65c2 0x9a655a 0xa4bb76 0xa4c03f 0xa4b2fe 0x9cba30 0x9cbc56 0x9b2384 0x9a5aa9 0x27a1fa9 0x1d511c5 0x1cb6022 0x1cb490a 0x1cb3db4 0x1cb3ccb 0x27a0879 0x27a093e 0x9a3a9b 0x1f3a 0x1ed5)
sqlite3_bind_text(addStmt, 1, [TypeName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(updateStmt, 2, [DateStr UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(updateStmt, 3, [Location UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(updateStmt, 4, [GeoCode UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(updateStmt, 5, [Note UTF8String], -1, SQLITE_TRANSIENT);
Are you sure that first one sould be addStmt?
Given that that's the only place where it's used in the code shown, and in a section clearly designed to populate the eight updateStmt variables, I'd say it's most likely a typo or cut-and-paste error.
i want to know how to pass particular word or character in search query to find records..
i am passing in the format like '%%%#%%'.
but it doesn't give me output...
this is my code...
searchName = [NSString stringWithFormat:#"'%%%#%%'",[[NSUserDefaults standardUserDefaults] objectForKey:#"key"]];
-(void)getSearchData
{
NSLog(#"search Name %#", searchName);
searchArray = [[NSMutableArray alloc] init];
const char *sql = "select * from features where lower(name) like ? limit 500";
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL)!= SQLITE_OK)
{
NSAssert1(0,#"error : failed to prepare statement with message '%s'.",sql ite3_errmsg(database));
}
sqlite3_bind_text(selectstmt, 1, [searchName UTF8String], -1, SQLITE_TRANSIENT);
// sqlite3_bind_text(selectstmt, 2, [state UTF8String], -1, SQLITE_TRANSIENT);
while (sqlite3_step(selectstmt) == SQLITE_ROW)
{
NSMutableDictionary *datah = [[NSMutableDictionary alloc] init];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,0)] forKey:#"number"];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,1)] forKey:#"name"];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,2)] forKey:#"type"];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,4)] forKey:#"country"];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,5)] forKey:#"lat"];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,6)] forKey:#"long"];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,7)] forKey:#"elevation"];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,8)] forKey:#"area"];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,3)] forKey:#"state"];
[searchArray addObject:datah];
[datah release];
}
sqlite3_reset(selectstmt);
}
if i pass a same query with word "s" i got 100 of record in sqlite query builder but if m pass through above code it doesn't serach any record
please help me
I worked on the code given above and fixed it as follows
-(void)getSearchData:(NSString*)searchText
{
**NSString* searchName = [NSString stringWithFormat:#"%%%#%%",searchText];**
NSLog(#"search Name %#", searchName);
sqlite3 *database;
sqlite3_stmt *selectstmt;
NSString* searchQuery = #"SELECT * FROM contacts WHERE lower(name) like ?";
const char *sql = [searchQuery UTF8String];
**if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {**
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) != SQLITE_OK)
{
//NSAssert1(0,#"error : failed to prepare statement with message '%s'.",sql ite3_errmsg(database));
}
sqlite3_bind_text(selectstmt, 1, [searchName UTF8String], -1, SQLITE_STATIC);
while (sqlite3_step(selectstmt) == SQLITE_ROW)
{
NSLog(#"%#",[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,0)]);
}
sqlite3_reset(selectstmt);
}
sqlite3_close(database);
}
The changed portions can be found between Double *. you would also like to read this
Two steps that I changed were opening the database connection which You must have done elsewhere and second removing the '' from searchName. Do let us know if this resolves the issue.