家庭記賬本App_Fragment使用SQLite實現簡單儲存及查詢
阿新 • • 發佈:2021-06-21
AddFragment.java:
package com.example.familybooks; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.Toast; public class AddFragment extends Fragment { @Nullable String TAG="TAG"; private EditText costTitle,costMoney; private Button btn_go,btn_return; private DatePicker costDate; private SQLiteDatabase database; DatabaseHelper helper; @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){ View view=inflater.inflate(R.layout.activity_add,container,false); return view; } public void onActivityCreate(Bundle savedInstanceState){ super.onActivityCreated(savedInstanceState); costTitle=(EditText)getActivity().findViewById(R.id.et_costtitle); costMoney=(EditText)getActivity().findViewById(R.id.et_costmoney); costDate=(DatePicker) getActivity().findViewById(R.id.dp_costdate); helper=new DatabaseHelper(getActivity(),"account_cost",null,1); btn_go=(Button)getActivity().findViewById(R.id.btn_go); btn_go.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getActivity(),"accoun_cost",Toast.LENGTH_LONG).show(); database=helper.getWritableDatabase(); Insert(); costTitle.setText(""); //costDate.setText(""); costMoney.setText(""); Toast.makeText(getActivity(),"accoun_cost",Toast.LENGTH_LONG).show(); } }); btn_return=(Button)getActivity().findViewById(R.id.btn_return); btn_return.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { //得到資料庫 database=helper.getWritableDatabase(); //查詢資料 Query(); Toast.makeText(getActivity(),"accoun_cost",Toast.LENGTH_LONG).show(); } }); } public void Query(){ Cursor cursor=database.query("account_cost",null,null,null,null,null,null); if(cursor.moveToFirst()){ do{ String title=cursor.getString(cursor.getColumnIndex("title")); String date=cursor.getString(cursor.getColumnIndex("date")); String money=cursor.getString(cursor.getColumnIndex("money")); Log.i(TAG,"title:"+title); Log.i(TAG,"date:"+date); Log.i(TAG,"money:"+money); }while(cursor.moveToNext()); } cursor.close(); } public void Insert(){ ContentValues values=new ContentValues(); values.put("title",costTitle.getText().toString()); values.put("date",costTitle.getText().toString()); values.put("money",costMoney.getText().toString()); database.insert("account_cost",null,values); } }
DatabaseHelper.java:
package com.example.familybooks; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import androidx.annotation.Nullable; public class DatabaseHelper extends SQLiteOpenHelper { private Context context; public DatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) { super(context,"account_daily",null, 1); this.context=context; } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table diary(id integer PRIMARY KEY AUTOINCREMENT," +"costTitle text,costDate text,costMoney text)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("drop table if exists diary"); onCreate(db); } }
總結:
SQLiteOpenHelper是個抽象類,在該類中有如下兩個抽象方法,SQLiteOpenHelper的子類必須實現這兩個方法。
public abstract void onCreate(SQLiteDatabase db);
public abstract void onUpdate(SQLiteDatabase db,int oldVersion,int newVersion);
如果資料庫檔案不存在,只有oncreate()被呼叫(該方法在建立資料庫時被呼叫一次)。
如果資料庫檔案存在,會呼叫onupdate()方法升級資料庫,並更新版本號。