SQLiteOpenHelper類的簡化操作資料庫(api查詢方式)
阿新 • • 發佈:2019-01-23
<span style="font-family: Arial, Helvetica, sans-serif;">package com.sqf.sql.db;</span>
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; /** * 資料庫幫助類,用於建立和管理資料庫 * */ public class PersonSQLiteOpenHelper extends SQLiteOpenHelper{ private static final String TAG = "PersonSQLiteOpenHelper"; /** * 構造方法的四個引數 * 1.上下文 * 2.資料庫的名稱 * 3.遊標工程 * 4.資料庫的版本,從1開始 * */ public PersonSQLiteOpenHelper(Context context, String name, CursorFactory factory, int version) { super(context, "sql.db", null, 2); } public PersonSQLiteOpenHelper(Context context) { super(context, "sql.db", null, 2); } /** * 資料庫第一次建立的時,回撥此方法 * 初始化一些表 * */ @Override public void onCreate(SQLiteDatabase db) { //操作資料庫 String sql = "create table person(_id integer primary key autoincrement,name varchar(20),age integer);"; db.execSQL(sql); //建立person表 } /** * 資料庫版本號更新時回撥此方法 * 更新資料庫的內容(刪除表,修改表,新增表) * */ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { if(oldVersion == 1 && newVersion == 2){ //在person表中新增餘額列balance Log.i(TAG, "資料庫更新了"); //在person表新增一列 db.execSQL("alter table person add balance integer;"); } } }
package com.sqf.sql.dao; import java.util.ArrayList; import java.util.List; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteStatement; import android.text.TextUtils; import android.util.Log; import com.sqf.sql.db.PersonSQLiteOpenHelper; import com.sqf.sql.entities.Person; /** * api查詢方式 * */ public class PersonDao2 { private static final String TAG = "PersonDao2"; PersonSQLiteOpenHelper mOpenHelper;//資料庫的幫助類 public PersonDao2(Context context){ mOpenHelper = new PersonSQLiteOpenHelper(context); } /** * 新增到person表一條資料 * */ public void insert(Person person){ SQLiteDatabase db = mOpenHelper.getWritableDatabase(); if(db.isOpen()){ //如果資料庫開啟 ContentValues values = new ContentValues(); values.put("name", person.getName());//key作為要儲存的列名,value代表對應的值 values.put("age", person.getAge()); long id = db.insert("person", null , values); Log.i(TAG, id+""); db.close(); //資料庫關閉 } } public void delete(int id){ SQLiteDatabase db = mOpenHelper.getWritableDatabase();//獲得可寫入資料庫物件 if(db.isOpen()){ /* public int delete(String table, String whereClause, String[] whereArgs) { SQLiteStatement statement = new SQLiteStatement(this, "DELETE FROM " + table + (!TextUtils.isEmpty(whereClause) ? " WHERE " + whereClause : ""), whereArgs); */ /* delete方法的引數 1.表名 2.刪除語句where後面的字句 3.替換的?陣列 */ String whereClasuse = "_id = ?"; String [] whereArgs = {id+""}; int count = db.delete("person", whereClasuse, whereArgs); //返回值代表刪除行數 Log.i(TAG, "刪除了:"+count); db.close(); } } public void update(int id,String name){ SQLiteDatabase db = mOpenHelper.getWritableDatabase(); if(db.isOpen()){ ContentValues values = new ContentValues(); values.put("name", name); String whereClause = "_id = ?" ; int count = db.update("person", values, whereClause, new String[]{id+""}); Log.i(TAG, "修改了:"+count + "行"); db.close(); } } public List<Person> queryAll(){ SQLiteDatabase db = mOpenHelper.getReadableDatabase(); if(db.isOpen()){ String[] columns = {"_id","name","age"}; //需要查詢的列 String selection = null; // 選擇條件,給null查詢所有 String[] selectionArgs = null;//選擇條件引數,會把選擇條件中的?替換成這個陣列中的值 String groupBy = null; // 分組語句 group by name 注意些的時候需要要group by 去掉 String having = null; // 過濾語句 String orderBy = null ; //排序 Cursor cursor = db.query("person", columns, selection, selectionArgs, groupBy, having, orderBy); if(cursor != null && cursor.getCount() > 0){ List<Person> personList = new ArrayList<Person>(); while(cursor.moveToNext()){ //向下移動一位,直到最後一位,不可以往下移動了,停止 int id = cursor.getInt(0); String name = cursor.getString(1); int age = cursor.getInt(2); personList.add(new Person(id,name,age)); } db.close(); return personList; } db.close(); } return null; } public Person queryItem(int id){ SQLiteDatabase db = mOpenHelper.getReadableDatabase(); if(db.isOpen()){ String[] columns = {"_id","name","age"}; //需要查詢的列 String selection = "_id = ?"; // 選擇條件,給null查詢所有 String[] selectionArgs = {id+""};//選擇條件引數,會把選擇條件中的?替換成這個陣列中的值 String groupBy = null; // 分組語句 group by name 注意些的時候需要要group by 去掉 String having = null; // 過濾語句 String orderBy = null ; //排序 Cursor cursor = db.query("person", columns, selection, selectionArgs, groupBy, having, orderBy); if(cursor != null && cursor.moveToFirst()){ //cursor不位空,可以移動到第一行 int _id = cursor.getInt(0); String name = cursor.getString(1); int age = cursor.getInt(2); db.close(); return new Person(_id,name,age); } db.close(); } return null; } }
package com.sqf.sql.dao; import java.util.ArrayList; import java.util.List; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteStatement; import android.text.TextUtils; import android.util.Log; import com.sqf.sql.db.PersonSQLiteOpenHelper; import com.sqf.sql.entities.Person; /** * api查詢方式 * */ public class PersonDao2 { private static final String TAG = "PersonDao2"; PersonSQLiteOpenHelper mOpenHelper;//資料庫的幫助類 public PersonDao2(Context context){ mOpenHelper = new PersonSQLiteOpenHelper(context); } /** * 新增到person表一條資料 * */ public void insert(Person person){ SQLiteDatabase db = mOpenHelper.getWritableDatabase(); if(db.isOpen()){ //如果資料庫開啟 ContentValues values = new ContentValues(); values.put("name", person.getName());//key作為要儲存的列名,value代表對應的值 values.put("age", person.getAge()); long id = db.insert("person", null , values); Log.i(TAG, id+""); db.close(); //資料庫關閉 } } public void delete(int id){ SQLiteDatabase db = mOpenHelper.getWritableDatabase();//獲得可寫入資料庫物件 if(db.isOpen()){ /* public int delete(String table, String whereClause, String[] whereArgs) { SQLiteStatement statement = new SQLiteStatement(this, "DELETE FROM " + table + (!TextUtils.isEmpty(whereClause) ? " WHERE " + whereClause : ""), whereArgs); */ /* delete方法的引數 1.表名 2.刪除語句where後面的字句 3.替換的?陣列 */ String whereClasuse = "_id = ?"; String [] whereArgs = {id+""}; int count = db.delete("person", whereClasuse, whereArgs); //返回值代表刪除行數 Log.i(TAG, "刪除了:"+count); db.close(); } } public void update(int id,String name){ SQLiteDatabase db = mOpenHelper.getWritableDatabase(); if(db.isOpen()){ ContentValues values = new ContentValues(); values.put("name", name); String whereClause = "_id = ?" ; int count = db.update("person", values, whereClause, new String[]{id+""}); Log.i(TAG, "修改了:"+count + "行"); db.close(); } } public List<Person> queryAll(){ SQLiteDatabase db = mOpenHelper.getReadableDatabase(); if(db.isOpen()){ String[] columns = {"_id","name","age"}; //需要查詢的列 String selection = null; // 選擇條件,給null查詢所有 String[] selectionArgs = null;//選擇條件引數,會把選擇條件中的?替換成這個陣列中的值 String groupBy = null; // 分組語句 group by name 注意些的時候需要要group by 去掉 String having = null; // 過濾語句 String orderBy = null ; //排序 Cursor cursor = db.query("person", columns, selection, selectionArgs, groupBy, having, orderBy); if(cursor != null && cursor.getCount() > 0){ List<Person> personList = new ArrayList<Person>(); while(cursor.moveToNext()){ //向下移動一位,直到最後一位,不可以往下移動了,停止 int id = cursor.getInt(0); String name = cursor.getString(1); int age = cursor.getInt(2); personList.add(new Person(id,name,age)); } db.close(); return personList; } db.close(); } return null; } public Person queryItem(int id){ SQLiteDatabase db = mOpenHelper.getReadableDatabase(); if(db.isOpen()){ String[] columns = {"_id","name","age"}; //需要查詢的列 String selection = "_id = ?"; // 選擇條件,給null查詢所有 String[] selectionArgs = {id+""};//選擇條件引數,會把選擇條件中的?替換成這個陣列中的值 String groupBy = null; // 分組語句 group by name 注意些的時候需要要group by 去掉 String having = null; // 過濾語句 String orderBy = null ; //排序 Cursor cursor = db.query("person", columns, selection, selectionArgs, groupBy, having, orderBy); if(cursor != null && cursor.moveToFirst()){ //cursor不位空,可以移動到第一行 int _id = cursor.getInt(0); String name = cursor.getString(1); int age = cursor.getInt(2); db.close(); return new Person(_id,name,age); } db.close(); } return null; } }
Junit:
package com.sqf.sql.test;
import java.util.List;
import com.sqf.sql.dao.PersonDao;
import com.sqf.sql.dao.PersonDao2;
import com.sqf.sql.db.PersonSQLiteOpenHelper;
import com.sqf.sql.entities.Person;
import android.test.AndroidTestCase;
import android.util.Log;
public class TestCase2 extends AndroidTestCase {
private static final String TAG = "TestCase2";
public void test(){
//資料庫什麼時候建立
PersonSQLiteOpenHelper openHelper = new PersonSQLiteOpenHelper(getContext());
// 第一次連線資料庫時建立資料庫檔案,並呼叫onCreate
openHelper.getReadableDatabase();
}
public void testInsert(){
PersonDao2 dao = new PersonDao2(getContext());
dao.insert(new Person(1,"王五2",20));
}
public void testDelete(){
PersonDao2 dao = new PersonDao2(getContext());
dao.delete(2);
}
public void testUpdate(){
PersonDao2 dao = new PersonDao2(getContext());
dao.update(3, "哈哈");
}
public void testQueryAll(){
PersonDao2 dao = new PersonDao2(getContext());
List<Person> queryAll = dao.queryAll();
for(Person person:queryAll){
Log.i(TAG, person.toString());
}
}
public void testQuery(){
PersonDao2 dao = new PersonDao2(getContext());
Person person = dao.queryItem(3);
Log.i(TAG, person.toString());
}
}