如何防止sqlite主键更新时自动递增?
所以,问题很简单。我正在更新到一个表,并且主键是自动递增的......这意味着所有相关的表都被搞砸了。我正在使用 sqlite,所以我认为没有任何固有的机制可以捕获此表和相关表。
由于我必须使用的 sqlite 版本中缺乏对外键的支持,因此在输入新数据时,我通过将从插入到父表中返回的主键添加到相关表中来强制建立关系。
每次更新时是否有其他方法可以更新所有相关表?请说是..
编辑:上次编辑是错误的,我很困惑。我只是粘贴了下面管理插入/替换的代码。我正在替换,而不是更新,我的输入错误。
//inserts
public long insertIntoCompany(String company, int subs) {
ContentValues cv = new ContentValues();
cv.put(company_column, company);
cv.put(company_subscribed_column, subs);
return db.replace(company_table, null, cv);
}
So, the issue is simple. I am updating into a table, and the primary key is autoincrementing... This means that all related tables are screwed over. I am using sqlite, so I don't think there is any inherent mechanism that can catch this and related tables.
Due to a lack of support for foreign keys in the sqlite versions I must work with, I force the relations by adding the primary key returned from an insert into the parent table into the related tables when entering new data.
Is there an alternative to updating all related tables each time I do an update? Please say yes..
Edit: last edit was wrong, I got confused. I have simply pasted the code governing inserts/replacements below. I am replacing into, not updating into, typing error on my part.
//inserts
public long insertIntoCompany(String company, int subs) {
ContentValues cv = new ContentValues();
cv.put(company_column, company);
cv.put(company_subscribed_column, subs);
return db.replace(company_table, null, cv);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您实际上应该使用 SQL
update
查询。请参阅此答案。如果您粘贴您的查询,我们也许能够提供更多帮助。
You should actually be using the SQL
update
query. See this answer.If you paste your query we might be able to offer more help.