Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: insertOrReplaceObject与isAutoIncrement的语义 #63

Closed
LongTimeNoSeee opened this issue Jun 21, 2017 · 15 comments
Closed

Refactor: insertOrReplaceObject与isAutoIncrement的语义 #63

LongTimeNoSeee opened this issue Jun 21, 2017 · 15 comments

Comments

@LongTimeNoSeee
Copy link

在数据库对应的模型里面代码:
WCDB_SYNTHESIZE_COLUMN(Student, snu, "s_number")

WCDB_PRIMARY_AUTO_INCREMENT(Student, snu)

为什么在数据库中,没有显示主键的自动增长?

@RingoD
Copy link
Collaborator

RingoD commented Jun 21, 2017

自增的主键也可以用不自增的方式插入的,因此需设置isAutoIncrement才是自增。
参考:https://github.com/Tencent/wcdb/wiki/基础类、CRUD与Transaction#主键自增auto-increment

@LongTimeNoSeee
Copy link
Author

好的,在主键设置isAutoIncrement = yes后,记录已存在后,需要更新,但是调用insertOrReplaceObject方法后,依然会插入一行记录,请问这个问题怎么解决?

@RingoD
Copy link
Collaborator

RingoD commented Jun 22, 2017

insertOrReplaceObject对于不存在的会进行插入,已存在的则是覆盖。请参考:http://www.sqlite.org/lang_insert.html

@LongTimeNoSeee
Copy link
Author

对插入的对象,当对象的主键在数据库中已存在,但是在设置isAutoIncrement = yes后,依然会插入一条新的记录

@LongTimeNoSeee
Copy link
Author

这样不会起到更新的作用

@RingoD
Copy link
Collaborator

RingoD commented Jun 22, 2017

自增不会管当前对象的主键,相当于INSERT INTO myTable (NULL, "value");
需要自增就设置自增,不需要则不使用。
你的需求可能是“当主键不存在时,插入一条自增的数据;当主键存在时,则不做改动”,SQLite并没有直接提供这样的功能。

你可以用lastInsertedRowID的方法拿到上一次插入的id来实现,参考:https://github.com/Tencent/wcdb/wiki/基础类、CRUD与Transaction#主键自增auto-increment

@LongTimeNoSeee
Copy link
Author

好的,可能你没有理解我说的意思,我这边再测试测试。谢谢回答

@RingoD
Copy link
Collaborator

RingoD commented Jun 22, 2017

如果是更新,则设置isAutoIncrement为NO,并用lastInsertedRowID赋值到主键后再操作

@LongTimeNoSeee
Copy link
Author

意思就是使用insertOrReplaceObject方法时,如果要实现更新,就必须要把isAutoIncrement设置成NO是吧

@LongTimeNoSeee
Copy link
Author

insertOrReplaceObject实现插入的时候,再把isAutoIncrement设置成YES?

@RingoD
Copy link
Collaborator

RingoD commented Jun 22, 2017

对。
这里存在一个小问题,实际上insertOrReplaceObject是插入或替换,与isAutoIncrement的插入并自增其实是存在矛盾的。我后续看下怎么改。

@RingoD RingoD added the bug label Jun 22, 2017
@LongTimeNoSeee
Copy link
Author

好的👌

@RingoD RingoD self-assigned this Jun 22, 2017
@RingoD RingoD added enhancement and removed bug labels Jun 22, 2017
@RingoD RingoD changed the title iOS 主键自动增长的问题 Refactor: insertOrReplaceObjectisAutoIncrement的语义 Jun 22, 2017
@RingoD RingoD changed the title Refactor: insertOrReplaceObjectisAutoIncrement的语义 Refactor: insertOrReplaceObject与isAutoIncrement的语义 Jun 22, 2017
@RingoD
Copy link
Collaborator

RingoD commented Jun 30, 2017

19ebd28

@RingoD RingoD closed this as completed Jun 30, 2017
@ymsheng
Copy link

ymsheng commented Apr 20, 2018

@LongTimeNoSeee 在RingoD的2017年30号修改吗,是否解决了主键自增与insertOrReplaceObject冲突的问题,目前我pod引用最新的代码还是只能插入一条数据。

@RingoD
Copy link
Collaborator

RingoD commented Apr 20, 2018

@ymsheng Do not reply on a closed issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants