import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class MySQLiteHelper extends SQLiteOpenHelper {
public static final String DB_NAME="bookstore.db";
public static final int DB_VERSION=2;
public MySQLiteHelper(@Nullable Context context)
{super(context,DB_NAME,null,DB_VERSION);}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
updateMyDatabase(sqLiteDatabase,0,DB_VERSION);
}
public static void insertBook(SQLiteDatabase sqLiteDatabase,String name,String isbn,String description,int imageId){
ContentValues contentValues=new ContentValues();
contentValues.put("NAME",name);
contentValues.put("DESCRIPTION",description);
contentValues.put("ISBN",isbn);
contentValues.put("IMAGE_ID",imageId);
sqLiteDatabase.insert("BOOK",null,contentValues);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
public void updateMyDatabase(SQLiteDatabase sqLiteDatabase,int i,int i1){
if (i<1){
sqLiteDatabase.execSQL("CREATE TABLE BOOK(_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"NAME TEXT,ISBN TEXT,DESCRIPTION TEXT,IMAGE_ID INTEGER)");
insertBook(sqLiteDatabase,"Android移动应用基础教程","9787113252502","本书是第一本Android入门书籍,共15章", R.drawable.book1);
insertBook(sqLiteDatabase,"Android第一行代码(第二版)","978711343434","本书案例非常实用,深入浅出",R.drawable.book2);
insertBook(sqLiteDatabase,"Head First Android开发","9787113544356","开发Android应用的快速指南和参考书",R.drawable.book3);
}
if (i<2){
}
}
}