js对象在sqlite中保存和读取

js对象在sqlite中保存和读取,第1张

在SQLite中,你需要创建表来存储js对象的各个属性及其对应的值。可以使用JSONstringify()方法将js对象转换为JSON字符串,然后将其存储在SQLite表中的文本类型的列中。当需要从SQLite库中检索对象数据时,可以使用JSONparse()方法将JSON字符串转换回js对象,以便访问各个属性的值。另外,SQLite的API也支持直接使用Blob类型存储二进制数据,因此可以将js对象序列化为二进制格式再保存在SQLite数据库中。

下面这个文件创建了Bookdb,创建了表book,另外提供两个简单的方法,里面有很多不完善的地方,没有一一写出来。时间有限,希望能帮助到你。
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;
}
}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/13384305.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-25
下一篇 2023-07-25

发表评论

登录后才能评论

评论列表(0条)

保存