編輯:關於Android編程
(1)目錄結構如下:
(2)鏈接數據庫的文件:DBHelper.java要繼承SQLiteOpenHelper類<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PHByZSBjbGFzcz0="brush:java;">package com.lc.sqlite_demo1.db;
import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "mydb.db"; // 創建數據庫的文件
private static final int VERSION = 2; // 數據庫版本,版本是更新的依據
/*
* 需要這個構造方法: 數據庫只有在調用getWritableDatabase();getReadableDatabase(;方法的時候才會創建數據庫
*/
public DBHelper(Context context) {
super(context, DB_NAME, null, VERSION);
}
/*
* public DBManager(Context context, String name, CursorFactory factory, int
* version) { super(context, name, factory, version); // TODO Auto-generated
* constructor stub
*
* }
*/
/*
* 創建數據庫的時候用到的語句
*/
@Override
public void onCreate(SQLiteDatabase db) {
// 數據類型:varchar、int、long、float、boolean、text、blob、clob等類型
// 建表語句執行
String sql = "create table person(pid integer primary key autoincrement,name varchar(64),address varchar(200))";
db.execSQL(sql);
}
/*
* 更新數據庫的時候使用到的; 這一句在一開始的時候是不會添加的,因為版本號是一樣的只有執行一次之後,修改了版本號才會執行這段代碼
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "alter table person add age integer";
db.execSQL(sql);
}
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
}
}
(3)創建一個DBManager.java還有數據的增、刪、改、查及無sql語句的增、刪、改、查方法匯總:
package com.lc.sqlite_demo1.db; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; public class DBManager { private DBHelper dbHelper; private SQLiteDatabase database; public DBManager(Context context) { dbHelper = new DBHelper(context); } /* * 用於更新數據的包括插入、刪除、修改 */ public boolean updateBySQL(String sql, Object[] bindArgs) { boolean flag = false; try { database.execSQL(sql, bindArgs); flag = true; } catch (SQLException e) { e.printStackTrace(); } /* * finally { if (database != null) { database.close(); } } */ return flag; } /* * 單獨一個方法,獲得數據庫的鏈接 */ public void getDataBaseConn() { database = dbHelper.getWritableDatabase(); } /* * 釋放數據庫鏈接 */ public void releaseConn() { if (database != null) { database.close(); } } /* * 查找數據的測試程序 */ public void queryBySQL(String sql, String[] selectionArgs) { Cursor cursor = database.rawQuery(sql, selectionArgs); while (cursor.moveToNext()) { System.out.println("-->>" + cursor.getString(cursor.getColumnIndex("name"))); System.out.println("-->>" + cursor.getString(cursor.getColumnIndex("address"))); System.out.println("-->>" + cursor.getInt(cursor.getColumnIndex("age"))); } } /* * 查找單條數據的查詢方法 */ public MapquerySingleResultBySQL(String sql, String[] selectionArgs) { Map map = new HashMap (); Cursor cursor = database.rawQuery(sql, selectionArgs); int cols_len = cursor.getColumnCount(); while (cursor.moveToNext()) { for (int i = 0; i < cols_len; i++) { String cols_name = cursor.getColumnName(i); String cols_value = cursor.getString(cursor .getColumnIndex(cols_name)); if (cols_value == null) { cols_value = ""; } map.put(cols_name, cols_value); } } return map; } /** * 同構反射獲得數據庫的記錄; 聲明Class的屬性必須都是String類型 * * @param sql * @param selectionArgs * @param cls * @return */ public T querySingleCursor(String sql, String[] selectionArgs, Class cls) { T t = null; Cursor cursor = database.rawQuery(sql, selectionArgs); int cols_len = cursor.getColumnCount(); while (cursor.moveToNext()) { try { t = cls.newInstance(); for (int i = 0; i < cols_len; i++) { String cols_name = cursor.getColumnName(i); String cols_value = cursor.getString(cursor .getColumnIndex(cols_name)); if (cols_value == null) { cols_value = ""; } Field field = null; try { field = cls.getDeclaredField(cols_name); } catch (NoSuchFieldException e) { e.printStackTrace(); } field.setAccessible(true); field.set(t, cols_value); } } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } return t; } /** * 同構反射獲得多條數據庫的記錄; 聲明Class的屬性必須都是String類型 * * @param sql * @param selectionArgs * @param cls * @return */ public List queryMutilCursor(String sql, String[] selectionArgs, Class cls) { List list = new ArrayList (); Cursor cursor = database.rawQuery(sql, selectionArgs); int cols_len = cursor.getColumnCount(); while (cursor.moveToNext()) { try { T t = cls.newInstance(); for (int i = 0; i < cols_len; i++) { String cols_name = cursor.getColumnName(i); String cols_value = cursor.getString(cursor .getColumnIndex(cols_name)); if (cols_value == null) { cols_value = ""; } Field field = null; try { field = cls.getDeclaredField(cols_name); } catch (NoSuchFieldException e) { e.printStackTrace(); } field.setAccessible(true); field.set(t, cols_value); list.add(t); } } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } return list; } /* * 查找多條數據的查詢方法 */ public List
package com.lc.sqlite_demo1; import java.util.List; import java.util.Map; import com.lc.sqlite_demo1.db.DBHelper; import com.lc.sqlite_demo1.db.DBManager; import android.content.ContentValues; import android.database.Cursor; import android.test.AndroidTestCase; public class MyTest extends AndroidTestCase { /* * 測試創建數據庫,只有調用getReadableDatabase()/getReadableDatabase()方法才會創建數據庫 */ public void initTable() { DBHelper dbManager = new DBHelper(getContext()); dbManager.getReadableDatabase(); } public void insert() { // String sql = "insert into person(name,address,age) values(?,?,?)"; String sql = "insert into person(name,address) values(?,?)"; Object[] bindArgs = { "張三", "成都" }; DBManager dbManager = new DBManager(getContext()); dbManager.getDataBaseConn(); dbManager.updateBySQL(sql, bindArgs); dbManager.releaseConn(); } public void update() { String sql = "update person set name=?,address=?,age=? where pid =1"; Object[] bindArgs = { "王五", "北京", 23 }; DBManager dbManager = new DBManager(getContext()); dbManager.getDataBaseConn(); dbManager.updateBySQL(sql, bindArgs); dbManager.releaseConn(); } public void delete() { String sql = "delete from person where pid =?"; Object[] bindArgs = { 1 }; DBManager dbManager = new DBManager(getContext()); dbManager.getDataBaseConn(); dbManager.updateBySQL(sql, bindArgs); dbManager.releaseConn(); } public void query() { String sql = "select * from person"; DBManager dbManager = new DBManager(getContext()); dbManager.getDataBaseConn(); dbManager.queryBySQL(sql, null); dbManager.releaseConn(); } public void querySingleResultBySQLTest() { String sql = "select * from person where pid =?"; DBManager dbManager = new DBManager(getContext()); dbManager.getDataBaseConn(); Mapmap = dbManager.querySingleResultBySQL(sql, new String[] { "2" }); System.out.println("--->>" + map.get("name")); System.out.println("--->>" + map.get("address")); System.out.println("--->>" + map.get("age")); dbManager.releaseConn(); } public void queryMutiResultBySQLTest() { String sql = "select * from person where name like ?"; DBManager dbManager = new DBManager(getContext()); dbManager.getDataBaseConn(); List > list = dbManager.queryMutiResultBySQL(sql, new String[] { "%張%" }); for (Map map2 : list) { System.out.println("---->>" + map2.get("name")); System.out.println("---->>" + map2.get("address")); System.out.println("--->>" + map2.get("age")); } dbManager.releaseConn(); } /**************************** 以下是不需要sql語句的查詢方法測試 **********************************/ /* * 無需查詢語句的插入操作 */ public void insertNotSQLTest() { DBManager dbManager = new DBManager(getContext()); dbManager.getDataBaseConn(); ContentValues values = new ContentValues(); values.put("name", "xuliugen"); values.put("address", "dfhkjsdhfkjhsd"); values.put("age", 22); dbManager.insertByNotSQL("person", null, values); dbManager.releaseConn(); } /* * 無需查詢語句的更新操作 */ public void updateNotSQLTest() { DBManager dbManager = new DBManager(getContext()); dbManager.getDataBaseConn(); ContentValues values = new ContentValues(); values.put("name", "fff"); values.put("address", "12srqrqwrewrqrwetrew"); values.put("age", 22); dbManager.updateByNotSQL("person", values, "pid=? and name=?", new String[] { "5", "fff" }); dbManager.releaseConn(); } /* * 無需查詢語句的刪除操作 */ public void deleteNotSQL() { DBManager dbManager = new DBManager(getContext()); dbManager.getDataBaseConn(); dbManager.deleteNotSQL("person", "pid=?", new String[] { "5" }); dbManager.releaseConn(); } /* * 無需查詢語句的查詢操作 */ public void queryByNotSQLTest() { DBManager dbManager = new DBManager(getContext()); dbManager.getDataBaseConn(); Cursor cursor = dbManager.queryByNotSQL(false, "person", null, null, null, null, null, null, null); while (cursor.moveToNext()) { System.out.println("-->>" + cursor.getColumnIndex("name")); System.out.println("-->>" + cursor.getColumnIndex("age")); System.out.println("-->>" + cursor.getColumnIndex("address")); } dbManager.releaseConn(); } }
上一篇文章已經介紹了如何為RecyclerView添加FootView,在此基礎上,要添加分頁加載的功能其實已經很簡單了。 上一篇文章地址:為RecyclerView添加
最近突然心血來潮,對一些Loading感興趣,Loading這玩意說重要也重要,說不重要也不重要,因為這是一個提升你產品體驗的一個細節,如果loading做的好,對於一些
android的L新版本中增加了“打擾”的新功能,相信很多同學搞不明白。找了一篇介紹勿擾模式很好的文章,可惜是英文的,現翻譯如下,相
本文實例講述了Android控件之Spinner用法。分享給大家供大家參考。具體如下:以下模擬下拉列表的用法布局文件:<?xml version=1.0 e