昨天所做:保存新建账单的数据到SQLite
代码是冲刺完才粘的,可能会和所说的有所不同
package com.example.accountbook; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteStatement; import android.util.Log; import android.widget.Toast; import java.util.List; public class DB extends SQLiteOpenHelper { private static final String DB_NAME="accountbook"; private static final int DB_VERSION=1; private static DB mHelper=null; private SQLiteDatabase mDB=null; private static String TABLE_NAME="默认账本"; private static final String TABLE_NAME_BOOK = "book"; public DB(Context context) { super(context,DB_NAME,null,DB_VERSION); } public DB(Context context ,int version) { super(context,DB_NAME,null,version); } public SQLiteDatabase getMDB() { return mDB; } public void setTableName(String TABLE_NAME) { this.TABLE_NAME = TABLE_NAME; } //利用单例模式获取数据库帮助器的唯一实例 public static DB getInstance(Context context,int version) { if (version>0&&mHelper==null) { mHelper=new DB(context, version); }else if (mHelper==null) { mHelper=new DB(context); } return mHelper; } //获取账本名 public String getTableName() { return TABLE_NAME; } //打开数据库的读连接 public SQLiteDatabase openReadLink() { if (mDB==null||!mDB.isOpen()) { mDB=mHelper.getReadableDatabase(); } return mDB; } //打开数据库的写连接 public SQLiteDatabase openWriteLink() { if (mDB==null||!mDB.isOpen()) { mDB=mHelper.getWritableDatabase(); } return mDB; } //关闭数据库连接 public void closeLink() { if (mDB!=null&&mDB.isOpen()) { mDB.close(); mDB=null; } } //创建数据库,执行建表语句 public void onCreate(SQLiteDatabase db) { String drop_sql="DROP TABLE IF EXISTS "+TABLE_NAME+";"; db.execSQL(drop_sql); String create_sql="CREATE TABLE IF NOT EXISTS "+TABLE_NAME+" (" +"id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +"money double NOT NULL," +"choose VARCHAR NOT NULL," +"sort varchar NOT NULL," +"time varchar NOT NULL," +"remark varchar" +")"; db.execSQL(create_sql); Log.d("create","建表成功"+TABLE_NAME_BOOK); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } //添加账单 public long insert(BillBean billbean) { long result = -1; ContentValues cv = new ContentValues(); cv.put("money",billbean.getMoney()); cv.put("choose",billbean.getChoose()); cv.put("sort",billbean.getSort()); cv.put("time",billbean.getTime()); cv.put("remark",billbean.getRemark()); result = mDB.insert(TABLE_NAME,"",cv); if(result >= 0) { Log.i("add", "添加账单成功"); } return result; }//检测某个表是否存在,存在返回false,不存在返回true public boolean tableIsExist(String bookname){ Log.d("exit","开始检测表 "+bookname+" 是否存在"); if(bookname == null){ Log.d("exit","表 "+bookname+" 名字为空"); return false; } Cursor cursor = null; try { String sql = "select count(*) from "+TABLE_NAME_BOOK+" where bookname ='"+bookname+"' "; cursor = mDB.rawQuery(sql, null); if (cursor.getInt(0)==0) { Log.d("exit","表 "+bookname+" 检测返回值 false"); return false; } } catch (Exception e) { // TODO: handle exception } Log.d("exit","表 "+bookname+" 检测返回值 true"); return true; } //获取表中数据的数量 public long getCount(String tablename) { String select_sql = "select count(*) from "+tablename; SQLiteStatement statement = mDB.compileStatement(select_sql); long count = statement.simpleQueryForLong(); return count; } }
今天将做:将SQLite中的数据显示到主页面
遇到问题:
忘了截图
报错db是空指针
重写onResume()和onPause()