实例: 会员信息管理
功能:1.查看数据库 2.清空数据库 3.增加会员 4.删除会员 5.更新会员 6.查找会员
数据库基类 – DBHelper.java
01 | package com.wirelessqa.sqlite; |
03 | import android.content.Context; |
04 | import android.database.sqlite.SQLiteDatabase; |
05 | import android.database.sqlite.SQLiteOpenHelper; |
06 | import android.util.Log; |
09 | * DBHelper继承了SQLiteOpenHelper,作为维护和管理数据库的基类 |
10 | * @author bixiaopeng 2013-2-16 下午3:05:52 |
12 | public class DBHelper extends SQLiteOpenHelper{ |
14 | public static final String DB_NAME = "wirelessqa.db"; |
15 | public static final String DB_TABLE_NAME = "info"; |
16 | private static final int DB_VERSION=1; |
17 | public DBHelper(Context context) { |
20 | super(context, DB_NAME, null, DB_VERSION); |
24 | public void onCreate(SQLiteDatabase db) { |
26 | db.execSQL("CREATE TABLE IF NOT EXISTS info" + |
27 | "(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, age INTEGER, website STRING,weibo STRING)"); |
28 | Log.i(WirelessQA.TAG, "create table"); |
32 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
35 | Log.i("WIRELESSQA", "update sqlite "+oldVersion+"---->"+newVersion); |
数据库业务操作 – DBManager.java
001 | package com.wirelessqa.sqlite; |
003 | import java.util.ArrayList; |
004 | import java.util.List; |
006 | import android.content.ContentValues; |
007 | import android.content.Context; |
008 | import android.database.Cursor; |
009 | import android.database.sqlite.SQLiteDatabase; |
010 | import android.util.Log; |
013 | *DBManager是建立在DBHelper之上,封装了常用的业务方法 |
014 | * @author bixiaopeng 2013-2-16 下午3:06:26 |
016 | public class DBManager { |
018 | private DBHelper helper; |
019 | private SQLiteDatabase db; |
021 | public DBManager(Context context){ |
022 | helper = new DBHelper(context); |
023 | db = helper.getWritableDatabase(); |
031 | public void add(List<MemberInfo> memberInfo) { |
032 | db.beginTransaction(); |
034 | for (MemberInfo info : memberInfo) { |
035 | Log.i(WirelessQA.TAG, "------add memberInfo----------"); |
036 | Log.i(WirelessQA.TAG, info.name + "/" + info.age + "/" + info.website + "/" + info.weibo); |
038 | db.execSQL("INSERT INTO info VALUES(null,?,?,?,?)", new Object[] { info.name, info.age, info.website, |
041 | db.setTransactionSuccessful(); |
054 | public void add(int _id, String name, int age, String website, String weibo) { |
055 | Log.i(WirelessQA.TAG, "------add data----------"); |
056 | ContentValues cv = new ContentValues(); |
058 | cv.put("name", name); |
060 | cv.put("website", website); |
061 | cv.put("weibo", weibo); |
062 | db.insert(DBHelper.DB_TABLE_NAME, null, cv); |
063 | Log.i(WirelessQA.TAG, name + "/" + age + "/" + website + "/" + weibo); |
071 | public void delData(String name) { |
073 | String[] args = { name }; |
074 | db.delete(DBHelper.DB_TABLE_NAME, "name=?", args); |
075 | Log.i(WirelessQA.TAG, "delete data by " + name); |
082 | public void clearData() { |
083 | ExecSQL("DELETE FROM info"); |
084 | Log.i(WirelessQA.TAG, "clear data"); |
092 | public ArrayList<MemberInfo> searchData(final String name) { |
093 | String sql = "SELECT * FROM info WHERE name =" + "'" + name + "'"; |
094 | return ExecSQLForMemberInfo(sql); |
097 | public ArrayList<MemberInfo> searchAllData() { |
098 | String sql = "SELECT * FROM info"; |
099 | return ExecSQLForMemberInfo(sql); |
109 | public void updateData(String raw, String rawValue, String whereName) { |
110 | String sql = "UPDATE info SET " + raw + " =" + " " + "'" + rawValue + "'" + " WHERE name =" + "'" + whereName |
113 | Log.i(WirelessQA.TAG, sql); |
122 | private ArrayList<MemberInfo> ExecSQLForMemberInfo(String sql) { |
123 | ArrayList<MemberInfo> list = new ArrayList<MemberInfo>(); |
124 | Cursor c = ExecSQLForCursor(sql); |
125 | while (c.moveToNext()) { |
126 | MemberInfo info = new MemberInfo(); |
127 | info._id = c.getInt(c.getColumnIndex("_id")); |
128 | info.name = c.getString(c.getColumnIndex("name")); |
129 | info.age = c.getInt(c.getColumnIndex("age")); |
130 | info.website = c.getString(c.getColumnIndex("website")); |
131 | info.weibo = c.getString(c.getColumnIndex("weibo")); |
143 | private void ExecSQL(String sql) { |
146 | Log.i("execSql: ", sql); |
147 | } catch (Exception e) { |
148 | Log.e("ExecSQL Exception", e.getMessage()); |
159 | private Cursor ExecSQLForCursor(String sql) { |
160 | Cursor c = db.rawQuery(sql, null); |
164 | public void closeDB() { |
会员信息的JavaBean – MemberInfo.java
01 | package com.wirelessqa.sqlite; |
05 | * @author bixiaopeng 2013-2-16 下午3:07:02 |
07 | public class MemberInfo { |
12 | public String website; |
15 | public MemberInfo(int _id,String name,int age,String website,String weibo){ |
19 | this.website = website; |
首页显示 – MainActivity.java
显示结果页 – DisplayActivity.java
01 | package com.wirelessqa.sqlite; |
03 | import android.os.Bundle; |
04 | import android.widget.TextView; |
08 | * @author bixiaopeng 2013-2-16 下午3:06:36 |
10 | public class DisplayActivity extends MainActivity{ |
11 | private String result = null; |
12 | private TextView display = null; |
14 | protected void onCreate(Bundle savedInstanceState) { |
15 | super.onCreate(savedInstanceState); |
16 | setContentView(R.layout.activity_display); |
17 | Bundle extras = getIntent().getExtras(); |
18 | result = extras.getString("searchResult"); |
19 | display = (TextView)findViewById(R.id.display_txt); |
20 | display.setText(result); |
源码下载:http://download.youkuaiyun.com/detail/wirelessqa/5066148