yunniw
yunniw
采纳率100%
2020-09-01 09:55

android studio 用listview获取sqlite数据库中数据时软件闪退

这是发生闪退处的代码,每次运行到cursor就会闪退

HistoryActivity是所在的activity

DataBaseHelper helper=new DataBaseHelper(HistoryActivity.this);
SQLiteDatabase db=helper.getWritableDatabase();
 Cursor cursor=db.query("AccountingRecord",null,null,null,null,null,null);

DataBaseHelper:

public class DataBaseHelper extends SQLiteOpenHelper{
    static String name="AccountingRecord.db";
    static int dbVersion=1;
    public DataBaseHelper(Context context){
        super(context,name,null,dbVersion);
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String sql="create table AccountRecord(id integer primary key autoincrement,type varchar(20),state varchar(20),income double,date varchar(20))";
        sqLiteDatabase.execSQL(sql);
    }
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {

    }
}

报错日志:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simpleaccount/com.example.simpleaccount.HistoryActivity}: android.database.sqlite.SQLiteException: no such table: AccountingRecord (Sqlite code 1 SQLITE_ERROR): , while compiling: SELECT * FROM AccountingRecord, (OS error - 2:No such file or directory)
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • caozhy 回答这么多问题就耍赖把我的积分一笔勾销了 7月前

    SQLiteException: no such table: AccountingRecord
    要么是没有找到数据库文件,要么是数据库没有 AccountingRecord 表
    检查下

    public void onCreate(SQLiteDatabase sqLiteDatabase) {
    String sql="create table AccountRecord(id integer primary key autoincrement,type varchar(20),state varchar(20),income double,date varchar(20))";
    sqLiteDatabase.execSQL(sql);
    }
    这里是 AccountRecord 不是 AccountingRecord

    点赞 3 评论 复制链接分享
  • as_csdn 代码的灵魂是bug! 7月前

    Cursor cursor=db.query("AccountingRecord",null,null,null,null,null,null);
    String sql="create table AccountRecord(id integer primary key autoincrement,type varchar(20),state varchar(20),income double,date varchar(20))";
    表写错了,统一一下

    点赞 评论 复制链接分享