安卓开发五——创建数据库和增加数据
package com.example.myapplication; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBHelper extends SQLiteOpenHelper { private static int DB_VERSION = 1; private static String DB_NAME = "account_daily2.db"; public DBHelper(Context context) { super(context, DB_NAME ,null, DB_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String sql="create table account2(_id integer primary key autoincrement," +//主键 "Se varchar(20),"+// "Title varchar(20)," +//Title "Date varchar(20)," +//Date "Money vaechar(20))";//Money db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } }
我们要存入数据首先得有对象和视图中的数据绑定在一起。
之后我们在点击时需要将获取的数据存入数据库中。
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.ContentValues; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.text.Spanned; import android.view.Gravity; import android.view.View; import android.widget.DatePicker; import android.widget.EditText; import android.widget.Spinner; import android.widget.Toast; import java.text.SimpleDateFormat; import java.util.Date; public class new_cost extends AppCompatActivity { private DBHelper helper; private Spinner et_cost_se; private EditText et_cost_title; private EditText et_cost_money; private DatePicker dp_cost_date; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_new_cost); initView(); } private void initView() { helper = new DBHelper(new_cost.this); et_cost_se=findViewById(R.id.spin); et_cost_title = findViewById(R.id.et_cost_title); et_cost_money = findViewById(R.id.et_cost_money); dp_cost_date = findViewById(R.id.dp_cost_date); } public void okButton(View view) { String seStr=et_cost_se.getSelectedItem().toString(); String titleStr = et_cost_title.getText().toString().trim(); String moneyStr = et_cost_money.getText().toString().trim(); String dateStr = dp_cost_date.getYear() + "-" + (dp_cost_date.getMonth() + 1) + "-" + dp_cost_date.getDayOfMonth();//这里getMonth会比当前月份少一个月,所以要+1 if ("".equals(moneyStr)) {//可以不填写Title但是不能不填金额 Toast toast = Toast.makeText(this, "请填写金额", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } else { if(seStr.equals("收入")) { StringBuilder sb = new StringBuilder(moneyStr); sb.insert(0, "+"); moneyStr=sb.toString(); } else { StringBuilder sb = new StringBuilder(moneyStr); sb.insert(0, "-"); moneyStr=sb.toString(); } String[] strArr1=dateStr.split("-"); if(strArr1[1].length()==1)//保证日期格式对齐 { StringBuilder sb = new StringBuilder(strArr1[1]); sb.insert(0, "0"); dateStr=strArr1[0]+"-"+sb.toString(); } StringBuilder sb = new StringBuilder(strArr1[2]); if(strArr1[2].length()==1) { sb.insert(0, "0"); } dateStr=dateStr+"-"+sb.toString(); SQLiteDatabase db = helper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("Se",seStr); values.put("Title", titleStr); values.put("Money", moneyStr); values.put("Date", dateStr); long account = db.insert("account2", null, values); if (account > 0) { Toast toast = Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); setResult(1); finish(); } else { Toast toast = Toast.makeText(this, "请重试", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); db.close(); } setResult(1); finish(); } } }
这里将收入100元显示成+100,支出100元显示成-100。
时间的格式统一设置为xxxx-xx-xx的格式