First, modify the table structure of the database (update database version).
1, in the Personsqliteopenhelper class, the Personsqliteopenhelper constructor method needs to pass in 4 parameters, the last one is the database version. The Onupgrade (sqlitedatabase db, int oldversion, int newversion) method in the Personsqliteopenhelper class is called when the version value changes (can only be incremented).
2. Use the Execute SQL statement in the Onupgrade method to change the table structure of the database. Using the Execsql (SQL) method of the Sqlitedatabase object to modify the table structure of the database, parameter SQL is the SQL statement that needs to be executed.
3. The SQL statement that adds a column to the table is: ALTER table person ADD account varchar (20).
The table structure adds a column of code as follows:
1 Public void int int arg2) {2 db.execsql ("ALTER TABLE person add account varchar"); 3 }
View Code
II. Transaction of the database
1. Why do I need a business?
A transaction is guaranteed when multiple operations succeed at the same time or fail simultaneously. For example, bank transfer: A transfer to B, you need to guarantee a reduction in account a, but also to ensure that the amount in the B account increases. Take the example of analog transfer. Therefore, the table structure of the database needs to be modified
2. Set up a business.
①.new out a Personsqliteopenhelper object helper;
②. Obtain a writable Sqlitedatabase (database) object db from the Getwritabledatabase () method of the Personsqliteopenhelper object;
③. Open a transaction by using the BeginTransaction () method of the Sqlitedatabase (database) object (the specific wording of the transaction already has an instance in the description document of the method: Using a try...finally logic to write the transaction);
④. Write the logic that needs to be executed in try, and at the end of the try code block execute the Sqlitedatabase (database) object's Settransactionsuccessful () method, which is the token database transaction execution succeeds, If you do not have this code, execution fails by default, and the data is rolled back.
⑤. Execute the Endtransaction () method of the Sqlitedatabase (database) object in the finally code block, and close the database object. Alternatively, you can add a catch code block after the try code block to catch various exceptions.
The database transaction code is as follows:
1 Public voidTesttransecation ()throwsException {2Personsqliteopenhelper helper =NewPersonsqliteopenhelper (GetContext ());3Sqlitedatabase db =helper.getwritabledatabase ();4 db.begintransaction ();5 Try {6Db.execsql ("update person Set account = account-1000 where name =?"),7 NewObject[] {"Zhangsan" });8 9Db.execsql ("update person Set account = account + + where name =?"),Ten NewObject[] {"Wangwu" }); One db.settransactionsuccessful (); A}Catch(Exception e) { - - } the finally { - db.endtransaction (); - db.close (); - } +}
View Code
Android Learning Notes Database (SQLite) (ii)