将带有图像的按钮存入sqlite3数据库

时间:2012-01-09 05:07:15

标签: iphone sqlite insert uibutton

我有一个表格(表格视图),其中包含textfields和textview作为子视图,我需要保存填写在这些字段中的数据。我使用sqlite3.Everything工作正常,保存检索和显示保存的数据。但是在表格中看,我有一个单元格,其中包含一个按钮作为子视图。我们知道在iphone中没有称为复选框按钮的概念。因此我使用了按钮并添加了默认图像作为以下所示的子视图:

enter image description here

如果未选择该按钮,图像将保持不变。

现在,如果用户选择按钮,则图像会更改为复选标记,表示逻辑每年都在工作,如下所示:

enter image description here

现在我的问题是如何将按钮图像保存到sqlite3.I使用以下查询来插入值:

NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO reminders(name,event,date,tim,bfr,num,bod,grp) VALUES (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\",\"%@\", \"%@\", \"%@\")", fieldOne.text, fieldTwo.text,fieldThree.text,fieldFour.text,fieldFive.text,fieldSix.text,textView.text,fieldSeven.text];

同样如何在sqlite3中保存带有图像作为子视图的按钮。我听说在sqlite3中没有类似BOOL值存储的概念。相反,它存储为int值,即“0”为false,“1”为true .Hence我使用以下查询来插入按钮图像:

int selValue;

if(checkboxSelected == NO)
{
    selValue = 0;
}

else if(checkboxSelected == YES)
{
    selValue = 1;
}
NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO reminders(name,event,date,tim,bfr,val,num,bod,grp) VALUES (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\",\"%@\", \"%@\", \"%@\")", fieldOne.text, fieldTwo.text,fieldThree.text,fieldFour.text,fieldFive.text,isSelected,fieldSix.text,textView.text,fieldSeven.text]; 

在控制台中,如果选择了按钮,则selValue显示为1,否则显示为0。

但是我有一个名为视图编辑提醒的控制器页面,用户可以在其中查看已保存的提醒并编辑更改。现在我已经保存了一个名为Amma的提醒,在保存时我选择了每年按钮,但是即使为提醒检索了插入的数据,也无法正常工作,如下所示:

enter image description here

编辑:

我为从sqlite3数据库表中检索数据而实现的代码:

ReminderClass *remind = [[ReminderClass alloc]init];
                remind.Name = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 1)];
                remind.Event = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 2)];
                remind.Date = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 3)];

                remind.time = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 4)]; 
                remind.numDays = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 5)];
                remind.selString = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 6)]];
                remind.number = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 7)];
                remind.msgBody = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 8)];
                remind.remGroup = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 9)];

编辑:

这是我为编辑内容所做的:

-(void)tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (aTable.editing) 
    {
        ReminderClass *rem = (ReminderClass *)[self.monthArray objectAtIndex:indexPath.section];

        // Instantiate your detail/editor view controller,
        // and pass in the ReminderClass object to be edited.
        ERAddReminderViewController *rdvc = [[[ERAddReminderViewController alloc]initWithReminder:rem]autorelease];
        [self.navigationController pushViewController:rdvc animated:YES];
    }

    [atableView deselectRowAtIndexPath:indexPath animated:YES];
}

请帮助我提出宝贵的建议

提前感谢所有人:)

2 个答案:

答案 0 :(得分:1)

如果你得到remind.selString,那么将其保存在

NSString *str = [NSString stringWithFormat:@"%@", remind.selString];

然后使用

检查您的状态
if([str isEqualToString:@"0"]){
// put your code here to set your images 
}
else{

}

答案 1 :(得分:1)

设定:

int selValue;

if(checkboxSelected == NO)
{
    selValue = 0;
}

else if(checkboxSelected == YES)
{
    selValue = 1;
}


       NSNumber *number = [NSNumber numberWithInt:selValue]; 
    NSString *isSelected = [number stringValue];


 NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO reminders(name,event,date,tim,bfr,val,num,bod,grp) VALUES (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\",\"%@\", \"%@\", \"%@\")", fieldOne.text, fieldTwo.text,fieldThree.text,fieldFour.text,fieldFive.text,isSelected,fieldSix.text,textView.text,fieldSeven.text];

得到:

 remind.selString = [[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 6)];

 NSString *one = remind.selString;
     NSNumber* i = [NSNumber numberWithInt:[one intValue]];;
     BOOL isOn = [i boolValue];

if(isOn){
   //set ON Image
}
else{
   //set OFF image
}