dimanche 19 avril 2015

iOS) fmdb : creat table and insert

I made it to create table but failed to insert.

when my app is launched first time, it creates table usercart :

- (void)createDatabaseStructure{
if ([db open]) {
NSString *create_table =@"create table if not exists usercart ('id' integer,'productid' integer,'name' text NOT NULL,'description' text NOT NULL,'campaignid' integer NOT NULL,'startdate' date NULL,'enddate' date NULL,'martid' integer NOT NULL,'martname' text NOT NULL,'price' integer NOT NULL,'update' date NOT NULL)";
if ([db executeUpdate:create_table]) {
NSLog(@"created table");
[db close];

And I made it so NSLog shows me "created table"

and when i clicked a button, it inserts an item:

- (void)insertUserCartItem:(HMUserCartItem*)cartItem{
if ([db open]) {
if ([db executeUpdate:@"INSERT INTO usercart ('id', 'productid', 'name', 'description', 'campaignid', 'startdate', 'enddate', 'martid', 'martname', 'price', 'update') VALUES (?,?,?,?,?,?,?,?,?,?,?)" withParameterDictionary:[cartItem SQLDictionary]]) {
NSLog(@"insert at table success!");
[db close];

- (NSDictionary*)SQLDictionary{
return @{
@"id":[NSNumber numberWithUnsignedInteger:self.databaseId],
@"productid":[NSNumber numberWithUnsignedInteger:self.itemId],
@"campaignid":[NSNumber numberWithUnsignedInteger:self.campaignId],
@"martid":[NSNumber numberWithUnsignedInteger:self.martId],
@"price":[NSNumber numberWithUnsignedInteger:self.itemPrice],

but finally, [db executeUpdate:@"INSERT ~] returns false and log show me :

Could not find index for martid
Could not find index for martname
Could not find index for update
Could not find index for id
Could not find index for campaignid
Could not find index for price
Could not find index for startdate
Could not find index for enddate
Could not find index for description
Could not find index for name
Could not find index for productid
Error: the bind count (0) is not correct for the # of variables in the query (11) (INSERT INTO usercart ('id', 'productid', 'name', 'description', 'campaignid', 'startdate', 'enddate', 'martid', 'martname', 'price', 'update') VALUES (?,?,?,?,?,?,?,?,?,?,?)) (executeUpdate)
Error Domain=FMDatabase Code=0 "not an error" UserInfo=0x174a6b080 {NSLocalizedDescription=not an error} // NSLog(@"%@",db.lastError);
not an error // from NSLog(@"%@",db.lastErrorMessage);

db.lastError and db.lastErrorMessage tells me 'not an error' I don't know what I am missing. And actually i don't understand why 'Could not find index for' order is from martid to productid(in dictionary it starts with id and finally goes to update)

please help me!

Aucun commentaire:

Enregistrer un commentaire