---恢复内容开始---
记账本小程序两天时间尝试使用xml文件进行数据的存储,但实际操作发现不可行,所以果断放弃采用Sqlite数据库。
- 数据库的创建
- 定义一个数据库的帮助类MyDataBaseOpenHelper extends SQLiteOpenHelper
- 指定数据库的信息
通过helper得带一个可写,可读或可写且可读的数据库,数据可才会被创建
package com.fmd.database; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteDatabase.OpenParams; import android.database.sqlite.SQLiteOpenHelper; public class MyDataBaseOpenHelper extends SQLiteOpenHelper { public MyDataBaseOpenHelper(Context context) { super(context, "test.db", null, 1); } } //这句代码执行完毕数据库不会创建 MyDataBaseOpenHelper helper= new MyDataBaseOpenHelper(this); //下面这行代码执行后数据库才会被创建 helper.getWritableDatabase();
- 数据库的增删改查
- 增: insert into tally (title,date,money) values ("花销",“2019-1-13”,“110”)
- 删: delete from tally where title = "花销"
- 改: update tally set money = "100"
- 查: select * from tally where title = ?
MainActivity,java
package com.fmd.database; import java.util.List; import com.fmd.database.dao.TallyDao; import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private EditText rg_title; private EditText rg_date; private EditText rg_money; private TallyDao dao; private LinearLayout ll_result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //这句代码执行完毕数据库不会创建 MyDataBaseOpenHelper helper= new MyDataBaseOpenHelper(this); //下面这行代码执行后数据库才会被创建 helper.getWritableDatabase(); dao = new TallyDao(this); ll_result = findViewById(R.id.ll_result) rg_title = findViewById(R.id.rg_title); rg_date = findViewById(R.id.rg_date); rg_money = findViewById(R.id.rg_money); } public void save(View view) { String title = rg_title.getText().toString().trim(); if(TextUtils.isEmpty(title)) { Toast.makeText(this, "请输入收支的标题", 0).show(); return; } String date = rg_date.getText().toString().trim(); if(TextUtils.isEmpty(date)) { Toast.makeText(this, "请输入收支的日期", 0).show(); return; } String money = rg_money.getText().toString().trim(); if(TextUtils.isEmpty(money)) { Toast.makeText(this, "请输入收支的金额", 0).show(); return; } dao.add(title, date, money); Toast.makeText(this, "数据添加成功", 0).show(); List<Tally> tallys = dao.findAll(); ll_result.removeAllViews(); for(Tally tally:tallys) { TextView tv = new TextView(this); tv.setText(tally.toString()); ll_result.addView(tv); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
TallyDBOpenHelper.java
package com.fmd.tallybook_2.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class TallyDBOpenHelper extends SQLiteOpenHelper { public TallyDBOpenHelper(Context context) { super(context, "info.db", null, 1); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("creat table tally (_id intager primary key autoincrement,title varchar(20),date varchar(20),money varchar(20) )"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } }
TallyDao.java
package com.fmd.tallybook_2.db.dao; import java.util.ArrayList; import java.util.List; import com.fmd.tallybook_2.db.TallyDBOpenHelper; import com.fmd.tallybook_2.domain.Tally; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; public class TallyDao { private TallyDBOpenHelper helper; //只有一个有参的构造方法,要求必须传入上下文 public TallyDao(Context context) { helper = new TallyDBOpenHelper(context); } //添加一个记录 public void add(String title,String date,String money) { SQLiteDatabase db = helper.getWritableDatabase(); db.execSQL("insert into tally (title,date,money) values (?,?,?)",new Object[] {title,date,money}); db.close(); } //删除一个记录 public void delete(String title) { SQLiteDatabase db = helper.getWritableDatabase(); db.execSQL("delete from tally where title = ?",new Object[] {title}); db.close(); } //修改一个记录 public void update(String title,String newdate,String newmoney) { SQLiteDatabase db = helper.getWritableDatabase(); db.execSQL("update tally set date = ?",new Object[] {newdate,title,newmoney}); db.close(); } //查询一条记录 public String find(String title) { String date = null; String money = null; SQLiteDatabase db = helper.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from tally where title = ?",new String[] {title}); boolean result = cursor.moveToNext(); if(result) { date = cursor.getString(0); money = cursor.getString(0); } cursor.close(); db.close(); return date+money; } //获取全部的账单信息 public List<Tally> fidnAll(){ List<Tally> tallys = new ArrayList<Tally>(); SQLiteDatabase db = helper.getReadableDatabase(); Cursor cursor = db.rawQuery("select title, date, money from tally", null); while(cursor.moveToNext()) { String title = cursor.getString(0); String date = cursor.getString(1); String money = cursor.getString(2); Tally tally = new Tally(); tally.setTitle(title); tally.setDate(date); tally.setMoney(money); tallys.add(tally); } cursor.close(); db.close(); return tallys; } }
Tally.java
package com.fmd.tallybook_2.domain; public class Tally { private String title; private String date; private String money; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getMoney() { return money; } public void setMoney(String money) { this.money = money; } @Override public String toString() { return "Tally [title=" + title + ", date=" + date + ", money=" + money + "]"; } }
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.fmd.database.MainActivity" > <EditText android:id="@+id/rg_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请输入Title"/> <EditText android:id="@+id/rg_date" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请输入Date"/> <EditText android:id="@+id/rg_money" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请输入Money"/> <Button android:onClick="save" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="保存" /> <LinearLayout android:id="@+id/ll_result" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> </LinearLayout> </LinearLayout>
---恢复内容结束---