一、通过Android的API对sqlite数据库的操作
通过已有的ContentValues类,实例一个对象value来调用其中内部的方法来操作sqlite数据库
代码:
package com.example.databasedemo; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.util.Log; /** * 这个类用于对数据库的增删改查 */ public class Dao { private final DatabaseHelper mHelper; private static final String TAG="Dao"; public Dao(Context context){ //创建数据库 //Ctrl+Alt+F,之后加回车,创建成员变量 mHelper = new DatabaseHelper(context); } public void insert(){ SQLiteDatabase db = mHelper.getWritableDatabase(); // String sql="insert into "+Constants.TABLE_NAME+"(_id,name,age,salary,phone) values(?,?,?,?,?)"; // Object []obj={1,"haohao",20,10000}; // db.execSQL(sql,obj); ContentValues values =new ContentValues(); //插入数据 values.put("_id",2); values.put("name","me"); values.put("age",20); values.put("salary",5); db.insert(Constants.TABLE_NAME,null,values); db.close(); } public void delete(){ SQLiteDatabase db = mHelper.getWritableDatabase(); // String sql="delete from "+Constants.TABLE_NAME+" where name = ?"; // Object []obj={"hao"}; // db.execSQL(sql,obj); int result = db.delete(Constants.TABLE_NAME, null, null); Log.d(TAG,"result="+result); db.close(); } public void update(){ SQLiteDatabase db = mHelper.getWritableDatabase(); // String sql="update "+Constants.TABLE_NAME+" set salary= ? where name = ?"; // Object []obj={3,"hao"}; // db.execSQL(sql,obj); ContentValues values = new ContentValues(); values.put("salary",54321); db.update(Constants.TABLE_NAME,values,null,null); db.close(); } public void query(){ SQLiteDatabase db = mHelper.getWritableDatabase(); // String sql="select * from "+Constants.TABLE_NAME+" where name = ?"; // String []obj={"hao"}; // Cursor cursor = db.rawQuery(sql, obj); // while (cursor.moveToNext()){ // String name = cursor.getString(cursor.getColumnIndex("name")); // String age = cursor.getString(cursor.getColumnIndex("age")); // Log.d(TAG,"名字:"+name+"年龄:"+age); // } // cursor.close(); Cursor cursor = db.query(Constants.TABLE_NAME, null, null, null, null, null, null); while (cursor.moveToNext()){ int id = cursor.getInt(0); String name = cursor.getString(1); Log.d(TAG,"ID="+id+"name="+name); } cursor.close(); db.close(); } }
ContentValues
二、数据库事务
package com.example.transcationdemo; import androidx.appcompat.app.AppCompatActivity; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DatabaseHelper helper =new DatabaseHelper(this); helper.getWritableDatabase(); // toTestInsert(); toTestUpdate(); } public void toTestInsert(){ DatabaseHelper helper=new DatabaseHelper(getApplicationContext()); SQLiteDatabase db=helper.getWritableDatabase(); db.execSQL("insert into givemoney values( 1 , ‘company‘, 100000)"); db.execSQL("insert into givemoney values( 2 , ‘me‘, 0)"); db.close(); } public void toTestUpdate(){ DatabaseHelper helper =new DatabaseHelper(getApplicationContext()); SQLiteDatabase db=helper.getWritableDatabase(); db.beginTransaction(); try { db.execSQL("update givemoney set money = 100000-12000 where name = ‘company‘"); int a=10/0; db.execSQL("update givemoney set money = 12000 where name = ‘me‘"); db.setTransactionSuccessful(); } catch (SQLException e) { }finally { db.endTransaction(); db.close(); } } }
Transcation
两个特点
1、安全性
通过Try{}Catch{}的进行,保证一次性将Try中的内容全部执行,避免数据丢失和运算的错误。
2、高效性
事务:先将数据写入内存,然后一次写入数据库。
普通:打开数据库,存入数据,关闭数据库。
原文地址:https://www.cnblogs.com/suanai/p/12274977.html
时间: 2024-10-24 05:32:05