Android 數(shù)據(jù)庫連通性
Android 中一個非常有用的特征就是存在本地關(guān)系數(shù)據(jù)庫。保證您能在本地文件中存儲您的數(shù)據(jù),但通常更有用的是使用一個關(guān)系型數(shù)據(jù)庫管理系統(tǒng)(Relational Database Management System,RDBMS)來存儲。Android 提供給您常用的 SQLite 數(shù)據(jù)庫來進(jìn)行處理,因?yàn)閷τ谙?Android 這類嵌入式系統(tǒng)它是高度優(yōu)化的。它被 Android 上的核心應(yīng)用程序所用。例如,用戶地址簿是存儲在一個 SQLite 數(shù)據(jù)庫中。現(xiàn)在,對于給定的 Android 的 Java 實(shí)現(xiàn),您可以使用 JDBC 來訪問這些數(shù)據(jù)庫。出人意料的是,Android 甚至包括構(gòu)成主要部分 JDBC API 的 java.sql 和 javax.sql 包。然而,當(dāng)涉及使用本地 Android 數(shù)據(jù)庫進(jìn)行處理時,這毫無用處。相反地,您想要使用 android.database 和 android.database.sqlite 包。清單 5 是一個使用這些類存儲和檢索數(shù)據(jù)的示例。
清單 5. 使用 Android 進(jìn)行數(shù)據(jù)庫訪問
public class StocksDb { private static final String DB_NAME = "stocks.db"; private static final int DB_VERSION = 1; private static final String TABLE_NAME = "stock"; private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY, symbol TEXT, max_price DECIMAL(8,2), " + "min_price DECIMAL(8,2), price_paid DECIMAL(8,2), " + "quantity INTEGER)"; private static final String INSERT_SQL = "INSERT INTO " + TABLE_NAME + " (symbol, max_price, min_price, price_paid, quantity) " + "VALUES (?,?,?,?,?)"; private static final String READ_SQL = "SELECT id, symbol, max_price, " + "min_price, price_paid, quantity FROM " + TABLE_NAME; private final Context context; private final SQLiteOpenHelper helper; private final SQLiteStatement stmt; private final SQLiteDatabase db; public StocksDb(Context context){ this.context = context; helper = new SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION){ @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { throw new UnsupportedOperationException(); } }; db =helper.getWritableDatabase(); stmt = db.compileStatement(INSERT_SQL); } public Stock addStock(Stock stock){ stmt.bindString(1, stock.getSymbol()); stmt.bindDouble(2, stock.getMaxPrice()); stmt.bindDouble(3, stock.getMinPrice()); stmt.bindDouble(4, stock.getPricePaid()); stmt.bindLong(5, stock.getQuantity()); int id = (int) stmt.executeInsert(); return new Stock (stock, id); } public ArrayList<Stock> getStocks() { Cursor results = db.rawQuery(READ_SQL, null); ArrayList<Stock> stocks = new ArrayList<Stock>(results.getCount()); if (results.moveToFirst()){ int idCol = results.getColumnIndex("id"); int symbolCol = results.getColumnIndex("symbol"); int maxCol = results.getColumnIndex("max_price"); int minCol = results.getColumnIndex("min_price"); int priceCol = results.getColumnIndex("price_paid"); int quanitytCol = results.getColumnIndex("quantity"); do { Stock stock = new Stock(results.getString(symbolCol), results.getDouble(priceCol), results.getInt(quanitytCol), results.getInt(idCol)); stock.setMaxPrice(results.getDouble(maxCol)); stock.setMinPrice(results.getDouble(minCol)); stocks.add(stock); } while (results.moveToNext()); } if (!results.isClosed()){ results.close(); } return stocks; } public void close(){ helper.close(); } }
出處:CSDN
責(zé)任編輯:bluehearts
上一頁 享受Android應(yīng)用程序的Java技術(shù)盛宴 [3] 下一頁 享受Android應(yīng)用程序的Java技術(shù)盛宴 [5]
◎進(jìn)入論壇網(wǎng)絡(luò)編程版塊參加討論
|