简体   繁体   English

ios:使用FMDB插入sqlite表

[英]ios: insert into sqlite table using FMDB

I am trying to create and insert into a table that is created on the way(dynamic table name). 我正在尝试创建并插入到路上创建的表(动态表名称)。 I am able to create the table, but can not insert into it. 我能够创建表,但不能插入其中。 I am using the code that is given below, every time I try, success = 0. 我正在使用下面给出的代码,每次尝试时,success = 0。

BOOL success =  [db executeUpdate:@"INSERT INTO ? (fname, lname,) VALUES
(?,?);",business.tablename,businessType.fname, businessType.lname, nil];
NSLog(@"success=%d", success);

I have been trying for a long time, please provide me with an example for inserting in table with table name in variable. 我已经尝试了很长时间,请提供一个示例,用于在表中插入变量中的表名。

Try like this 试试这样吧

NSString *insertQuery = [NSString stringWithFormat:@"INSERT INTO %@ (fname, lname,) VALUES (?,?);",business.tablename];
BOOL success = [db executeUpdate:insertQuery,businessType.fname, businessType.lname, nil];

Hope this helps 希望这可以帮助

NSString *insertQuery = [NSString stringWithFormat:@"INSERT INTO %@ (firstName, lastName,) VALUES ('James','Bond');",_tableName];
FMDatabase * userDB = [self openDB:[appDel getDBPath]];
if(userDB){

 [userDB beginTransaction];
 [userDB executeUpdate:insertQuery];
 [userDB  commit];
 [userDB close];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM