【问题标题】:Retrieving data from DB crashes从数据库崩溃中检索数据
【发布时间】:2019-10-30 09:14:37
【问题描述】:

我目前正在学习使用 SQLite 存储一些数据。目前的项目计划是一个笔记应用程序。启动应用程序时出现许多错误。一是不能插入数据,二是表格注释不存在。如果我留下代码以在登录中显示数据,应用程序就会崩溃。

我试图删除函数来检索数据并显示它,应用程序停止崩溃,但是,我仍然收到关于无法将数据插入表中的错误

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DatabaseHandler db = new DatabaseHandler(this);
            db.AddNote(new Note("12313", "jkfsabk"));

        List<Note> notes = db.getAllNotes();

        for (Note c: notes){
            String log = "NAME: " + c.getNoteName() + " DESCRIPTION: " + c.getNoteDescription() + "\n";
            text = text + log;
        }
        System.out.println(text);
public class DatabaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "notesDB";
    private static final String TABLE_NAME = "notes";

    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_DESCRIPTION = "description";


    public DatabaseHandler(Context context) {
        super(context, Util.DATABASE_NAME, null, Util.DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACT_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + KEY_ID + " INTEGER PRIMARY KEY ," + KEY_NAME + " TEXT," + KEY_DESCRIPTION + " TEXT)";
        db.execSQL(CREATE_CONTACT_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

        onCreate(db);
    }

    public void AddNote(Note note){
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues value = new ContentValues();
        value.put(KEY_NAME, note.getNoteName());
        value.put(KEY_DESCRIPTION, note.getNoteDescription());

        db.insert(TABLE_NAME, null, value);
        db.close();
    }

    Note getNote(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_NAME, new String[]{KEY_ID, KEY_NAME, KEY_DESCRIPTION}, KEY_ID
                + "=?", new String[]{String.valueOf(id)}, null, null, null, null);
        if (cursor != null){
            cursor.moveToFirst();
        }

        Note note = new Note(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2));

        return note;
    }

    public List<Note> getAllNotes(){
        List<Note> noteList = new ArrayList<>();

        String selectQuery = "SELECT  * FROM " + TABLE_NAME;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if(cursor.moveToFirst()){
            do{
                Note note = new Note();
                note.setID(Integer.parseInt(cursor.getString(0)));
                note.setNoteName(cursor.getString(1));
                note.setNoteDescription(cursor.getString(2));

                noteList.add(note);
            } while(cursor.moveToNext());

        } cursor.close();

        return noteList;
    }

    public int updateNotes(Note note){
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, note.getNoteName());
        values.put(KEY_DESCRIPTION, note.getNoteDescription());

        return db.update(TABLE_NAME, values, KEY_ID + "=?", new String[]{String.valueOf(note.getID())});
    }

    public void deleteNote(Note note){
        SQLiteDatabase db = this.getWritableDatabase();

        db.delete(TABLE_NAME, KEY_ID + "=?", new String[]{String.valueOf(note.getID())});
        db.close();
    }

    public int getNoteCount(Note note){
        String countQuery = "SELECT  * FROM " + TABLE_NAME;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();


        return cursor.getCount();
    }
}
public class Note {
    private int noteID;
    private String noteName;
    private String noteDescription;
    public Note() {}
    public Note(Integer id, String notename, String notedescription) {
        this.noteID = id;
        this.noteName = notename;
        this.noteDescription = notedescription;
    }

    public Note(String notename, String notedescription){
        this.noteName = notename;
        this.noteDescription = notedescription;
    }
    public void setID(int id) {
        this.noteID = id;
    }
    public int getID() {
        return this.noteID;
    }
    public void setNoteName(String notename) {
        this.noteName = notename;
    }
    public String getNoteName() {
        return this.noteName;
    }
    public void setNoteDescription(String notedescription) {
        this.noteDescription = notedescription;
    }
    public String getNoteDescription() {
        return this.noteDescription;
    }


}

