简体   繁体   English

FMDB自动插入

[英]FMDB Insert into with AutoIncrement

I'm trying to figure this out since a while but I can't seem to find a solution. 一段时间以来,我一直在试图找出答案,但似乎找不到解决方案。 I have a table that was created as: 我有一个表创建为:

 [db executeUpdate:@"CREATE TABLE 'MEDIA' (identifier INTEGER PRIMARY KEY AUTOINCREMENT, taskIdentifier INTEGER, type INTEGER, data BLOB)"]

How am I supposed to insert a record into this table? 我应该如何在此表中插入一条记录? I tried this: 我尝试了这个:

 [db executeUpdate:@"INSERT INTO 'MEDIA' VALUES($next_id,?,?,?)" withArgumentsInArray:arguments];

whereas arguments is an NSArray with 4 values, the first being a dummy value (since it should autoincrement itself) but to no avail. 而参数是一个具有4个值的NSArray,第一个是虚拟值(因为它应该自动递增),但无济于事。 How is this supposed to work with FMDB? FMDB应该如何使用?

You need to ask yourself if you really want to have the AUTOINCREMENT key word there. 您需要问自己是否真的要在其中使用AUTOINCREMENT关键字。 AUTOINCREMENT together with INTEGER PRIMARY KEY brings very specific behavior, which is described here . AUTOINCREMENT与INTEGER PRIMARY KEY一起带来了非常具体的行为,这说明在这里 It is very likely that you don't need it, in which case just leave INTEGER PRIMARY KEY in the column definition. 您很有可能不需要它,在这种情况下,只需将INTEGER PRIMARY KEY留在列定义中即可。 Now, columns defined as INTEGER PRIMARY KEY really become an alias of ROWID, so you have 2 options: 现在,定义为INTEGER PRIMARY KEY的列实际上成为ROWID的别名,因此您有2个选择:

  • omit the value for this column in the INSERT statement. 在INSERT语句中忽略此列的值。 This will automatically insert the next available value there. 这将自动在此插入下一个可用值。 From the docs: The usual algorithm is to give the newly created row a ROWID that is one larger than the largest ROWID in the table prior to the insert. 来自文档: The usual algorithm is to give the newly created row a ROWID that is one larger than the largest ROWID in the table prior to the insert. So your INSERT would look like this: [db executeUpdate:@"INSERT INTO 'MEDIA' VALUES(?,?,?)" withArgumentsInArray:arguments]; 因此,您的INSERT将如下所示: [db executeUpdate:@"INSERT INTO 'MEDIA' VALUES(?,?,?)" withArgumentsInArray:arguments]; but the arguments array would only contain 3 values for taskIdentifier , type , and data . 但是arguments数组将只包含taskIdentifiertypedata的 3个值。

  • provide the value for identifier column like this: [db executeUpdate:@"INSERT INTO 'MEDIA' VALUES(?,?,?,?)" withArgumentsInArray:arguments]; 提供标识符列的值,例如: [db executeUpdate:@"INSERT INTO 'MEDIA' VALUES(?,?,?,?)" withArgumentsInArray:arguments]; with the arguments array containing the identifier as NSNumber on index 0. 参数数组包含索引为0的标识符NSNumber

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

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