When beginners are learning Android, it's a bit complicated to see how SQLite database programming is on the books.
The difficulty of the beginner stage:
Set Up Database table field (field) data type (text,interge) Select more, consider more.
Data update inserts in activity have a lot of constraints to consider
It's a bit cumbersome to always have access to the data in the database, declaring a cursor in the activity every time and getting cursor through a lot of code.
The solutions to these difficulties are as follows
1. Data type with text (except ID number)
2. Both inserts or updates are implemented in the activity in the same function in the DB file, and there is no need to consider any factors in the activity (whether there is already data in the datasheet that is currently being inserted or updated)
3. Write a function that returns a value of cursor in the DB file (the returned cursor is a common cursor)
Some of the principles to know
1 db.update (TABLE_NAME, CV, where, wherevalues) if the update data fails to return 0
2 db.update (TABLE_NAME, CV, where, wherevalues) update all values in the table that conform to Where=wherevalue
3 If you want to query the value or to update the value has more than one keyword constraint, then you can fill in as follows where and where
String where = field1+ "=? and "+ field2 +" =? and "+ field3 +" =? ";
String[] Wherevalues = {string1, string2, string3};
Cursor Cursor = db.query (hstrybuytable, NULL, where, wherevalues, NULL, NULL, NULL);
Db.update (hstrybuytable, CV, where, wherevalues);
4) return Db.insert (hstrybuytable, NULL, CV) inserts data into the database as soon as it is invoked
Scheme:
1 when building database tables, please default AutoNumber and other field data types are text, (string types in Java are easy to convert to other data types)
public void OnCreate (Sqlitedatabase db) {
String sql = "CREATE TABLE" +youtablename+ "("
+table_id+ "Integer Prima Ry Key AutoIncrement, "
+table_field1+" text, "
+table_field2+" text, "
+table_field3+" text, "
+ table_field4+ "text)";
Db.execsql (SQL);
2 in helpdb to write a function to replace Insrtdb and UPDATADB, later in the other place directly to use is, don't think so much, directly can use this function is the key to the UPDATE function where statement written correctly (here the correct view of the project requirements).
public int Operatehproduct (string string1, string string2, Product string3) {
int i = 0;
if (updatehproduct (string1, string2, string3) = = 0) {//attempt to update data
i = 1;
Inserthproduct (string1, string2, string3);
}
return i;
}