有谁知道为什么我会收到上述错误?
这是代码:
这是包含我所有数据库信息的类:
package com.petroc.nationaldiploma;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MySQLiteHelper extends SQLiteOpenHelper {
public static final String TABLE_GRADES = "grades";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_GRADE = "grade";
public static final String COLUMN_MODULE = "module";
private static final String DATABASE_NAME = "grades.db";
private static final int DATABASE_VERSION = 1;
// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_GRADES + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_GRADE
+ " text not null" + COLUMN_MODULE + " text not null" + ");";
/*
* private static final String DATABASE_CREATE = "create table " +
* TABLE_GRADES + "(" + COLUMN_ID + " integer primary key autoincrement, " +
* COLUMN_GRADE + " text not null);";
*/
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_GRADES);
onCreate(db);
}
}
这是创建用于创建和操作数据库的方法的类
package com.petroc.nationaldiploma;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class GradesDataSource {
// Database fields
private SQLiteDatabase database;
private final MySQLiteHelper dbHelper;
private final String[] allColumns = { MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_GRADE, MySQLiteHelper.COLUMN_MODULE };
/*private final String[] allColumns = { MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_GRADE };*/
public GradesDataSource(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public Grade createGrade(String grade, String module) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_GRADE, grade);
values.put(MySQLiteHelper.COLUMN_MODULE, module);
long insertId = database.insert(MySQLiteHelper.TABLE_GRADES, null,
values);
Cursor cursor = database.query(MySQLiteHelper.TABLE_GRADES, allColumns,
MySQLiteHelper.COLUMN_ID + " = " + insertId, null, null, null,
null);
cursor.moveToFirst();
Grade newGrade = cursorToGrade(cursor);
cursor.close();
return newGrade;
}
/*
* public Grade createGrade(String grade) { ContentValues values = new
* ContentValues(); values.put(MySQLiteHelper.COLUMN_GRADE, grade); long
* insertId = database.insert(MySQLiteHelper.TABLE_GRADES, null, values);
* Cursor cursor = database.query(MySQLiteHelper.TABLE_GRADES, allColumns,
* MySQLiteHelper.COLUMN_ID + " = " + insertId, null, null, null, null);
* cursor.moveToFirst(); Grade newGrade = cursorToGrade(cursor);
* cursor.close(); return newGrade; }
*
* public void deleteGrade(Grade grade) { long id = grade.getId();
* System.out.println("Grade deleted with id: " + id);
* database.delete(MySQLiteHelper.TABLE_GRADES, MySQLiteHelper.COLUMN_ID +
* " = " + id, null); }
*/
public List<Grade> getAllGrades() {
List<Grade> grades = new ArrayList<Grade>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_GRADES, allColumns,
null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Grade grade = cursorToGrade(cursor);
grades.add(grade);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return grades;
}
private Grade cursorToGrade(Cursor cursor) {
Grade grade = new Grade();
grade.setId(cursor.getLong(0));
grade.setGrade(cursor.getString(1));
return grade;
}
}
答案 0 :(得分:0)
在cursorToGrade()函数中,从db获取值时,你应该使用类似这样的东西而不是硬编码0&amp; 1。
grade.setId(cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.COLUMN_ID ));
grade.setGrade(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.COLUMN_MODULE));
答案 1 :(得分:0)
在null之后添加空格:
+ " text not null, " + COLUMN_MODULE + " text not null" + ");";
答案 2 :(得分:0)
您在字符串中的列名后忘记了一个简单的,
。这就是你获得Column Not Found
的原因。改变
private static final String DATABASE_CREATE = "create table "
+ TABLE_GRADES + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_GRADE
+ " text not null" + COLUMN_MODULE + " text not null" + ");";
到
private static final String DATABASE_CREATE = "create table "
+ TABLE_GRADES + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_GRADE
+ " text not null, " + COLUMN_MODULE + " text not null" + ");";
希望这有帮助。
答案 3 :(得分:0)
请参阅此列 COLUMN_GRADE +“text not null”在null之后缺少逗号(,)和空格。
private static final String DATABASE_CREATE = "create table " + TABLE_GRADES
+ "(" + COLUMN_ID + " integer primary key autoincrement, "
+ COLUMN_GRADE + " text not null"
+ COLUMN_MODULE + " text not null" + ");";
你的创建表查询错误首先请更正。
private static final String DATABASE_CREATE = "create table " + TABLE_GRADES + "("
+ COLUMN_ID + " integer primary key autoincrement, "
+ COLUMN_GRADE + " text not null, "
+ COLUMN_MODULE + " text not null" + ");";
更正此创建查询并删除数据库或卸载您的应用并再次运行您的应用并进行检查。
我希望它可以帮到你。