我正在尝试使用 sqlite 管理器创建数据库。但是这些值没有存储在数据库中。如果我单击保存按钮,警报消息将显示在这样的“数据插入失败”上。我正在尝试纠正这些问题。在这个案例我访问了很多教程。但我无法纠正我的问题。昨天我完全被这个问题阻止了。请给我任何想法或建议如何保存数据。谢谢大家访问这个问题。TC
数据库.m
// 创建数据库
-(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];
}
}