android.database.sqlite.SQLiteException:没有这样的表:SimpleTable(代码1 SQLITE_ERROR [1]):,而在编译时:SELECT * FROM SimpleTable

问题描述 投票:1回答:1

我已经尝试了所有可以找到的解决方案,但仍然无法解决此错误。该错误发生在第四行的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;
     }

}```
android android-studio android-studio-3.0
1个回答
0
投票

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

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

幸运的是,可以很容易地通过以下方法解决:

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

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

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

正如已经有人提到的那样,现在应该在Android上使用Room处理SQL。我了解您可能才刚刚起步,但是一旦病情好转,一定要退房。这使事情变得更加容易。

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

© www.soinside.com 2019 - 2024. All rights reserved.