如何通过Firefox的SQLite插件添加另一行到我的数据库?
问题描述:
我刚刚在我的Iphone Apps中开始使用SQLite,我使用SQLite插件为Firefox创建了数据库,并且运行该应用程序的效果很好。但现在我试图向其中添加另一行并查看其工作,但出于某种原因,我可以添加另一行,但无法在我的应用中看到它。 这里是我用来遍历行的代码。如何通过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的数据库被保存在文件夹用户/库/应用程序支持/ iPhone模拟器/应用程序文件夹。你需要去那里删除数据库,然后再次运行应用程序。
什么是'databasePath'? – 2011-06-10 01:24:37
请参阅编辑... – Ashutosh 2011-06-10 02:09:10
实际上,无论何时您使用sqlite时,数据库都会保存在文件夹user/library/application support/iphone simulator/app文件夹中。你需要去那里删除数据库,然后再次运行应用程序。 – Ashutosh 2011-06-18 00:32:24