我有一个sqlite文件,我添加到我的Xcode项目并将其复制到iPhone上的Documents目录中。我已经验证它存在,但似乎仍然无法更新文件。下面的代码是应该将数据传递到sqlite文件的操作方法:
-(IBAction)saveButtonPressed:(id)sender
{
//Catch current time in specified format
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *currentDate = [NSDate date];
NSString *localDateString = [dateFormat stringFromDate:currentDate];
//Catch location
CLLocation *location = [self.locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
NSString *currentCoordinate = [NSString stringWithFormat:@"%f, %f",coordinate.latitude,coordinate.longitude];
NSLog(@"%@ ... %@",localDateString,currentCoordinate);
NSString *query = [NSString stringWithFormat:@"INSERT INTO sqliteData values(null, 'near', '%@', '%@')", localDateString, currentCoordinate];
[self.dbManager executeQuery:query];
if (self.dbManager.affectedRows != 0) {
NSLog(@"Query was executed");
}
else{
NSLog(@"Could not execute query.");
}
}
executeQuery方法:
- (void)executeQuery:(NSString *)query
{
[self runQuery:[query UTF8String] isQueryExecutable:YES];
}
编辑: runQuery方法
-(void)runQuery:(const char *)query isQueryExecutable:(BOOL)queryExecutable{
sqlite3 *sqlite3Database;
// Define database file path.
NSString *databasePath = [self.documentsDirectory stringByAppendingPathComponent:self.databaseFilename];
// Initialize the results array.
if (self.arrayResults != nil) {
[self.arrayResults removeAllObjects];
self.arrayResults = nil;
}
self.arrayResults = [[NSMutableArray alloc] init];
// Initialize the column names array.
if (self.arrColumnNames != nil) {
[self.arrColumnNames removeAllObjects];
self.arrColumnNames = nil;
}
self.arrColumnNames = [[NSMutableArray alloc] init];
// Open the database.
BOOL openDatabaseResult = sqlite3_open([databasePath UTF8String], &sqlite3Database);
if(openDatabaseResult == SQLITE_OK) {
// Declare a sqlite3_stmt object in which will be stored the query after having been compiled into a SQLite statement.
sqlite3_stmt *compiledStatement;
// Load all data from database to memory.
BOOL prepareStatementResult = sqlite3_prepare_v2(sqlite3Database, query, -1, &compiledStatement, NULL);
if(prepareStatementResult == SQLITE_OK) {
// Check if the query is non-executable.
if (!queryExecutable){
// In this case data must be loaded from the database.
// Declare an array to keep the data for each fetched row.
NSMutableArray *arrDataRow;
// Loop through the results and add them to the results array row by row.
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Initialize the mutable array that will contain the data of a fetched row.
arrDataRow = [[NSMutableArray alloc] init];
// Get the total number of columns.
int totalColumns = sqlite3_column_count(compiledStatement);
// Go through all columns and fetch each column data.
for (int i=0; i<totalColumns; i++){
// Convert the column data to text (characters).
char *dbDataAsChars = (char *)sqlite3_column_text(compiledStatement, i);
// If there are contents in the currenct column (field) then add them to the current row array.
if (dbDataAsChars != NULL) {
// Convert the characters to string.
[arrDataRow addObject:[NSString stringWithUTF8String:dbDataAsChars]];
}
// Keep the current column name.
if (self.arrColumnNames.count != totalColumns) {
dbDataAsChars = (char *)sqlite3_column_name(compiledStatement, i);
[self.arrColumnNames addObject:[NSString stringWithUTF8String:dbDataAsChars]];
}
}
// Store each fetched data row in the results array, but first check if there is actually data.
if (arrDataRow.count > 0) {
[self.arrayResults addObject:arrDataRow];
}
}
}
else {
int executeQueryResults = sqlite3_step(compiledStatement);
if (executeQueryResults == SQLITE_DONE) {
// Keep the affected rows.
self.affectedRows = sqlite3_changes(sqlite3Database);
// Keep the last inserted row ID.
self.lastInsertedRowID = sqlite3_last_insert_rowid(sqlite3Database);
}
else {
NSLog(@"DB Error: %s", sqlite3_errmsg(sqlite3Database));
}
}
}
else {
NSLog(@"%s", sqlite3_errmsg(sqlite3Database));
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(sqlite3Database);
}
答案 0 :(得分:0)
问题最终导致我的设备上有一个旧的SQLite文件副本。我删除了应用程序并重新启动它;这就是诀窍。