1

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?

8
  • What does sqlite3_errmsg report????? Commented Mar 24, 2014 at 20:59
  • 1
    Your first sql = ... statement does not even compile with these two quotation marks. Plus there is no reason for using stringWithFormat. You can simply go for sql = @"..."; So chieck you rode and then mend the question. Commented Mar 24, 2014 at 21:01
  • You realize, of course that your CREATE TABLE string is getting broken in the middle. Commented Mar 24, 2014 at 21:01
  • 1
    I think it's spelled AUTOINCREMENT. Also, as a side note, I'd suggest allowing core data to handle the database. Commented Mar 24, 2014 at 21:01
  • 1
    Do not use stringWithFormat. It will fail when a value has a quote or other special character. Do it properly and avoid SQL injection attacks and other general errors. Commented Mar 24, 2014 at 21:14

1 Answer 1

1

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");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.