sqlite3无法在iOS 6中添加数据库

时间:2015-03-26 16:54:54

标签: ios xcode sqlite

我在将文本字段数据插入数据库时​​遇到问题。它提醒我fail to add in database

请某人帮我解决我的新项目。

 NSString*docdir;
    NSArray*dirpath;

    dirpath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    docdir=[dirpath objectAtIndex:0];
    _databasepath=[[NSString alloc]initWithString:[docdir stringByAppendingPathComponent:@"sehatis.db"]];
    NSFileManager*filemng=[NSFileManager defaultManager];
    if ([filemng fileExistsAtPath:_databasepath]==NO) {
        const char*dbpath=[_databasepath UTF8String];
        if (sqlite3_open(dbpath, &_db)==SQLITE_OK) {
            char *errormsg;
            const char*sql_statement="CREATE TABLE IF NOT EXITS userss(ID INTEGER PRIMARY KEY AUTOINCREMENT,name text,age integer,gender text,female text,height integer,weight integer,blood text,email text,password text,conformpwd text)";
            if (sqlite3_exec(_db, sql_statement, NULL, NULL, &errormsg)!=SQLITE_OK) {
                [self showUIAlertViewWithMessage:@"fail to create table" andtitle:@"error"];
            }
            sqlite3_close(_db);
        }
        else{
            NSLog(@"fail to register");

            [self showUIAlertViewWithMessage:@"fail to open / create table" andtitle:@"error"];
        }


}
}




- (IBAction)GREATEPROFILE:(id)sender {

    sqlite3_stmt*statement;
    const char*dbspath=[_databasepath UTF8String];
    if (sqlite3_open(dbspath, &_db)==SQLITE_OK) {
        NSString*insert_sql=[NSString stringWithFormat:@"INSERT INTO userss(name,age,gender,female,height,weight,blood,email,password,conformpwd)values(\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\")",self.name.text,self.age.text,self.gender.text,self.female.text,self.height.text,self.weight.text,self.blood.text,self.email.text,self.password.text,self.conformpwd.text];
                const char*insert_statement=[insert_sql UTF8String];
        sqlite3_prepare_v2(_db, insert_statement,-1, &statement,NULL);
        if (sqlite3_step(statement)==SQLITE_DONE) {
            [self showUIAlertViewWithMessage:@"user add to data base" andtitle:@"message"];

            self.name.text=@"";
            self.age.text=@"";
            self.gender.text=@"";
            self.female.text=@"";
            self.height.text=@"";
            self.weight.text=@"";
            self.blood.text=@"";
            self.email.text=@"";
            self.password.text=@"";
            self.conformpwd.text=@"";

        }
        else{
            [self showUIAlertViewWithMessage:@"fail to add in database" andtitle:@"error"];
        }
        sqlite3_finalize(statement);
        sqlite3_close(_db);
    }
        }

1 个答案:

答案 0 :(得分:0)

我认为问题出在这里。您有CREATE TABLE IF NOT EXITS,但应该是EXISTS

您也不希望使用格式化字符串来插入变量值。请改用绑定操作。有关详细信息,请参阅here

int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
int sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64,
                    void(*)(void*));
int sqlite3_bind_double(sqlite3_stmt*, int, double);
int sqlite3_bind_int(sqlite3_stmt*, int, int);
int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
int sqlite3_bind_null(sqlite3_stmt*, int);
int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
int sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
                     void(*)(void*), unsigned char encoding);
int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);