SoFunction
Updated on 2025-04-07

Bank transfer examples of transaction operation methods in Android database

This article describes the bank transfer function of transaction operation methods in Android database. Share it for your reference, as follows:

Main java

package ;
import ;
import ;
import ;
import ;
import ;
import ;
public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  (savedInstanceState);
  setContentView(.activity_main);
 }
 //Click the button to execute this method public void transtation(View v){
  //1. Create an object of help class  BankOpenHelper bankOpenHelper = new BankOpenHelper(this);
  //2. Call the getReadableDatabase of the database help class object to create the database, initialize the table data, and get a SqliteDatabase object to transfer (sql statement)  SQLiteDatabase db = ();
  //3. Transfer money, reduce Li Si's money by 200, and Zhang San adds 200.  ();//Open a database transaction  try {
   ("update account set money= money-200 where name=?",new String[]{"Li Si"});
   int i = 100/0;//Simulate an exception   ("update account set money= money+200 where name=?",new String[]{"Zhang San"});
   ();//All SQL statements in the mark transaction are executed successfully  } finally {
   ();//Judge whether the transaction's tag is successful. If it is not successful, roll back the SQL statement executed before the error  }
 }
}

It is best to create a package yourself to write the database class

package ;
import ;
import ;
import ;
import ;
public class BankOpenHelper extends SQLiteOpenHelper {
 public BankOpenHelper(Context context) {
  super(context, "", null, 1);
  // TODO Auto-generated constructor stub
 }
 @Override
 public void onCreate(SQLiteDatabase db) {
  ("create table account (_id integer primary key autoincrement,name varchar(20),money varchar(20))");
  ("insert into account ('name','money') values ('Zhang San','2000')");
  ("insert into account ('name','money') values ('Li Si','5000')");
 }
 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  // TODO Auto-generated method stub
 }
}

xml  A button click event, very special

<?xml version="1.0"?>
-<RelativeLayout tools:context=".MainActivity" android:paddingTop="@dimen/activity_vertical_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="/tools" xmlns:andro>
<Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/transtation" android:onClick="transtation" android:layout_centerInParent="true"/>
</RelativeLayout>

Attached:

Database transaction description:

Transaction: Execute multiple SQL statements, either successfully executed at the same time or failed at the same time. Some cannot succeed and some fail.

Bank transfer

//Click the button to execute this methodpublic void transtation(View v){
 //1. Create an object of help class BankOpenHelper bankOpenHelper = new BankOpenHelper(this);
 //2. Call the getReadableDatabase of the database help class object to create the database, initialize the table data, and get a SqliteDatabase object to transfer (sql statement) SQLiteDatabase db = ();
 //3. Transfer money, reduce Li Si's money by 200, and Zhang San adds 200. ();//Open a database transaction try {
  ("update account set money= money-200 where name=?",new String[]{"Li Si"});
  int i = 100/0;//Simulate an exception  ("update account set money= money+200 where name=?",new String[]{"Zhang San"});
  ();//All SQL statements in the mark transaction are executed successfully } finally {
  ();//Judge whether the transaction's tag is successful. If it is not successful, roll back the SQL statement executed before the error }
}

For more information about Android related content, please check out the topic of this site:Android database operation skills summary》、《Summary of Android's SQLite database skills》、《Android programming activity operation skills summary》、《Android file operation skills summary》、《Android development introduction and advanced tutorial》、《Android resource operation skills summary》、《Android View View Tips Summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.