如何在iphone中保存sqlite3中的数据

时间:2013-07-11 12:01:18

标签: iphone ios objective-c sqlite

我正在尝试使用sqlite manager创建数据库。但是这些值没有存储在数据库中。如果我单击“保存”按钮,警报消息将显示在“数据插入失败”上。我正在尝试纠正这些问题。在这种情况下我访问了这么多教程。但是我无法纠正我的问题。昨天我完全被阻止了这个问题。请给我任何想法或建议如何保存数据。谢谢大家访问这个问题.TC

DataBase.m

//创建DATABASE

-(BOOL)createDB

{

NSString *docsDir;

NSArray *dirPaths;


// Get the document directory


dirPaths=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);


docsDir=dirPaths[0];


// Build the path to the database file


databasePath=[[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:@"Feedback.db"]];


BOOL isSuccess=YES;


NSFileManager *fileManager=[NSFileManager defaultManager];


if([fileManager fileExistsAtPath:databasePath]==0)


{


const char *dbpath=[databasePath UTF8String];


if(sqlite3_open(dbpath, &database)==SQLITE_OK)


{


char *errMsg;


const char *sql_stmt= "create table if not exists Feeback details (Traineeid integer, Trainername text,Traineename text,Rating float)";


if(sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg)!=SQLITE_OK)


{


isSuccess=NO;


 NSLog(@"Failed to create table");


}


sqlite3_close(database);


return isSuccess;


}


else


{


isSuccess=NO;


NSLog(@"Failed to open/Create database");


}


}

return isSuccess;

}

// save data in the Database

-(BOOL) saveData:(NSString *)Traineeid Trainername:(NSString *)Trainername Traineename:(NSString *)Traineename Rating:(NSString *)Rating;


{

const char *dbpath=[databasePath UTF8String];


if(sqlite3_open(dbpath, &database)==SQLITE_OK)


{


NSString *insertSQL=[NSString stringWithFormat:@"insert into Feedbackdetails(Traineeid,Trainername,Traineename,Rating) values(\"%d\",\"%@\", \"%@\", \"%@\")",[Traineeid integerValue],Trainername,Traineename,Rating];


const char *insert_stmt=[insertSQL UTF8String];


sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL);


if(sqlite3_step(statement)==SQLITE_DONE)


{

return YES;

}

else

{

return NO;

}

sqlite3_reset(statement);


}

return NO;

}


FeebBackForm.m

-(IBAction)saveData:(id)sender

{

BOOL success=NO;

NSString *alertString = @"Data Insertion failed";

if (Traineeid.text.length>0 &&Trainername.text.length>0 &&Traineename.text.length>0 &&Rating.text.length>0)

{

success=[[DBManager getSharedInstance]saveData:Traineeid.text Trainername:Trainername.text Traineename:Traineename.text Rating:Rating.text];

}

else

{

alertString = @"Enter all fields";

}

if (success == NO) 

{

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:alertString message:nil
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];


[alert show];


}

}

1 个答案:

答案 0 :(得分:0)

你的put字符串在你的sql语句中引用你的int参数。

这可能不是你唯一的问题,但你应该绑定params。看起来你在sql insert语句中的整数值周围加上引号,它们被定义为表中的整数。

"create table if not exists Feeback details (Traineeid integer, Trainername text,Traineename text,Rating float)"

"insert into Feedbackdetails(Traineeid,Trainername,Traineename,Rating) values(\"%d\",\"%@\", \"%@\", \"%@\")"

注意你的整数双引号。

此外,注销数据库的路径(即使在模拟器中运行)。转到sqlite cmd行并确保db存在且空表存在。这有助于排除故障。

最后,看看@ fmdb sqlite包装器 - 它有助于使用sqlite,但它的代码也显示了使用sqlite raw的良好模式,如果这是你的偏好。

这是我的一个样本中的类似函数,它显示了如何绑定params。您还应该最终确定准备的内容:

- (void)updateContact: (Contact*)contact error:(NSError**)error
{
    if (![self ensureDatabaseOpen:error])
    {
        return;
    }

    NSLog(@">> ContactManager::updateContact");

    // prep statement
    sqlite3_stmt    *statement;
    NSString *querySQL = @"update contacts set name=?,address=?,phone=? where id=?";
    NSLog(@"query: %@", querySQL);
    const char *query_stmt = [querySQL UTF8String];

    // preparing a query compiles the query so it can be re-used.
    sqlite3_prepare_v2(_contactDb, query_stmt, -1, &statement, NULL);     
    sqlite3_bind_text(statement, 1, [[contact name] UTF8String], -1, SQLITE_STATIC);
    sqlite3_bind_text(statement, 2, [[contact address] UTF8String], -1, SQLITE_STATIC);
    sqlite3_bind_text(statement, 3, [[contact phone] UTF8String], -1, SQLITE_STATIC);
    sqlite3_bind_int64(statement, 4, [[contact id] longLongValue]);

    NSLog(@"bind name: %@", [contact name]);
    NSLog(@"bind address: %@", [contact address]);
    NSLog(@"bind phone: %@", [contact phone]);
    NSLog(@"bind int64: %qi", [[contact id] longLongValue]);

    // process result
    if (sqlite3_step(statement) != SQLITE_DONE)
    {
        NSLog(@"error: %@", sqlite3_errmsg(_contactDb));
    }

    sqlite3_finalize(statement);
}