Sqlite3 INSERT INTO问题×377

时间:2010-04-14 18:36:53

标签: objective-c sqlite

我正在创建一个练习应用程序,用于记录7天内每天4个“套装”中使用的重量和用户所做的“代表”数量,以便用户查看他们的进度。

我已经构建了名为FIELDS的数据库表,其中包含2列ROWFIELD_DATA,我可以使用下面的代码将数据加载到数据库中。但代码有一个sql语句,说

INSERT OR REPLACE INTO FIELDS (ROW, FIELD_DATA)VALUES (%d, '%@'); 

当我将声明更改为:

INSERT INTO FIELDS (ROW, FIELD_DATA)VALUES (%d, '%@'); 

什么都没发生。那就是没有数据记录在数据库中。

以下是代码:

#define kFilname @"StData.sqlite3"

- (NSString *)dataFilePath
{
  NSArray *paths = NSSearchPathForDirectoriesInDomains
    (NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  return [documentsDirectory stringByAppendingPathComponent:kFilname];
}

-(IBAction)saveData:(id)sender;
{

  for (int i = 1; i <= 8; i++)
  {
    NSString *fieldName = [[NSString alloc]initWithFormat:@"field%d", i];
    UITextField *field = [self valueForKey:fieldName];
    [fieldName release];

    NSString *insert = [[NSString alloc] initWithFormat:
        @"INSERT OR REPLACE INTO FIELDS (ROW, FIELD_DATA)
          VALUES (%d, '%@');",i, field.text];
    // sqlite3_stmt *stmt;

    char *errorMsg;

    if (sqlite3_exec (database, [insert UTF8String],
        NULL, NULL, &errorMsg) != SQLITE_OK)
    {
      // NSAssert1(0, @"Error updating table: %s", errorMsg);
      sqlite3_free(errorMsg);
    }  
  }
  sqlite3_close(database);    
}

那么我该如何修改代码来做我想要的呢?这看起来似乎是一个简单的sql语句改变,但显然必须有更多。我是Objective-C和iPhone编程的新手。 我不习惯使用sql语句,因为我已经在ASP中创建了多年的Web应用程序。

任何帮助将不胜感激,这让我疯狂!

1 个答案:

答案 0 :(得分:0)

建议:

  • 使用硬编码值编写一个insert语句,以查看插入是否正常工作
  • 你的文件名没有路径。是否在执行时假定当前目录?从哪个目录运行?
  • 如果可能的话,在屏幕上写一条消息,看看你得到的是什么。它们是否正确?