2011-04-08 78 views
1

我对sqlite很熟悉,试图将值插入数据库,但给出异常。 tf1和tf2是文本字段的对象。这里是我的代码:在将数据插入数据库中的问题

 
@synthesize tf1, tf2, add, disp; 
-(id)initApp 

{ 
if(![super init]) 
     return nil; 

    //DB stored in application bundle 
    NSString *DBName = @"kbase.sqlite"; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *path = [documentsDirectory stringByAppendingString:DBName]; 

    db = [FMDatabase databaseWithPath:path]; 
    [db setLogsErrors:TRUE]; 
    [db setTraceExecution:TRUE]; 

    if(![db open]) 
    { 
     NSLog(@"Could not open Database"); 
     return 0; 
    } 

    else 
    { 
     NSLog(@"Database opened successfully"); 
    } 
    return self; 
} 


- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self initApp]; 
    [add addTarget:self action:@selector(insertButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    [disp addTarget:self action:@selector(displaytButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
} 


-(IBAction)insertButtonClicked :(id)sender 
{ 
    NSLog(@"insert"); 
    NSLog(@"db.....%@",db); 
    [db executeUpdate:@"insert into info values (?,?)", tf1.text, tf2.text]; 

    [self resignFirstResponder]; 

} 

-(IBAction)displayButtonClicked:(id)sender 

{ 
    NSString *str1 = tf2.text; 
    NSString *returnResult = [[NSString alloc]init]; 
    FMResultSet *rs = [db executeQuery:@"select name from info where rollno = 1"]; 

    while ([rs next]) 
    { 
     returnResult = [rs stringForColumn:@"rollno"]; 
     [tf1 setText:returnResult]; 
     [tf2 setText:returnResult]; 
    }  
} 

DATABSE成功地打开和打印插入 异常后CONTROLL终止:

 
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL executeQuery:]: unrecognized selector sent to instance 0x3d30ef0' 

我不能够配置问题。 plz帮助。

+0

显示executeUpdate的代码 – 2011-04-08 08:40:28

+0

如您所见,NSURL对象获取查询方法。你可以在启动你的语句之前转储数据库的类名?并请发布代码如何设置数据库。 – 2011-04-08 08:42:42

+0

你在做什么executeUpdate方法? 你可以显示代码吗? – 2011-04-08 08:45:01

回答

0

我想使你的方法

[DB的executeUpdate:@ “插入信息值(?,?)” withValue1:tf1.text withValue2:tf2.text]。

代替

[分贝的executeUpdate: “(?,?)插入到信息值” @,tf1.text,tf2.text];

+0

让我解释一切。我已经通过sqlite管理器创建了数据库,并且只声明了列名“name”,“rollno”。我把这个数据库拖到我的资源文件夹中。所以它只是例外 – Ketan 2011-04-08 13:43:16

0

看起来db在此上下文中不存在。如果db是一个类变量,请确保它不在其他地方发布。简单地把一个NSLog(@"%@", db);放在执行之上来测试它。

+0

db是FMDatabase类对象! – Ketan 2011-04-08 09:45:49

+0

抱歉显示路径:localhost/Users/gauravmurghai/Library/Application%20Support/iPhone%20Simulator/User/Library/Preferences/.GlobalPreferences.plist。我没有把calue放在UITextField中! – Ketan 2011-04-08 10:01:37