当我第一次启动应用程序时,它工作正常,并显示在应用程序中插入新的记录但是一旦我关闭应用程序它没有显示sqlite数据库的任何记录它只显示我插入硬编码形式sqlite的记录app.any以外的经理想法如何解决这个问题,以便当我关闭应用程序时,它还应该显示新插入的数据。这是插入数据的代码
- (void) addCoffeePL:(NSString *)dbPath {
if(addStmt == nil) {
const char *sql = "insert into publish(userID,Catgeory,SubCategory,ContentType,ContentTitle,ContentDescription,ContenAddedTime,Size,UserName,organizationCode) Values(?,?,?,?,?,?,?,?,?,?)";
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}
sqlite3_bind_text(addStmt, 1, [userID UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 2, [categoryTitle UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 3, [subCategoryTitle UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 4, [contentType UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 5, [contentTitle UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 6, [contentDescription UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 7, [contentAddedDateTime UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 9, [userName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 10, [size UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 11, [organizationCode UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(addStmt))
NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
//SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
coffeeID = sqlite3_last_insert_rowid(database);
//Reset the add statement.
sqlite3_reset(addStmt);
sqlite3_finalize(addStmt);
}
答案 0 :(得分:1)
确定, 每次要使用它时都不应打开数据库。而是使用数据库连接创建单例类
#define kDatabaseFilename @"yourDatabaseName.sqlite"
@implementation DatabaseConnector
+ (DatabaseConnector *)instance {
static dispatch_once_t once;
static DatabaseConnector * sharedInstance;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
- (id) init
{
self = [super init];
if (self)
{
NSString *pathsToResources = [[NSBundle mainBundle] resourcePath];
NSString *databasePath = [pathsToResources stringByAppendingPathComponent:kDatabaseFilename];
NSArray *pathsToDocuments = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [pathsToDocuments objectAtIndex:0];
NSString *newDatabasePath = [documentsDirectory stringByAppendingPathComponent:kDatabaseFilename];
if (![[NSFileManager defaultManager] isReadableFileAtPath:newDatabasePath])
{
NSLog(@"No database available, copying from bundle");
if ([[NSFileManager defaultManager] copyItemAtPath:databasePath toPath:newDatabasePath error:NULL] != YES)
{
NSLog(@"Fail to copy database from %@ to %@", databasePath, newDatabasePath);
}
}
if (sqlite3_open([newDatabasePath UTF8String], &_database) != SQLITE_OK)
{
NSLog(@"Failed to open database!");
}
}
return self;
}
然后使用此函数插入:
- (void)addCoffeePL:(YourObject*)yourObject
{
sqlite3_stmt *statement;
const char *sql = "insert into publish(userID,Catgeory,SubCategory,ContentType,ContentTitle,ContentDescription,ContenAddedTime,Size,UserName,organizationCode) Values(?,?,?,?,?,?,?,?,?,?)";
if (sqlite3_prepare_v2(_database, sql, -1, &statement, NULL) != SQLITE_OK)
{
NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(_database));
}
sqlite3_bind_text(statement, 1, [yourObject.userID UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 2, [yourObject.categoryTitle UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 3, [yourObject.subCategoryTitle UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 4, [yourObject.contentType UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 5, [yourObject.contentTitle UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 6, [yourObject.contentDescription UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 7, [yourObject.contentAddedDateTime UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 8, [yourObject.userName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 9, [yourObject.size UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 10, [yourObject.organizationCode UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(statement))
{
NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(_database));
}
sqlite3_reset(statement);
}
答案 1 :(得分:0)
您缺少第8个参数
sqlite3_bind_text(addStmt, 7, [contentAddedDateTime UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 9, [userName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 10, [size UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 11, [organizationCode UTF8String], -1, SQLITE_TRANSIENT);
更改为
sqlite3_bind_text(addStmt, 7, [contentAddedDateTime UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 8, [userName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 9, [size UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 10, [organizationCode UTF8String], -1, SQLITE_TRANSIENT);
答案 2 :(得分:0)
只需按照下面给出的示例,其中使用单例和SQLC模式的SQLC ..对您来说可能更有帮助......