android.database.sqlite.SQLiteException: 没有这样 table: SimpleTable (code 1 SQLITE_ERROR[1]): , 编译时: SELECT * FROM SimpleTable

android.database.sqlite.SQLiteException: no such table: SimpleTable (code 1 SQLITE_ERROR[1]): , while compiling: SELECT * FROM SimpleTable

I have tried all of the solutions I could find and still haven't been able to get around this error. The error occurs in the getNotes() section, in its forth line. Also, the getNote() is currently not doing anything, so you don't need to look at that. I directly copied all code from these youtube tutorials, which I then adapted to my needs.

https://www.youtube.com/watch?v=pa_lghjVQVA&t=933s

Thanks for any help.


import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Build;
import android.provider.ContactsContract;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class NoteDatabase extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 2;
    private static final String DATABASE_NAME = "SimpleDB";
    private static final String TABLE_NAME = "SimpleTable";

    //nome das colunas da tabela
    private static final String KEY_ID = "_id";
    private static final String KEY_TITLE = "title";
    private static final String KEY_LOCATION = "location";
    private static final String KEY_STARS = "stars";
    private static final String KEY_OPENING_HOURS_1 = "opening_hours_1";
    private static final String KEY_OPENING_HOURS_2 = "opening_hours_2";
    private static final String KEY_OPENING_HOURS_3 = "opening_hours_3";
    private static final String KEY_NOTAS = "notas";

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


    @Override
    public void onCreate(SQLiteDatabase db) {
        String createDB = "CREATE TABLE " + TABLE_NAME + " (" + KEY_ID + "INTEGER PRIMARY KEY," +
                KEY_TITLE + "TEXT," +
                KEY_LOCATION + "TEXT," +
                KEY_STARS + "TEXT," +
                KEY_OPENING_HOURS_1 + "TEXT," +
                KEY_OPENING_HOURS_2 + "TEXT," +
                KEY_OPENING_HOURS_3 + "TEXT," +
                KEY_NOTAS + "TEXT" + " )";
        db.execSQL(createDB);

    }

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

    }

    public long addNote(Note note) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues v = new ContentValues();
        v.put(KEY_TITLE, note.getTitle());
        v.put(KEY_LOCATION, note.getLocation());
        v.put(KEY_STARS, note.getStars());
        v.put(KEY_OPENING_HOURS_1, note.getOpening_hours_1());
        v.put(KEY_OPENING_HOURS_2, note.getOpening_hours_2());
        v.put(KEY_OPENING_HOURS_3, note.getOpening_hours_3());
        v.put(KEY_NOTAS, note.getNotas());

        long ID = db.insert(TABLE_NAME, null, v);
        Log.d("Inserted", "ID ->" + ID);
        return ID;
    }

    public Note getNote(long id) {
        //select * from database table where id=1
        SQLiteDatabase db = this.getWritableDatabase();
        String[] query = new String[]{KEY_ID, KEY_TITLE, KEY_STARS,
                KEY_OPENING_HOURS_1, KEY_OPENING_HOURS_2, KEY_OPENING_HOURS_3, KEY_NOTAS};
        Cursor cursor = db.query(TABLE_NAME, query, KEY_ID + "=?",
                new String[]{String.valueOf(id)}, null, null, null, null);

        if (cursor != null)
            cursor.moveToFirst();

        return new Note(cursor.getLong(0), cursor.getString(1), cursor.getString(2),
                cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6),
                cursor.getString(7));
    }



     public List<Note> getNotes() {
         SQLiteDatabase db = this.getReadableDatabase();
         List<Note> allNotes = new ArrayList<>();
         //select from databaseName
         String query = "SELECT * FROM " + TABLE_NAME;
         Cursor cursor = db.rawQuery(query, null);
         if (cursor.moveToFirst()) {
             do {
                 Note note = new Note();
                 note.setID(cursor.getLong(0));
                 note.setTitle(cursor.getString(1));
                 note.setLocation(cursor.getString(2));
                 note.setStars(cursor.getString(3));
                 note.setOpening_hours_1(cursor.getString(4));
                 note.setOpening_hours_2(cursor.getString(5));
                 note.setOpening_hours_3(cursor.getString(6));
                 note.setNotas(cursor.getString(7));

                 allNotes.add(note);

             } while (cursor.moveToNext());
         }

         return allNotes;
     }

}```

我假设您一直在关注逐步构建数据库处理的视频。

我已经使用了你的代码,添加了一个注释 class 这样它就可以编译并稍微尝试一下。代码本身工作正常,但是您(可能对代码进行了增量更改)删除了 table 并且尚未重新创建它。因此它总是会导致崩溃。

幸运的是,这可以很容易地解决:

  • 要么重新安装应用程序
  • 或者通过增加数据库版本

如果您选择删除并重新安装应用程序,您可以将数据库版本重置为1。

如果您选择增加数据库版本,将调用 onUpgrade,这将删除 table(如果存在),然后重新创建它(通过调用 onCreate)。

正如有人在评论中提到的那样,SQL 现在应该使用 Room 在 Android 上处理。我知道您可能刚刚起步,但是一旦您变得更好,您一定要看看 Room。它使事情变得容易得多。

希望这能解决您的问题:)