我写了两个使用了Android 自带数据库操作demo,这两个demo都是备忘录。
有一个代码封装很好,数据库相关操作写成一个数据库管理类MemoSqlDataManage。
下载地址:http://download.csdn.net/detail/qq_16064871/8444969
下面我贴一下主要的代码:
1、辅助数据库类MySQLiteUtil,继承SQLiteOpenHelper类。有两个方法。
//辅助数据库类 public class MySQLiteUtil extends SQLiteOpenHelper { public MySQLiteUtil(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } String CREATE_TABLE_SQL = "create table user_tb(_id integer primary key autoincrement,subject,body,date)"; // 继承SQLiteOpenHelper类,要实现的方法,第一次安装会执行 public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_TABLE_SQL); // 在这个数据库,创建一张表 // 可以在这里一直创建表 } // 继承SQLiteOpenHelper类,要实现的方法,第一次安装会执行 public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { String oldVersion = null; System.out.print("-----" + oldVersion + "-----"); } }
2、数据库管理类MemoSqlDataManage,里面有数据库增删查改数据库语句。
//数据库管理类 public class MemoSqlDataManage { private static MemoSqlDataManage m_MemoSqlDataManage = null; private MySQLiteUtil mySQLiteUtil; // 辅助数据库 private SQLiteDatabase db; // 数据库db private Context mContext; // 单例模式 public static MemoSqlDataManage GetInstance(Context base) { if (m_MemoSqlDataManage == null) { m_MemoSqlDataManage = new MemoSqlDataManage(base); } return m_MemoSqlDataManage; } // 管理类初始化 public MemoSqlDataManage(Context base) { // 打开或创建test.db数据库 mySQLiteUtil = new MySQLiteUtil(base, "memento.db", null, 1); mContext = base; db = mySQLiteUtil.getReadableDatabase(); } /** * 往数据库添加数据 * * @param strsubject * @param strbody * @param strdate * @return */ public boolean addSqlData(String strsubject, String strbody, String strdate) { if (!strsubject.equals("")) { db.execSQL("insert into user_tb values(null,?,?,?)", new String[] { strsubject, strbody, strdate }); Toast.makeText(mContext, "添加备忘录成功", Toast.LENGTH_LONG).show(); return true; } else { Toast.makeText(mContext, "主题不能为空!", Toast.LENGTH_LONG).show(); } return false; } /** * 更新数据库 * * @param strsubject * @param strbody * @param strdate */ public void update(int nid, String strbody) { String strSQL = "update user_tb set body='" + strbody + "' where _id=" + nid; db.execSQL(strSQL); Toast.makeText(mContext, "更新备忘录成功", Toast.LENGTH_LONG).show(); } /** * 删除数据 * * @param nid * @return */ public boolean deleteSqlData(int nid) { boolean bdelete = false; if (bdelete == false) { String strSQL = "delete from user_tb where _id=" + nid; db.execSQL(strSQL); bdelete = true; Toast.makeText(mContext, "删除备忘录成功", Toast.LENGTH_LONG).show(); } return true; } // 查询数据库全部数据 public Cursor querySqlData() { Cursor cursor = db.rawQuery("select * from user_tb", null); // 查询全部数据 return cursor; } // 根据条件查询数据库数据 public Cursor querySqlData(int nid) { Cursor cursor = db.rawQuery("select * from user_tb where _id=" + nid, null); // 查询全部数据 return cursor; } // 关闭数据库 public boolean closeSql() { db.close(); return false; } }
3、这个页面是查询数据库所有数据,用listview显示。用了这个适配器SimpleCursorAdapter。这个适配器就是专门用来数据库数据适配的。
public class QureyDBActivity extends Activity { private MemoSqlDataManage mSqlDataManage; private ListView mlistView; private SimpleCursorAdapter mSimpleCursorAdapter; private Cursor mcursor; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_qurey); // 实例化MemoSqlDataManage数据库管理类 mSqlDataManage = MemoSqlDataManage.GetInstance(getApplicationContext()); mlistView = (ListView) findViewById(R.id.listView1); setAdapter(); mlistView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { mcursor.moveToPosition(arg2); int nid = mcursor.getInt(mcursor.getColumnIndex("_id")); String strtheme = mcursor.getString(mcursor .getColumnIndex("subject")); String strbody = mcursor.getString(mcursor .getColumnIndex("body")); String strdate = mcursor.getString(mcursor .getColumnIndex("date")); Intent intent = new Intent(); intent.setClass(QureyDBActivity.this, UpdateDBActivity.class); Bundle bundle = new Bundle(); bundle.putInt("id", nid); bundle.putString("theme", strtheme); bundle.putString("body", strbody); bundle.putString("date", strdate); intent.putExtras(bundle); startActivityForResult(intent, 4); } }); } @SuppressWarnings("deprecation") public void setAdapter() { // SimpleCursorAdapter适配器是用于数据库 mcursor = mSqlDataManage.querySqlData(); if (mcursor.getCount() > 0) { mcursor.moveToFirst(); mSimpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.item, mcursor, new String[] { "_id", "subject", "body", "date" }, new int[] { R.id.memento_id, R.id.memento_subject, R.id.memento_body, R.id.memento_date }); mlistView.setAdapter(mSimpleCursorAdapter); } } // 返回activity页面刷新 protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { setAdapter(); } } }
如果要详细项目源码,免费下载网址如下:
http://download.csdn.net/detail/qq_16064871/8444969
转载请注明出处,谢谢,如有疑问,留下评论!