Android sqlite 数据库操作

我写了两个使用了Android 自带数据库操作demo,这两个demo都是备忘录。

有一个代码封装很好,数据库相关操作写成一个数据库管理类MemoSqlDataManage。

下载地址:http://download.csdn.net/detail/qq_16064871/8444969

 

下面我贴一下主要的代码:

1、辅助数据库类MySQLiteUtil,继承SQLiteOpenHelper类。有两个方法。

[java]  view plain copy
  1. //辅助数据库类  
  2. public class MySQLiteUtil extends SQLiteOpenHelper {  
  3.     public MySQLiteUtil(Context context, String name, CursorFactory factory,  
  4.             int version) {  
  5.         super(context, name, factory, version);  
  6.     }  
  7.   
  8.     String CREATE_TABLE_SQL = "create table user_tb(_id integer primary key autoincrement,subject,body,date)";  
  9.   
  10.     // 继承SQLiteOpenHelper类,要实现的方法,第一次安装会执行  
  11.     public void onCreate(SQLiteDatabase db) {  
  12.         db.execSQL(CREATE_TABLE_SQL); // 在这个数据库,创建一张表  
  13.         // 可以在这里一直创建表  
  14.     }  
  15.   
  16.     // 继承SQLiteOpenHelper类,要实现的方法,第一次安装会执行  
  17.     public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {  
  18.         String oldVersion = null;  
  19.         System.out.print("-----" + oldVersion + "-----");  
  20.     }  
  21.   
  22. }  

 

2、数据库管理类MemoSqlDataManage,里面有数据库增删查改数据库语句。

[java]  view plain copy
  1. //数据库管理类  
  2. public class MemoSqlDataManage {  
  3.     private static MemoSqlDataManage m_MemoSqlDataManage = null;  
  4.     private MySQLiteUtil mySQLiteUtil; // 辅助数据库  
  5.     private SQLiteDatabase db; // 数据库db  
  6.     private Context mContext;  
  7.   
  8.     // 单例模式  
  9.     public static MemoSqlDataManage GetInstance(Context base) {  
  10.         if (m_MemoSqlDataManage == null) {  
  11.             m_MemoSqlDataManage = new MemoSqlDataManage(base);  
  12.         }  
  13.         return m_MemoSqlDataManage;  
  14.     }  
  15.   
  16.     // 管理类初始化  
  17.     public MemoSqlDataManage(Context base) {  
  18.         // 打开或创建test.db数据库  
  19.         mySQLiteUtil = new MySQLiteUtil(base, "memento.db"null1);  
  20.         mContext = base;  
  21.         db = mySQLiteUtil.getReadableDatabase();  
  22.     }  
  23.   
  24.     /** 
  25.      * 往数据库添加数据 
  26.      *  
  27.      * @param strsubject 
  28.      * @param strbody 
  29.      * @param strdate 
  30.      * @return 
  31.      */  
  32.     public boolean addSqlData(String strsubject, String strbody, String strdate) {  
  33.         if (!strsubject.equals("")) {  
  34.             db.execSQL("insert into user_tb values(null,?,?,?)"new String[] {  
  35.                     strsubject, strbody, strdate });  
  36.             Toast.makeText(mContext, "添加备忘录成功", Toast.LENGTH_LONG).show();  
  37.             return true;  
  38.         } else {  
  39.             Toast.makeText(mContext, "主题不能为空!", Toast.LENGTH_LONG).show();  
  40.         }  
  41.         return false;  
  42.     }  
  43.   
  44.     /** 
  45.      * 更新数据库 
  46.      *  
  47.      * @param strsubject 
  48.      * @param strbody 
  49.      * @param strdate 
  50.      */  
  51.     public void update(int nid, String strbody) {  
  52.         String strSQL = "update user_tb set body='" + strbody + "' where _id="  
  53.                 + nid;  
  54.         db.execSQL(strSQL);  
  55.         Toast.makeText(mContext, "更新备忘录成功", Toast.LENGTH_LONG).show();  
  56.     }  
  57.   
  58.     /** 
  59.      * 删除数据 
  60.      *  
  61.      * @param nid 
  62.      * @return 
  63.      */  
  64.     public boolean deleteSqlData(int nid) {  
  65.         boolean bdelete = false;  
  66.         if (bdelete == false) {  
  67.             String strSQL = "delete from user_tb where _id=" + nid;  
  68.             db.execSQL(strSQL);  
  69.             bdelete = true;  
  70.             Toast.makeText(mContext, "删除备忘录成功", Toast.LENGTH_LONG).show();  
  71.         }  
  72.         return true;  
  73.     }  
  74.   
  75.     // 查询数据库全部数据  
  76.     public Cursor querySqlData() {  
  77.         Cursor cursor = db.rawQuery("select * from user_tb"null); // 查询全部数据  
  78.         return cursor;  
  79.     }  
  80.   
  81.     // 根据条件查询数据库数据  
  82.     public Cursor querySqlData(int nid) {  
  83.         Cursor cursor = db.rawQuery("select * from user_tb where _id=" + nid,  
  84.                 null); // 查询全部数据  
  85.         return cursor;  
  86.     }  
  87.   
  88.     // 关闭数据库  
  89.     public boolean closeSql() {  
  90.         db.close();  
  91.         return false;  
  92.     }  
  93. }  



