android - 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;
}
}```
解决方案
我假设您一直在关注逐步建立数据库处理的视频。
我已经获取了您的代码,添加了一个 Note 类,以便它可以编译和使用它。代码本身可以正常工作,但是您(可能对代码的增量更改)删除了该表并且尚未重新创建它。因此,它总是会导致崩溃。
幸运的是,这可以通过以下方式轻松解决:
- 要么重新安装应用程序
- 或者通过增加数据库版本
如果您选择删除并重新安装该应用程序,您可以将数据库版本重置为1。
如果您选择增加数据库版本,onUpgrade
将调用它将删除表(如果存在)然后重新创建它(通过调用onCreate
)。
正如有人已经在评论中提到的那样,Room
现在应该在 Android 上使用 SQL 来处理 SQL。我知道您可能刚刚开始,但是一旦您变得更好,您绝对应该去看看 Room。它让事情变得容易多了。
希望这可以解决您的问题:)
推荐阅读
- python-3.x - 如何将逗号分隔的数据读入没有列标题的数据框中?
- docker - 一次使用多个 docker 镜像
- jmeter - 如何在 JMeter 中多次运行 Postman 测试
- java - 错误构建项目spring org.springframework.beans.factory.UnsatisfiedDependencyException
- r - 为 scale_x_continous 标签的特定元素加下划线
- ios - ExpoKit:安装 expo-in-app-purchase 后出现“本机模块不能为空”错误
- excel - 使用从“dd/MM/yyyy”格式的单元格中获取的日期填充用户表单文本框
- python-3.x - 从列表索引中的每个字符串中提取首字母缩写词
- perforce - Deleted (not submitted) directory in Perforce: cannot revert, force-sync, reconcile or do anything else
- sql - 将字符串格式的日期转换为日期数据类型