首页 > 解决方案 > android.database.sqlite.SQLiteException: no such table: SimpleTable (code 1 SQLITE_ERROR[1]): , while compile: SELECT * FROM SimpleTable

问题描述

我已经尝试了所有我能找到的解决方案,但仍然无法解决这个错误。错误发生在 getNotes() 部分的第四行。此外, getNote() 目前没有做任何事情,所以你不需要看那个。我直接从这些 youtube 教程中复制了所有代码,然后根据我的需要进行了调整。

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

谢谢你的帮助。


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;
     }

}```

标签: androidandroid-studioandroid-studio-3.0

解决方案


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

我已经获取了您的代码,添加了一个 Note 类,以便它可以编译和使用它。代码本身可以正常工作,但是您(可能对代码的增量更改)删除了该表并且尚未重新创建它。因此,它总是会导致崩溃。

幸运的是,这可以通过以下方式轻松解决:

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

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

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

正如有人已经在评论中提到的那样,Room现在应该在 Android 上使用 SQL 来处理 SQL。我知道您可能刚刚开始,但是一旦您变得更好,您绝对应该去看看 Room。它让事情变得容易多了。

希望这可以解决您的问题:)


推荐阅读