import androidcontentContentValues;
import androidcontentContext;
import androiddatabaseCursor;
import androiddatabasesqliteSQLiteDatabase;
import androiddatabasesqliteSQLiteOpenHelper;
import androidgraphicsBitmap;
import androidgraphicsBitmapFactory;
import javaioByteArrayOutputStream;
import static comumemyapplicationColumnsBOOK_AUTHOR;
/
_ Created by AlphaGo on 2017/3/11 just simple sample
/
public class BookDbHelper extends SQLiteOpenHelper {
public static final String DB_NAME = "Bookdb";
public static final String TABLE_BOOK = "Book";
public static final int DB_VERSION = 1;
private Context mContext;
public BookDbHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
createBookmarkTbl(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private void createBookmarkTbl(SQLiteDatabase db) {
dbexecSQL("CREATE TABLE IF NOT EXISTS " + TABLE_BOOK + "("
+ Columns_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ BOOK_AUTHOR + " TEXT,"
+ ColumnsBOOK_INTRODUCTION + " TEXT,"
+ ColumnsBOOK_LABEL + " TEXT ,"
+ ColumnsBOOK_NAME + " INTEGER NOT NULL DEFAULT 0,"
+ ColumnsBOOK_PRICE + " FLOAT,"
+ ColumnsBOOK_STOCK + " INTEGER NOT NULL,"
+ ColumnsBOOK_ICON + " BLOB"
+ ");");
}
/
Insert Item
/
public long insertItem(Book book) {
long id;
ContentValues initValues = new ContentValues();
ByteArrayOutputStream os = new ByteArrayOutputStream();
bookiconcompress(BitmapCompressFormatPNG, 100, os);
initValuesput(BOOK_AUTHOR, bookauthor);
initValuesput(ColumnsBOOK_INTRODUCTION, bookintroduction);
initValuesput(ColumnsBOOK_LABEL, booklabel);
initValuesput(ColumnsBOOK_NAME, bookname);
initValuesput(ColumnsBOOK_PRICE, bookprice);
initValuesput(ColumnsBOOK_STOCK, bookstock);
initValuesput(ColumnsBOOK_ICON, ostoByteArray());
SQLiteDatabase db = getWritableDatabase();
id = dbinsert(TABLE_BOOK, null, initValues);
dbclose();
return id;
}
/
retrieve item by database index
/
public Book getItemByIndex (int index){
Book book = new Book();
SQLiteDatabase db = getWritableDatabase();
Cursor cursor = dbquery(TABLE_BOOK, null, "_id =", new String[]{StringvalueOf(index)}, null, null, null);
while (cursormoveToNext()){
int id = cursorgetInt(0);
String author = cursorgetString(1);
String introduction = cursorgetString(2);
String label = cursorgetString(3);
String name = cursorgetString(4);
float price = cursorgetFloat(5);
int stock = cursorgetInt(6);
byte[] bytes = cursorgetBlob(7);
Bitmap icon = BitmapFactorydecodeByteArray(bytes, 0, byteslength);
bookicon = icon;
// TODO: 2017/3/11 one more thing
}
return book;
}
private static class Book {
public String author;
public String introduction;
public String label;
public String name;
public float price;
public int stock;
public Bitmap icon;
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)