Created
November 30, 2010 05:29
-
-
Save shirou/721209 to your computer and use it in GitHub Desktop.
DB access for Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment