Iphone 一旦应用程序停止并再次执行,Sqlite不会插入数据
我正在开发一个简单的应用程序,其中它正在创建一个sqlite数据库并向其中插入数据。 它工作正常,数据被插入到表中 但是当我停止应用程序(在xcode中)并再次运行时,数据并没有插入到数据库中 这是我使用的代码:Iphone 一旦应用程序停止并再次执行,Sqlite不会插入数据,iphone,objective-c,database,sqlite,Iphone,Objective C,Database,Sqlite,我正在开发一个简单的应用程序,其中它正在创建一个sqlite数据库并向其中插入数据。 它工作正常,数据被插入到表中 但是当我停止应用程序(在xcode中)并再次运行时,数据并没有插入到数据库中 这是我使用的代码: +(DBManager*)getSharedInstance{ if (!sharedInstance) { sharedInstance = [[super allocWithZone:NULL]init]; [sharedInstance c
+(DBManager*)getSharedInstance{
if (!sharedInstance) {
sharedInstance = [[super allocWithZone:NULL]init];
[sharedInstance createDB];
}
return sharedInstance;
}
-(BOOL)createDB{
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString:
[docsDir stringByAppendingPathComponent: @"SchedulerDataBase.db"]];
BOOL isSuccess = YES;
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt ="create table if not exists Scheduler (Date integer primary key,Event text)";
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;
}
-(BOOL)saveData:(int)date withEvent:(NSString *)event
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:@"insert into Scheduler (Date, Event) values(\"%d\", \"%@\")",date,event];
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;
}
当我停止项目,然后试图保存到数据库时,它总是在输入
在这个循环的else条件下
if (sqlite3_step(statement) == SQLITE_DONE)
{
return YES;
}
else {
return NO;
}
谁能帮我一下吗。
提前感谢。在“保存数据”方法中,您似乎永远不会完成语句或关闭数据库
sqlite3_finalize(statement);
sqlite3_close (_database);
可能您的数据库在上次保存时仍被锁定。这是因为只有在启动程序而不调用
createDB
时,您才能在创建数据库时获取数据库路径。您将不会保存数据
您必须向saveData中添加以下代码:
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString:
[docsDir stringByAppendingPathComponent: @"SchedulerDataBase.db"]];
或者只需创建一个方法,如-(NSString*)openDB
,该方法返回上述数据库路径,并在保存数据代码的第一行中使用它,如:
const char *dbpath = [[self openDB] UTF8String];
是否检查了sqlite3_prepare_v2()的返回值?SQLITE_在这两种情况下都可以吗?何时调用
saveData:withEvent:
方法?我建议您首先检查启动代码在创建数据库方面是否走上了正确的道路。@AnkitJain在第一种情况下返回YES。在第二种情况下返回NO。@TomHarrington它在-(void)中保存textViewDidChange:(TextView*)TextView{