今天学习了sqlite数据库,更新了工程
StarbuzzDatabaseHelper:
package bjfu.it.sun.starbuzz; import android.content.ContentValues; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import static android.icu.lang.UProperty.NAME; public class StarbuzzDatabaseHelper extends SQLiteOpenHelper { private static final String DB_NAME = "starbuzz.db";//数据库的名称 private static final int DB_VERSION = 2;//数据库的版本号 //StarbuzzDatabaseHelper的默认的构造函数,必须提供构造函数,制定数据库名和版本 public StarbuzzDatabaseHelper(Context context ){ super(context,DB_NAME,null,DB_VERSION); } @Override public void onCreate(SQLiteDatabase db) { //创建表 db.execSQL("CREATE TABLE DRINK(_id INTEGER PRIMARY KEY AUTOINCREMENT," +"NAME TEXT," +"DESCRIPTION TEXT," +"IMAGE_RESOURCE_ID INTEGER);" ); //插入咖啡的信息,3条数据 insertDrink(db,"Latte","A couple of espresso shots with steames milk",R.drawable .coffee1 ); insertDrink(db,"Cappuccino","Espresso,hot milk,and a steamed milk foam",R.drawable.coffee2); insertDrink(db,"Filter","Highest quality beans roasted and brewed fresh",R.drawable.coffee3); } public static void insertDrink (SQLiteDatabase db,String name,String description,int resourceld){ ContentValues drinkValues=new ContentValues(); drinkValues.put("NAME",name); drinkValues.put("DESCRIPTION",description ); drinkValues.put("IMAGE_RESOURCE_ID",resourceld ); long result=db.insert("DRINK",null,drinkValues) ; Log.d ("sqlite","insert"+name+",_id"+result ); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { if(oldVersion <=1){ //NUMERIC这种值可以代表bool类型 db.execSQL("ALTER TABLE DRINK ADD COLUMN FAVORITE NUMERIC;"); } } }
DrinkActivity:
package bjfu.it.sun.starbuzz; import androidx.appcompat.app.AppCompatActivity; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteOpenHelper; import android.os.Bundle; import android.util.Log; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class DrinkActivity extends AppCompatActivity { public static final String EXTRA_DRINKID="drinkId"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_drink); //从inrent中取出数组的索引 int drinkId=getIntent().getIntExtra(EXTRA_DRINKID ,0) ; // Drink drink=Drink.drinks[drinkId]; // // //显示咖啡照片 // ImageView photo=findViewById(R.id.photo ); // photo.setImageResource(drink.getImageResourceId() ) ; // photo.setContentDescription(drink.getName() ) ; // // // //显示咖啡名称 // TextView name=findViewById(R.id.name ); // name.setText(drink.getName() ); // // //显示咖啡的描述 // TextView description =findViewById(R.id.description ) ; // description.setText(drink.getDescription() ); //实例化一个helper对象, SQLiteOpenHelper starbuzzDataBaseHelper=new StarbuzzDatabaseHelper(this); // //获得数据引用 // SQLiteDatabase db=starbuzzDataBaseHelper.getReadableDatabase() ; try( SQLiteDatabase db=starbuzzDataBaseHelper.getReadableDatabase()){ //YOYO读取数据库 }catch(SQLiteException e){ Log.e("sqlite",e.getMessage()); Toast toast =Toast.makeText(this,"database unavailable",Toast.LENGTH_SHORT); toast.show() ; //若出现错误,用log.e打印error级的日志, 并弹出toast提示 } } }