当应用程序终止并再次启动时,无法插入iphone的sqlite数据库
该应用程序第一次工作正常,但当我重新启动它时,它可以读取早期的值,但无法插入新值。。。请帮忙 以下是我如何称呼这个当应用程序终止并再次启动时,无法插入iphone的sqlite数据库,iphone,sqlite,operation,Iphone,Sqlite,Operation,该应用程序第一次工作正常,但当我重新启动它时,它可以读取早期的值,但无法插入新值。。。请帮忙 以下是我如何称呼这个 -(void)checkAndCreateDatabase{ NSLog(@"hello"); databaseName = @"EMAP234.sql"; NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, Y
-(void)checkAndCreateDatabase{
NSLog(@"hello");
databaseName = @"EMAP234.sql";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
NSLog(@"database path %@",databasePath);
BOOL success;
// Create a FileManager object, we will use this to check the status
// of the database and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the database has already been created in the users filesystem
success = [fileManager fileExistsAtPath:databasePath];
// If the database already exists then return without doing anything
if(success) return;
// If not then proceed to copy the database from the application to the users filesystem
// Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
[fileManager release];
}
-(void)insertIntoDatabase:(NSString *)insertCommand{
// Setup the database object
sqlite3 *database;
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = [insertCommand UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
NSLog(@"inserted");
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
你好Ankit我用了你的密码打了电话
DatabaseOperations *obj=[[DatabaseOperations alloc]init];
[obj checkAndCreateDatabase];
NSLog(@"patient ID %d",[obj readMaxPatientIDFromDatabase]);
patientID= [NSString stringWithFormat:@"HCI%d", ([obj readMaxPatientIDFromDatabase]+1)];
NSLog(@"patient ID %@",patientID);
[self generatePatientInsertCommand];
[obj insertIntoDatabase:patientInsertCommand];
[self generateCarrierInsertCommand];
[obj insertIntoDatabase:carrierInsertCommand];
[self generateSyncTableInsertCommand];
[obj insertIntoDatabase:syncTableInsertCommand];
NSString *msg=@"your PatientID is";
[obj release];
msg=[msg stringByAppendingString:patientID];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"REGISTERED" message:msg delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
[alert show];
[alert release];
我唯一改变的是使用不同的数据库文件,我在checkAndCreateDatabase函数中选择了这个文件
[self checkAndCreateDatabase];
[self insertIntoDatabase:[NSString stringWithFormat:@"INSERT INTO User (uId,userName,userBirthday) VALUES (\"%d\", \"%@\", \"%@\" )",3,@"Admin",@"2010-12-01"]];
代码在每次插入新uId时都运行良好。请检查您的读取函数,如果可能,请将读取函数的代码与上述代码以及调用这两个函数的方式(即读取和插入函数)一起粘贴。数据库中没有条目,我已通过sqlite管理器进行了检查。我在nsobject类中有这两个函数,并从rootViewController调用它们。您第一次提到应用程序工作正常,但当我重新启动它时,它可以读取早期值,但无法插入新值。你能用从rootviewController调用这些方法的方式粘贴代码吗?通过在创建数据库时创建模式而不是动态创建表来解决问题。
-(void)checkAndCreateDatabase{
NSLog(@"hello");
databaseName = @"MasterDB.sqlite";
....
}