3、这个页面是查询数据库所有数据,用listview显示。用了这个适配器SimpleCursorAdapter。这个适配器就是专门用来数据库数据适配的。

[java]  view plain copy
  1. public class QureyDBActivity extends Activity {  
  2.     private MemoSqlDataManage mSqlDataManage;  
  3.     private ListView mlistView;  
  4.     private SimpleCursorAdapter mSimpleCursorAdapter;  
  5.     private Cursor mcursor;  
  6.   
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_qurey);  
  10.   
  11.         // 实例化MemoSqlDataManage数据库管理类  
  12.         mSqlDataManage = MemoSqlDataManage.GetInstance(getApplicationContext());  
  13.   
  14.         mlistView = (ListView) findViewById(R.id.listView1);  
  15.   
  16.         setAdapter();  
  17.         mlistView.setOnItemClickListener(new OnItemClickListener() {  
  18.             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
  19.                     long arg3) {  
  20.                 mcursor.moveToPosition(arg2);  
  21.                 int nid = mcursor.getInt(mcursor.getColumnIndex("_id"));  
  22.                 String strtheme = mcursor.getString(mcursor  
  23.                         .getColumnIndex("subject"));  
  24.                 String strbody = mcursor.getString(mcursor  
  25.                         .getColumnIndex("body"));  
  26.                 String strdate = mcursor.getString(mcursor  
  27.                         .getColumnIndex("date"));  
  28.                 Intent intent = new Intent();  
  29.                 intent.setClass(QureyDBActivity.this, UpdateDBActivity.class);  
  30.                 Bundle bundle = new Bundle();  
  31.                 bundle.putInt("id", nid);  
  32.                 bundle.putString("theme", strtheme);  
  33.                 bundle.putString("body", strbody);  
  34.                 bundle.putString("date", strdate);  
  35.                 intent.putExtras(bundle);  
  36.                 startActivityForResult(intent, 4);  
  37.             }  
  38.         });  
  39.     }  
  40.   
  41.     @SuppressWarnings("deprecation")  
  42.     public void setAdapter() {  
  43.         // SimpleCursorAdapter适配器是用于数据库  
  44.         mcursor = mSqlDataManage.querySqlData();  
  45.         if (mcursor.getCount() > 0) {  
  46.             mcursor.moveToFirst();  
  47.             mSimpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.item,  
  48.                     mcursor, new String[] { "_id""subject""body""date" },  
  49.                     new int[] { R.id.memento_id, R.id.memento_subject,  
  50.                             R.id.memento_body, R.id.memento_date });  
  51.             mlistView.setAdapter(mSimpleCursorAdapter);  
  52.         }  
  53.     }  
  54.   
  55.     // 返回activity页面刷新  
  56.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  57.         if (resultCode == RESULT_OK) {  
  58.             setAdapter();  
  59.         }  
  60.     }  
  61. }  

 

如果要详细项目源码,免费下载网址如下:
http://download.csdn.net/detail/qq_16064871/8444969

转载请注明出处,谢谢,如有疑问,留下评论!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值