{"id":426918,"date":"2021-04-12T00:27:33","date_gmt":"2021-04-11T16:27:33","guid":{"rendered":"http:\/\/4563.org\/?p=426918"},"modified":"2021-04-12T00:27:33","modified_gmt":"2021-04-11T16:27:33","slug":"android-room-insertonconflict-onconflictstrategy-replace%e4%bc%9a%e9%87%8d%e6%96%b0%e7%94%9f%e6%88%90-id-%e5%90%97-%e4%bb%80%e4%b9%88%e6%83%85%e5%86%b5%e4%b8%8b%e4%bc%9a%e9%87%8d%e6%96%b0","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=426918","title":{"rendered":"android Room @Insert(onConflict = OnConflictStrategy.REPLACE)\u4f1a\u91cd\u65b0\u751f\u6210 id \u5417? \u4ec0\u4e48\u60c5\u51b5\u4e0b\u4f1a\u91cd\u65b0\u751f\u6210 id?"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  android Room @Insert(onConflict = OnConflictStrategy.REPLACE)\u4f1a\u91cd\u65b0\u751f\u6210 id \u5417? \u4ec0\u4e48\u60c5\u51b5\u4e0b\u4f1a\u91cd\u65b0\u751f\u6210 id?               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wanlion <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p><b>1 User \u7c7b<\/b><br \/> \u53ea\u6709 3 \u4e2a\u5b57\u6bb5\uff1aid, username, password<br \/> \u5176\u4e2d id \u662f\u4e3b\u952e, \u5e76\u4e14\u662f\u81ea\u589e\u957f <\/p>\n<p> <b>2 dao \u7c7b<\/b><br \/> \u4f7f\u7528 @Insert \u6ce8\u89e3\u5982\u4e0b:<br \/> @Insert(onConflict = OnConflictStrategy.REPLACE)<br \/> public long saveUser(User user);<\/p>\n<p> <b>3 \u8c03\u7528<\/b><br \/> \u4e0d\u7ba1\u662f\u65b0\u589e\u8fd8\u662f\u4fee\u6539\uff0c \u5176\u4ed6\u5730\u65b9\u90fd\u662f\u8c03\u7528\u8be5\u65b9\u6cd5:<br \/> dao.saveUser(user);<\/p>\n<p><b>4 \u95ee\u9898<\/b><br \/> \u7136\u540e, \u53d1\u73b0\u4e00\u4e2a\u6709\u8da3\u7684\u73b0\u8c61, \u6709\u65f6\u5019\u5f15\u7528 user \u65f6\u4f1a\u5931\u6548, \u67e5\u770b\u6570\u636e\u5e93\u65f6\u53d1\u73b0, \u6307\u5b9a id \u7684\u6570\u636e\u5df2\u7ecf\u4e0d\u5b58\u5728\u3002<\/p>\n<p> <b>5 \u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u6837? <\/b><br \/> \u6765\u81ea stackoverflow \u7684\u89e3\u91ca:<br \/> @Insert(onConflict = OnConflictStrategy.REPLACE) \u6ce8\u89e3, \u5982\u679c\u6307\u5b9a id \u7684\u5bf9\u8c61\u6ca1\u6709\u4fdd\u5b58\u5728\u6570\u636e\u5e93\u4e2d, \u5c31\u4f1a\u65b0\u589e\u4e00\u6761\u6570\u636e\u5230\u6570\u636e\u5e93\u3002\u5982\u679c\u6307\u5b9a id \u7684\u5bf9\u8c61\u6570\u636e\u5df2\u7ecf\u4fdd\u5b58\u5230\u6570\u636e\u5e93\u4e2d, \u5c31\u4f1a\u5220\u9664\u6389\u539f\u6765\u7684\u6570\u636e, \u7136\u540e\u65b0\u589e\u4e00\u6761\u6570\u636e\u3002<\/p>\n<p> \u8fd9\u6837\u5c31\u89e3\u91ca\u4e86\u4e3a\u4ec0\u4e48\u5f15\u7528 user \u4f1a\u5931\u6548\u3002<br \/> \u539f\u8bdd\u5982\u4e0b:<br \/> A method, annotated with @Insert can return a long. This is the newly generated ID for the inserted row. A method, annotated with @Update can return an int. This is the number of updated rows.<br \/> update will try to update all your fields using the value of the primary key in a where clause. If your entity is not persisted in the database yet, the update query will not be able to find a row and will not update anything.<br \/> You can use @Insert(onConflict = OnConflictStrategy.REPLACE). This will try to insert the entity and, if there is an existing row that has the same ID value, it will delete it and replace it with the entity you are trying to insert. Be aware that, if you are using auto generated IDs, this means that the the resulting row will have a different ID than the original that was replaced. If you want to preserve the ID, then you have to come up with a custom way to do it.<\/p>\n<p> \u6765\u6e90:<br \/> https:\/\/stackoverflow.com\/questions\/48519896\/room-update-or-insert-if-not-exist-rows-and-return-count-changed-rows<\/p>\n<p> <b>6 \u7591\u60d1:<\/b><br \/> \u4f46\u662f, \u4e8b\u60c5\u603b\u662f\u6709\u4f46\u662f, \u7ecf\u8fc7\u6d4b\u8bd5\u53d1\u73b0, \u4fee\u6539\u4e86 username \u6216\u8005 password, id \u5e76\u6ca1\u6709\u6539\u53d8\u3002<\/p>\n<p> \u8fd9\u662f\u600e\u4e48\u56de\u4e8b? id \u7684\u6539\u53d8\u6709\u6761\u4ef6\u5462? \u8fd8\u662f\u7248\u672c\u4e0d\u540c\u624d\u5f15\u8d77\u6d4b\u8bd5\u7ed3\u679c\u4e0d\u540c?<br \/> \u54ea\u4f4d v \u53cb\u80fd\u5426\u89e3\u91ca\u4e0b?<\/p>\n<p> \u6280\u672f: android Room<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>2<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5734821\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Cabana <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f20\u8fdb\u65b9\u6cd5 saveUser \u4e2d\u7684 user id \u5b57\u6bb5\u6709\u6ca1\u6709\u503c\uff0c\u662f\u662f\u5426\u89e6\u53d1 onConflict \u7684\u5148\u51b3\u6761\u4ef6\u3002<\/p>\n<p>\u53e6\u5916 insert \u548c update \u4e0d\u8981\u6df7\u7528                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5734822\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : wanlion <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Cabana \u4fee\u6539\u65f6\u6709\u503c, \u800c\u4e14\u4fee\u6539\u6210\u529f\uff01                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>android Room @Ins&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/426918"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=426918"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/426918\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=426918"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=426918"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=426918"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}