我期待输出

名称:12313 描述:jkfsabk,

但我得到了错误:

2019-06-16 18:03:57.155 24382-24382/com.example.myapplication E/libc: Access denied finding property "vendor.perf.iop_v3.enable"
2019-06-16 18:03:57.155 24382-24382/com.example.myapplication E/libc: Access denied finding property "vendor.perf.iop_v3.enable.debug"
2019-06-16 18:03:57.165 24382-24382/com.example.myapplication E/libc: Access denied finding property "vendor.iop.enable_uxe"
2019-06-16 18:03:57.172 24382-24676/com.example.myapplication E/libc: Access denied finding property "vendor.debug.egl.profiler"
2019-06-16 18:03:57.172 24382-24676/com.example.myapplication E/libc: Access denied finding property "vendor.debug.prerotation.disable"
2019-06-16 18:03:57.386 24382-24382/com.example.myapplication E/libc: Access denied finding property "vendor.perf.gestureflingboost.enable"
2019-06-16 18:03:57.390 24382-24382/com.example.myapplication E/libc: Access denied finding property "vendor.perf.iop_v3.enable.debug"
2019-06-16 18:03:57.440 24382-24439/com.example.myapplication E/libc: Access denied finding property "vendor.debug.prerotation.disable"
2019-06-16 18:03:57.441 24382-24439/com.example.myapplication E/libc: Access denied finding property "ro.vendor.graphics.memory"
2019-06-16 18:03:57.441 24382-24439/com.example.myapplication E/libc: Access denied finding property "vendor.debug.prerotation.disable"
2019-06-16 18:03:57.441 24382-24439/com.example.myapplication E/libc: Access denied finding property "vendor.debug.prerotation.disable"
2019-06-16 18:03:57.442 24382-24439/com.example.myapplication E/libc: Access denied finding property "vendor.debug.prerotation.disable"
2019-06-16 18:03:57.449 24382-24439/com.example.myapplication E/libc: Access denied finding property "vendor.debug.egl.changepixelformat"
2019-06-16 18:03:57.482 24382-24439/com.example.myapplication E/libc: Access denied finding property "vendor.debug.egl.swapinterval"
2019-06-16 18:03:57.497 24382-24439/com.example.myapplication E/libc: Access denied finding property "vendor.debug.egl.swapinterval"
2019-06-16 18:03:57.518 24382-24439/com.example.myapplication E/libc: Access denied finding property "vendor.debug.egl.swapinterval"
2019-06-16 18:03:57.527 24382-24439/com.example.myapplication E/libc: Access denied finding property "vendor.debug.egl.swapinterval"
2019-06-16 18:03:57.544 24382-24439/com.example.myapplication E/libc: Access denied finding property "vendor.debug.egl.swapinterval"
2019-06-16 18:03:58.042 24382-24439/com.example.myapplication E/libc: Access denied finding property "vendor.debug.egl.swapinterval"
2019-06-16 18:03:58.542 24382-24439/com.example.myapplication E/libc: Access denied finding property "vendor.debug.egl.swapinterval"
2019-06-16 18:03:59.042 24382-24439/com.example.myapplication E/libc: Access denied finding property "vendor.debug.egl.swapinterval"
2019-06-16 18:03:59.561 24382-24439/com.example.myapplication E/libc: Access denied finding property "vendor.debug.egl.swapinterval"
2019-06-16 18:04:00.078 24382-24439/com.example.myapplication E/libc: Access denied finding property "vendor.debug.egl.swapinterval"
2019-06-16 18:04:00.573 24382-24439/com.example.myapplication E/libc: Access denied finding property "vendor.debug.egl.swapinterval"
2019-06-16 18:04:01.097 24382-24439/com.example.myapplication E/libc: Access denied finding property "vendor.debug.egl.swapinterval"
2019-06-16 18:04:01.593 24382-24439/com.example.myapplication E/libc: Access denied finding property "vendor.debug.egl.swapinterval"
2019-06-16 18:04:02.082 24382-24439/com.example.myapplication E/libc: Access denied finding property "vendor.debug.egl.swapinterval"

