經過Android的API對Sqlite數據庫進行操做

1、增刪改查java

android

 

 改sql

 

 查數據庫

 

 刪安全

這是刪除以前spa

 

 

 

 刪除三條code

 

 Dao.javasqlite

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,"zzw",19,2,1234567890};
//        db.execSQL(sql,obj);
        ContentValues values  =new ContentValues();
        //插入數據
        values.put("_id",2);
        values.put("name","zs");
        values.put("age",23);
        values.put("salary",5);
        values.put("phone",12345);
        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={"zzw"};
//        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,"zzw"};
//        db.execSQL(sql,obj);
        ContentValues values = new ContentValues();
        values.put("phone",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={"zzw"};
//        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();
    }
}

 

2、數據庫事務blog

兩個特色事務

一、安全性

經過Try{}Catch{}的進行,保證一次性將Try中的部分所有執行,避免數據丟失。

二、高效性

能夠在短期內將大量數據先寫入內存,而後一次寫入數據庫。

相關文章
相關標籤/搜索