我正在为我的学校项目创建一个应用程序,该应用程序必须将数据写入我的 sqlite 数据库。只要应用程序处于活动状态,它就可以工作,但是一旦应用程序关闭,我添加的数据就消失了,当我想读取这些数据时,这将无法正常工作。我包括了我的loadData
和saveData
方法。这两个函数中的两个数据库路径是相同的,所以我不是在其他地方写我的数据。我真的找不到解决方案或问题。我什至在我的输出中插入成功,所以插入成功。
- (void) saveData:(id)sender{
NSString *sqldb = [[NSBundle mainBundle] pathForResource:@"PXLate" ofType:@"sqlite3"];
sqlite3_stmt *stmt;
NSString *queryInsert = @"INSERT INTO assignments (name, lesson, dueDate, notification, start, at) VALUES ('abc','abc', 'abc', 1, 'abc', 'abc')";
NSLog(@"%@",sqldb);
NSLog(@"%@",queryInsert);
if(sqlite3_open([sqldb UTF8String], &_PXLate) == SQLITE_OK)
{
sqlite3_prepare_v2(_PXLate, [queryInsert UTF8String], -1, &stmt, NULL);
if(sqlite3_step(stmt)==SQLITE_DONE)
{
NSLog(@"insert success");
}
else
{
NSLog(@"insert un success");
NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(_PXLate));
}
int success=sqlite3_step(stmt);
if (success == SQLITE_ERROR)
{
NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(_PXLate));
//[_PXLate save:&error];
} sqlite3_finalize(stmt);
}
sqlite3_close(_PXLate);
}
和我的loadData
功能
- (void) loadData:(id)sender
{
//path for database
NSString *sqldb = [[NSBundle mainBundle] pathForResource:@"PXLate" ofType:@"sqlite3"];
//check if present
NSFileManager*fm=[NSFileManager defaultManager];
NSLog(@"path: %@", sqldb);
const char *dbpath = [sqldb UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &_PXLate) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat: @"SELECT * FROM assignments WHERE name='abc'", _label.text];
const char *query_stmt = [querySQL UTF8String];
NSLog(@"name");
NSLog(querySQL);
int response = sqlite3_prepare_v2(_PXLate, query_stmt, -1, &statement, NULL);
NSLog(@"response %d", response);
if (response == SQLITE_OK)
{
NSLog(@"name");
if (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *namefield = [[NSString alloc]
initWithUTF8String:
(const char *) sqlite3_column_text(
statement, 0)];
NSLog(@"name:%@", namefield);
_label.text = namefield;
} else {
_label.text = @"Match not found";
}
sqlite3_finalize(statement);
}
sqlite3_close(_PXLate);
}
}