gpt4 book ai didi

java - Android Sqlite 数据库错误 - 无法创建另一个表

转载 作者:行者123 更新时间:2023-12-01 12:40:38 25 4
gpt4 key购买 nike

我知道有很多关于这个问题的帖子,但我已经尝试了其中的几个,但当我调用 getHomework() 方法时,我仍然收到此错误。

Error:
android.database.sqlite.SQLiteException: no such table: homework (code 1): , while compiling: SELECT * FROM homework

DatabaseHelper - 这些是我认为错误所在的方法:

public void getHomework() {
SQLiteDatabase localSQLiteDatabase = getReadableDatabase();
String[] tblName = {};
Cursor localCursor = localSQLiteDatabase.rawQuery("SELECT * FROM homework",tblName);

localCursor.moveToFirst();
while (localCursor.isAfterLast() == false)
{

HashMap<String, String> homework = new HashMap<String, String>();
homework.put("Subject", localCursor.getString(0));
homework.put("DueDate", localCursor.getString(1));
net.attwoodthomas.mytimetable.app.FourthActivity.mHomeworkDue.add(homework);
localCursor.moveToNext();
}


}

public void getLessons()
{
SQLiteDatabase localSQLiteDatabase = getReadableDatabase();
String[] arrayOfString = new String[2];
arrayOfString[0] = MainActivity.mWeek;
arrayOfString[1] = MainActivity.mDay;
Cursor localCursor = localSQLiteDatabase.rawQuery("SELECT Period1, Period2, Period3, Period4, Period5, Period6 FROM lessons WHERE Week = ? AND day = ?", arrayOfString);
Log.d("DatabaseHelper", "1");
localCursor.moveToNext();
Log.d("DatabaseHelper", "2");
net.attwoodthomas.mytimetable.app.SecondActivity.period1 = localCursor.getString(localCursor.getColumnIndex("Period1"));
Log.d("DatabaseHelper", "3");
net.attwoodthomas.mytimetable.app.SecondActivity.period2 = localCursor.getString(localCursor.getColumnIndex("Period2"));
Log.d("DatabaseHelper", "4");
net.attwoodthomas.mytimetable.app.SecondActivity.period3 = localCursor.getString(localCursor.getColumnIndex("Period3"));
Log.d("DatabaseHelper", "5");
net.attwoodthomas.mytimetable.app.SecondActivity.period4 = localCursor.getString(localCursor.getColumnIndex("Period4"));
Log.d("DatabaseHelper", "6");
net.attwoodthomas.mytimetable.app.SecondActivity.period5 = localCursor.getString(localCursor.getColumnIndex("Period5"));
Log.d("DatabaseHelper", "7");
net.attwoodthomas.mytimetable.app.SecondActivity.period6 = localCursor.getString(localCursor.getColumnIndex("Period6"));
Log.d("DatabaseHelper", "8");
localCursor.close();
}

public void onCreate(SQLiteDatabase paramSQLiteDatabase)
{
paramSQLiteDatabase.execSQL("CREATE TABLE lessons (Week VARCHAR(1), Day VARCHAR(10), Period1 VACHAR(20),Period2 VARCHAR(20),Period3 VARCHAR(20),Period4 VARCHAR(20),Period5 VARCHAR(20),Period6 VARCHAR(20));");
paramSQLiteDatabase.execSQL("CREATE TABLE homework (Subject VARCHAR(50), DateDue VARCHAR(50), Description VARCHAR(100));");
if (paramSQLiteDatabase.rawQuery("SELECT * FROM lessons", null).moveToFirst())
{
Log.d("DatabaseHelper", "not emtpy");
return;
}
Log.d("DatabaseHelper", "emtpy");
paramSQLiteDatabase.execSQL("INSERT INTO lessons VALUES ('A', 'Monday', 'PE', 'PE', 'SE', 'Reading', 'Drama', 'Music'), ('A', 'Tuesday', 'PE', 'PE', 'SE', 'Reading', 'Drama', 'Music'),('A', 'Wednesday', 'PE', 'PE', 'SE', 'Reading', 'Drama', 'Music'),('A', 'Thursday', 'PE', 'PE', 'SE', 'Reading', 'Drama', 'Music'),('A', 'Friday', 'PE', 'PE', 'SE', 'Reading', 'Drama', 'Music'),('B', 'Monday', 'PE', 'PE', 'SE', 'Reading', 'Drama', 'Music'),('B', 'Tuesday', 'PE', 'PE', 'SE', 'Reading', 'Drama', 'Music'),('B', 'Wednesday', 'PE', 'PE', 'SE', 'Reading', 'Drama', 'Music'), ('B', 'Thursday', 'PE', 'PE', 'SE', 'Reading', 'Drama', 'Music'),('B', 'Friday', 'PE', 'PE', 'SE', 'Reading', 'Drama', 'Music');");
paramSQLiteDatabase.execSQL("INSERT INTO homework VALUES ('Maths', '08.03.04', 'Finish p38');");
}

public void onUpgrade(SQLiteDatabase paramSQLiteDatabase, int paramInt1, int paramInt2)
{
paramSQLiteDatabase.execSQL("DROP TABLE IF EXISTS CREATE TABLE lessons (Week VARCHAR(1), Day VARCHAR(10), Period1 VACHAR(20),Period2 VARCHAR(20),Period3 VARCHAR(20),Period4 VARCHAR(20),Period5 VARCHAR(20),Period6 VARCHAR(20));");
paramSQLiteDatabase.execSQL("DROP TABLE IF EXISTS CREATE TABLE homework (Subject VARCHAR(50), DateDue VARCHAR(50), Description VARCHAR(100));");
onCreate(paramSQLiteDatabase);
}

整个项目可在 github 上获取:https://github.com/Ottermad/MyPlanner

如果我提供了太多/太少的信息,我很抱歉这是我的第一篇文章。谢谢

最佳答案

OnUpgrade 中的 DROP TABLE IF EXISTS 查询不正确。

语法是:

DROP TABLE IF EXISTS <DATABASE_NAME>.<TABLE_NAME>

关于java - Android Sqlite 数据库错误 - 无法创建另一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25136816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com