-1
- (void)viewDidLoad
{
    [super viewDidLoad];// Do any additional setup after loading the view.
    NSString *docsDir;
    NSArray *dirPaths;
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];

    databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@"Notebook1.sqlite"]];
    NSFileManager *fileMgr = [NSFileManager defaultManager];

    NSLog(@"%@",databasePath);

    if ([fileMgr fileExistsAtPath:databasePath]==NO) 
    {
        const char *dbpath = [databasePath UTF8String];    
        NSLog(@"hohoho, %s",dbpath);
        if (sqlite3_open(dbpath, &noteDB)==SQLITE_OK) 
        {
            char *errMsg;
            const char *sql_str = "CREATE TABLE IF NOT EXISTS Notebook1 (ID INTEGER PRIMARY KEY AUTOINCREMENT, Whattime Text, Address TEXT, What TEXT, Who TEXT, NOTE TEXT)";

            if (sqlite3_exec(noteDB, sql_str, NULL, NULL, &errMsg)!=SQLITE_OK) 
            {
                NSLog(@"Failed to create table");
            }


            sqlite3_close(noteDB);
        }
        else
        {
            NSLog(@"Failed to open/create database");
        }
    }
    self.title = @"Add new";
}

-(IBAction)addNote:(id)sender
{
    char *errMsg;
    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &noteDB)==SQLITE_OK) 
    {
        NSLog(@"we are here");
        NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO Notebook1(Whattime, Address, What, Who, Note) VALUES(\"%@\",\"%@\",\"%@\"\"%@\",\"%@\")", self.whenField.text, self.whenField.text, self.whenField.text,self.whenField.text, self.whenField.text];
        const char *insert_stmt = [insertSQL UTF8String];

        if (sqlite3_exec(noteDB, insert_stmt, NULL, NULL, &errMsg)==SQLITE_OK) 
        {
            self.whenField.text = @"";
            self.whereField.text = @"";
            self.whatField.text = @"";
            self.whoField.text = @"";
            self.noteView.text = @"";

            [self doAlert:@"add ok"];
        }
        else
        {
            NSLog(@"error: %s",errMsg);
            sqlite3_free(errMsg);
        }

        sqlite3_close(noteDB);
    }
}

在“viewDidLoad”中,程序创建了一个具有 6 个属性的 sqlite 表:ID、Whattime、Address、What、who、Note。在“addNote”中,我试图将一些数据插入到数据库中。但它没有这样做,日志显示:2013-10-12 05:10:49.328 Notebook[4266:a0b] error:4 values for 5 columns

4

2 回答 2

2

您的 sqlite 表有 6 个属性:ID、Whattime、Address、What、who、Note。并且您只插入 5 个值,即您没有插入 ID 的值,因为您也必须插入 ID 的值。如果您希望您的 ID 自动递增,请尝试以下操作:

-(IBAction)addNote:(id)sender
{
    NSString *dbFilePath =[DBclass getDBPath];

    sqlite3_stmt *addStmt = nil;
    if(sqlite3_open([dbFilePath UTF8String], &database)==SQLITE_OK)
    {

        const char *sql ="INSERT INTO Notebook1(ID,Whattime, Address, What, Who, Note) Values(?,?,?,?,?,?)";
        if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
            NSAssert1(0, @"Error while creating detail view statement. '%s'", sqlite3_errmsg(database));

        const char *insert_stmt = [insertSQL UTF8String];
        sqlite3_bind_int (addStmt, 1,   ID);
        sqlite3_bind_text(addStmt, 2,  [self.whenField.text UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(addStmt, 3,  [self.whenField.text UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(addStmt, 4,  [self.whenField.text UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(addStmt, 5,  [self.whenField.text UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(addStmt, 6,  [self.whenField.text UTF8String], -1, SQLITE_TRANSIENT);


        if(SQLITE_DONE != sqlite3_step(addStmt))
            NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
        else
            //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
            ID = sqlite3_last_insert_rowid(database);


        sqlite3_close(database);
    }

}
于 2013-10-12T07:19:12.460 回答
1

\"%@\",\"%@\",\"%@\"\"%@\",\"%@\"在此行中缺少逗号:

  NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO Notebook1(Whattime, Address, What, Who, Note) VALUES(\"%@\",\"%@\",\"%@\"\"%@\",\"%@\")", self.whenField.text, self.whenField.text, self.whenField.text,self.whenField.text, self.whenField.text];

它应该是这样的:

   NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO Notebook1(Whattime, Address, What, Who, Note) VALUES(\"%@\",\"%@\",\"%@\",\"%@\",\"%@\")", self.whenField.text, self.whenField.text, self.whenField.text,self.whenField.text, self.whenField.text];
于 2013-10-12T06:55:28.187 回答