在Java中一切皆對象,我們可以把要操作的數據封裝成一個數據訪問對象,在數據訪問對象中使用數據庫助手類,更方便我們操作數據庫,使用在數據訪問對象中可以使用數據庫的語句,也可以使用Android給我們封裝好的函數;使用Android自帶的函數我們可以不用考慮每個字段的數據類型,Android會進行轉換。
程序結構:
對數據的增、刪、改、查,隻在TestUserDb.java中測試,需要在AndroidManifest.xml中添加以下代碼
下面主要看下Dao的操作
使用SQLite語句的UserDao.java
package com.example.db.dao; import java.util.ArrayList; import java.util.List; import com.example.db.MySQLiteHelper; import com.example.db.domain.User; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; /** * user的數據訪問對象類-使用數據庫語句對數據操進行操作 * * @author Administrator * */ public class UserDao { private MySQLiteHelper helper; // 在構造函數中創建數據庫幫助類 public UserDao(Context context) { helper = new MySQLiteHelper(context, user.db, 1); } /** * 插入數據到數據庫 * * @param id * @param name * @param age */ public void add(int id, String name, int age) { SQLiteDatabase db = helper.getWritableDatabase(); db.execSQL(insert into user(id, name, age) values(?, ?, ?), new Object[] { id, name, age }); db.close(); } /** * 查詢是否成功 * * @param name */ public boolean select(String name) { SQLiteDatabase db = helper.getReadableDatabase(); Cursor cursor = db.rawQuery(select * from user where name=?, new String[] { name }); boolean result = cursor.moveToNext(); cursor.close(); db.close(); return result; } /** * 修改記錄 * * @param name * @param age */ public void update(String name, int age) { SQLiteDatabase db = helper.getWritableDatabase(); db.execSQL(update user set age=? where name=?, new Object[] { age, name }); db.close(); } /** * 刪除記錄 * * @param name */ public void delete(String name) { SQLiteDatabase db = helper.getWritableDatabase(); db.execSQL(delete from user where name=?, new Object[] { name }); db.close(); } /** * 查詢全部 * * @return */ public List selectAll() { SQLiteDatabase db = helper.getReadableDatabase(); Cursor cursor = db.rawQuery(select * from user, null); List users = new ArrayList(); while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex(id)); String name = cursor.getString(cursor.getColumnIndex(name)); int age = cursor.getInt(cursor.getColumnIndex(age)); User user = new User(id, name, age); users.add(user); user = null; } cursor.close(); db.close(); return users; } }
使用Android為我們提供的語句UserDaoBySys.java
package com.example.db.dao; import java.util.ArrayList; import java.util.List; import com.example.db.MySQLiteHelper; import com.example.db.domain.User; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; /** * user的數據訪問對象類 ;使用系統的API更新數據 * * @author Administrator * */ public class UserDaoBySys { private MySQLiteHelper helper; // 在構造函數中創建數據庫幫助類 public UserDaoBySys(Context context) { helper = new MySQLiteHelper(context, user.db, 1); } /** * 插入數據到數據庫 * * @param id * @param name * @param age */ public boolean add(int id, String name, int age) { SQLiteDatabase db = helper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(id, id); values.put(name, name); values.put(age, age); long result = db.insert(user, null, values); db.close(); if (result != -1) return true; else return false; } /** * 查詢是否成功 * * @param name */ public boolean select(String name) { SQLiteDatabase db = helper.getReadableDatabase(); // Cursor cursor = db.rawQuery(select * from user where name=?, // new String[] { name }); Cursor cursor = db.query(user, null, name=?, new String[] { name }, null, null, null); boolean result = cursor.moveToNext(); cursor.close(); db.close(); return result; } /** * 修改記錄 * * @param name * @param age */ public boolean update(String name, int age) { SQLiteDatabase db = helper.getWritableDatabase(); // db.execSQL(update user set age=? where name=?, new Object[] { age, // name }); ContentValues values = new ContentValues(); values.put(name, name); values.put(age, age); int result = db.update(user, values, name=?, new String[] { name }); db.close(); if (result > 0) return true; else return false; } /** * 刪除記錄 * * @param name */ public boolean delete(String name) { SQLiteDatabase db = helper.getWritableDatabase(); // db.execSQL(delete from user where name=?, new Object[] { name }); int result = db.delete(user, name=?, new String[] { name }); db.close(); if (result == 0) return false; else return true; } /** * 查詢全部 * * @return */ public List selectAll() { SQLiteDatabase db = helper.getReadableDatabase(); // Cursor cursor = db.rawQuery(select * from user, null); Cursor cursor = db.query(user, new String[] { id, name, age }, null, null, null, null, null); List users = new ArrayList(); while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex(id)); String name = cursor.getString(cursor.getColumnIndex(name)); int age = cursor.getInt(cursor.getColumnIndex(age)); User user = new User(id, name, age); users.add(user); user = null; } cursor.close(); db.close(); return users; } }
測試代碼TestUserDb.java
package com.example.db.test; import java.util.List; import com.example.db.MySQLiteHelper; import com.example.db.dao.UserDao; import com.example.db.domain.User; import android.database.sqlite.SQLiteDatabase; import android.test.AndroidTestCase; /** * 測試數據庫類 * @author Administrator * */ public class TestUserDb extends AndroidTestCase { public void createDB()throws Exception{ MySQLiteHelper helper = new MySQLiteHelper(getContext(), user.db, 1); SQLiteDatabase db = helper.getWritableDatabase(); //真正創建數據庫 db.close(); } public void testAdd() throws Exception{ UserDao dao = new UserDao(getContext()); dao.add(10, xxx, 10); } public void testSelect() throws Exception{ UserDao dao = new UserDao(getContext()); boolean result = dao.select(xxx); assertEquals(true, result); } public void testUpdate() throws Exception{ UserDao dao = new UserDao(getContext()); dao.update(xxx, 20); } public void testDelete() throws Exception{ UserDao dao = new UserDao(getContext()); dao.delete(xxx); } public void testSelectAll() throws Exception{ UserDao dao = new UserDao(getContext()); List users = dao.selectAll(); for(User user : users){ System.out.println(user.toString()); } } }
使用添加測試
添加瞭數據後我們有幾種方式可以查看數據是否已經添加瞭
一、再使用查詢測試,把查詢到的結果打印出來
二、把生成的數據庫導出,再使用SQLiteExpert查看
數據庫所在目錄
在DDMS上可以看到:/data/data/com.example.db.databases/user.db
查看的結果
SQLites Expert的使用也看簡單,下載路徑:點擊打開鏈接
三、使用命令查看數據
更詳細的代碼可以到以下地址下載:
點擊打開鏈接