在今天我学安卓的时候,发现能建一个表但是不能建两个表,于是我通过询问同学,我发现在数据库如果第一次有错误的时候,在以下的操作中,数据库还是会错误的,进行删除,重新启动就行。
package com.example.mylogin.db; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import androidx.annotation.Nullable; import com.example.mylogin.Bean.Daka; import java.util.ArrayList; public class DakaHelper extends SQLiteOpenHelper { private SQLiteDatabase db; public DakaHelper(@Nullable Context context) { super(context, "db_test1", null, 1); db = getReadableDatabase(); } @Override public void onCreate(SQLiteDatabase db) { //在第一次创建数据库的时候,创建一些字段 String sql = "create table daka(_id integer,date varchar(50), keys varchar(40),zongjievarchar(40),day varchar(40),zui varchar(40))"; db.execSQL(sql); //sql语句的执行函数 } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //如果这个表中存在daka,我们可以先把他去掉,然后重新创建 String sql = "DROP TABLE IF EXISTS daka"; db.execSQL(sql); onCreate(db); } //为使项目结构更加紧凑,我们在此类中编写增删改查的函数,因为只有登录和注册界面,因此只涉及到写入数据库insert和query的操作 public void insert(String date,String keys,String zongjie,String day,String zui ){ db.execSQL("insert into daka(date,keys,zongjie,day,zui)VALUES(?,?,?,?,?)",new Object[]{date,keys,zongjie,day,zui}); } public ArrayList<Daka> getAllDATA(){//查询数据库 ArrayList<Daka> list = new ArrayList<Daka>(); //查询数据库中的数据,并将这些数据按照降序的情况排列 Cursor cursor = db.query("daka",null,null,null,null,null,"date DESC"); while(cursor.moveToNext()){ int index_date = cursor.getColumnIndex("date"); int index_keys = cursor.getColumnIndex("keys"); int index_zongjie = cursor.getColumnIndex("zongjie"); int index_day = cursor.getColumnIndex("day"); int index_zui = cursor.getColumnIndex("zui"); String date = cursor.getString(index_date); String keys = cursor.getString(index_keys); String zongjie= cursor.getString(index_zongjie); String day = cursor.getString(index_day); String zui = cursor.getString(index_zui); list.add(new Daka(date,keys,zongjie,day,zui)); } return list; } }
标签:总结,index,15,String,每日,db,cursor,date,day From: https://www.cnblogs.com/nanbei666/p/17220241.html