问题描述
|
我只是在Iphone应用程序中从sqlite开始,我使用Firefox的sqlite附加组件创建了数据库,并很好地运行了该应用程序。但是现在我试图向其添加另一行并查看其工作,但是由于某种原因,我能够添加另一行但在我的应用程序中看不到它。
这是我用来遍历行的代码。
-(void) readAnimalsFromDatabase {
// Setup the database object
sqlite3 *database;
// Init the animals Array
animals = [[NSMutableArray alloc] init];
// 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 = \"select * from animals\";
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
while(sqlite3_step(compiledStatement) == sqlITE_ROW) {
// Read the data from the result row
Nsstring *aName = [Nsstring stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,1)];
Nsstring *aDescription = [Nsstring stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,2)];
Nsstring *aimageUrl = [Nsstring stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,3)];
// Create a new animal object with the data from the database
Animal *animal = [[Animal alloc] initWithName:aName description:aDescription url:aimageUrl];
// Add the animal object to the animals Array
[animals addobject:animal];
[animal release];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
-(void) checkAndCreateDatabase{
// Check if the sql database has already been saved to the users phone,if not then copy it over
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];
}
请帮我解决一下这个。
谢谢,
解决方法
实际上,无论何时使用sqlite,数据库都会保存在文件夹user / library / application support / iphone Simulator / app文件夹中。您需要去那里并删除数据库,然后再次运行该应用程序。