2011-04-08 76 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