2

有谁知道我为什么会收到上述错误?

这是代码:

这是包含我所有数据库信息的类:

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

4 回答 4

0

在 null 后添加一个空格:

+ " text not null, " + COLUMN_MODULE + " text not null" + ");";
于 2013-04-23T02:36:52.720 回答
0

在 cursorToGrade() 函数中,从 db 获取值时,您应该使用类似这样的东西,而不是硬编码 0 和 1。

grade.setId(cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.COLUMN_ID ));
grade.setGrade(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.COLUMN_MODULE));
于 2013-04-23T02:05:33.703 回答
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" + ");";

更正此创建查询并删除数据库或卸载您的应用程序并再次运行您的应用程序并检查它。

我希望它对你有所帮助。

于 2013-04-23T07:11:18.170 回答
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" + ");";

希望这可以帮助。

于 2013-04-23T04:27:45.607 回答