Вопрос

I want to create a table called "movies" with two columns, "movieId (primary key and auto increment)" and "name (text)"

sql = [NSString stringWithFormat:
                     @"CREATE TABLE IF NOT EXISTS 'movies' ('movieId' " "INT AUTO INCREMENT PRIMARY KEY , 'name' VARCHAR(255) NULL)"];
 if (sqlite3_exec(db, [sql UTF8String],NULL, NULL, &err) !=SQLITE_OK) {
        sqlite3_close(db);
        NSAssert(0, @"Table failed to create : movies");
    }

And I am trying to insert some data

 sql = [NSString stringWithFormat:@"INSERT INTEGERO movies('name') VALUES('movie name')"];
    if (sqlite3_exec(db, [sql UTF8String],NULL, NULL, &err) !=SQLITE_OK) 
{ 
   sqlite3_close(db); 
   NSAssert(0, @"Insert fail");
}

and I am getting this:

2014-03-24 22:47:59.634 ProjectName[418:60b] *** Assertion failure in -[Data insertRecords], /Users/name/Objective-C Apps ios7/ProjectName/ProjectName/Data.m:108
2014-03-24 22:47:59.637 ProjectName[418:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Failed Insert'
*** First throw call stack:
(0x2d6e1fd3 0x3838bccf 0x2d6e1ead 0x2e08ed5b 0x19ef9 0x18eef 0xfc19 0x2ff024cb 0x2ff02289 0x2ff79525 0x2ff77ea5 0x2ff779f3 0x2ff7797b 0x2ff77913 0x2ff6ff89 0x2ff04127 0x2ff77661 0x2ff77125 0x2ff09065 0x2ff06847 0x2ff7035d 0x2ff6cfcd 0x2ff6758b 0x2ff03709 0x2ff02871 0x2ff66cc9 0x32549aed 0x325496d7 0x2d6acab7 0x2d6aca53 0x2d6ab227 0x2d615f4f 0x2d615d33 0x2ff65ef1 0x2ff6116d 0x22a1d 0x38898ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException

It works great if I remove "Auto Increment" from create table command. How I can create a table with auto increment columns and insert data?

Это было полезно?

Решение

You don't need to use Auto Increment.

sql = [NSString stringWithFormat:
                     @"CREATE TABLE IF NOT EXISTS 'movies' ('movieId' " "INTEGER PRIMARY KEY, 'name' VARCHAR(255) NULL)"];
 if (sqlite3_exec(db, [sql UTF8String],NULL, NULL, &err) !=SQLITE_OK) {
        sqlite3_close(db);
        NSAssert(0, @"Table failed to create : movies");
    }

And what is "INTEGERO"?

sql = [NSString stringWithFormat:@"INSERT INTO movies('movieId','name') VALUES(null, 'movie name')"];
    if (sqlite3_exec(db, [sql UTF8String],NULL, NULL, &err) !=SQLITE_OK) 
{ 
   sqlite3_close(db); 
   NSAssert(0, @"Insert fail");
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top