FMDB internal memory error

Based on a background other than SQL, I create a simple table called test with three fields:

NSString *testtable = @"create table if not exists test(testid integer primary key, userid integer, contentid text)"; if (![db executeUpdate:testtable]) { NSLog(@"create test, %d:%@", [db lastErrorCode], [db lastErrorMessage]); return NO; } 

when I insert a record into the table, I got an exc_bad_access error:

  if (![db executeUpdate:@"insert into test values(?,?,?)", NULL, 1, @"HELLO"]) { NSLog(@"insert test, %d:%@", [db lastErrorCode], [db lastErrorMessage]); return NO; } 

If I change the column type "userid" from integer to text, this will work well. And I am testing the table using the sqlite command on the console, it works too. Guys, what do you think? Thanks...

I will try another way to deal with this problem:

  if (![db executeUpdate:@"insert into test values(?,?,?)", NULL, [NSNumber numberWithInt:1], @"HELLO"]) { NSLog(@"testint, %d:%@", [db lastErrorCode], [db lastErrorMessage]); return NO; } 

then it works. I can’t say the reason ... Maybe fmdb only supports inserting an object.

+4
source share
2 answers

I believe FMDB needs objects to replace? and int are not objects. Change the value of 1 to [NSNumber numberWithInt:1]

Also, where do you open your database, do you have something like this?

 db = [FMDatabase databaseWithPath:writableDBPath]; if ([db open]) { 

Add [db setTraceExecution:TRUE]; right after that, and it will output your SQL text for you.

First you can create your own SQL NSString and insert your int, and then make FMDB just run your already filled string.

+2
source

executeUpdate: only supports objects passed as placeholder values. You can use executeQueryWithFormat: if you prefer printf style arguments (where you can use% d to represent your number). Personally, I stick with objects, though, since executeQueryWithFormat: ultimately converts the values ​​to objects.

0
source

Source: https://habr.com/ru/post/1412593/


All Articles