Android SQLite method tutorial for setting primary key self growth

Today, a function of adding items is added to the app. The primary key of the item is an integer. Let it grow by itself.

Since you want to self grow, you don't need to assign a value to the ID field in the code. However, when debugging, it is found that the primary key cannot be repeated. After observing the output, if the value is not assigned, the ID defaults to 0. Therefore, it is repeated every time it is 0:

ID has no assignment. Why is it 0? That is, the default int of Java is 0. Therefore, at the object level, even if there is no assignment, the ID is 0 by default.

Therefore, the following problems should be solved at the level of database operation:

When assigning contentvalue, you can cancel the assignment of ID field.

The problem is solved and can grow by itself.

summary

In Android, the conditions for self growth of SQLite primary key are as follows:

1. In the database, set the fields to integer, primary and autoincrement.

2. In Java code, the primary key field is ignored when assigning contentvalue.

Well, the above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>