【问题讨论】:

  • 刚开始,从设备上卸载应用程序并重新运行。如果应用仍然崩溃,请发布错误日志。
  • 重新安装应用程序后,它停止崩溃,但数据仍未显示在日志中,而是出现错误:2019-06-16 17:55:51.086 23497-23556/com.example。 myapplication E/libc:访问被拒绝查找属性“vendor.debug.egl.swapinterval” 2019-06-16 17:55:51.077 23497-23497/com.example.myapplication W/RenderThread: type=1400 audit(0.0:1030970) : avc: 拒绝 { 读取} for name="u:object_r:vendor_default_prop:s0" dev="tmpfs" ino=15872 scontext=u:r:untrusted_app:s0:c11,c259,c512,c768 tcontext=u:object_r: vendor_default_prop:s0 tclass=文件许可=0
  • 非常感谢,我的问题解决了。

标签: java android sqlite insert


【解决方案1】:

我对您的代码进行了一些更正,我尝试使用此代码,然后得到您想要的结果。试试看。

1- DatabaseHandler 类

public class DatabaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "notesDB";
    private static final String TABLE_NAME = "notes";

    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_DESCRIPTION = "description";


    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME , null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACT_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + KEY_ID + " INTEGER PRIMARY KEY ," + KEY_NAME + " TEXT," + KEY_DESCRIPTION + " TEXT)";
        db.execSQL(CREATE_CONTACT_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

        onCreate(db);
    }

    public void AddNote(Note note){
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues value = new ContentValues();
        value.put(KEY_NAME, note.getNoteName());
        value.put(KEY_DESCRIPTION, note.getNoteDescription());

        db.insert(TABLE_NAME, null, value);
        db.close();
    }

    Note getNote(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_NAME, new String[]{KEY_ID, KEY_NAME, KEY_DESCRIPTION}, KEY_ID
                + "=?", new String[]{String.valueOf(id)}, null, null, null, null);
        if (cursor != null){
            cursor.moveToFirst();
        }

        Note note = new Note(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2));

        return note;
    }

    public ArrayList<Note> getAllNotes(){
        ArrayList<Note> noteList = new ArrayList<>();

        String selectQuery = "SELECT  * FROM " + TABLE_NAME;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if(cursor.moveToFirst()){
            do{
                Note note = new Note();
                note.setID(Integer.parseInt(cursor.getString(0)));
                note.setNoteName(cursor.getString(1));
                note.setNoteDescription(cursor.getString(2));

                noteList.add(note);
            } while(cursor.moveToNext());

        } cursor.close();

        return noteList;
    }

    public int updateNotes(Note note){
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, note.getNoteName());
        values.put(KEY_DESCRIPTION, note.getNoteDescription());

        return db.update(TABLE_NAME, values, KEY_ID + "=?", new String[]{String.valueOf(note.getID())});
    }

    public void deleteNote(Note note){
        SQLiteDatabase db = this.getWritableDatabase();

        db.delete(TABLE_NAME, KEY_ID + "=?", new String[]{String.valueOf(note.getID())});
        db.close();
    }

    public int getNoteCount(Note note){
        String countQuery = "SELECT  * FROM " + TABLE_NAME;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();


        return cursor.getCount();
    }
}


2- MainActivity 类



 setContentView(R.layout.activity_main);
        DatabaseHandler db = new DatabaseHandler(this);
        db.AddNote(new Note("12313", "jkfsabk"));

        ArrayList<Note> notes = db.getAllNotes();
        String log = null;
        for (Note c: notes){
             log = "NAME: " + c.getNoteName() + " DESCRIPTION: " + c.getNoteDescription() + "\n";
          //  text = text + log;
        }
        Toast.makeText(this, log+"" , Toast.LENGTH_SHORT).show(); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    相关资源
    最近更新 更多