在android中获取错误代码= 1,未创建列

时间:2012-08-26 13:50:25

标签: android sqlite

我正在尝试在数据库中插入记录但是我收到错误。我没有得到什么问题。我通过删除IMAGEPATH列尝试了相同的代码,但它仍然给出了相同的错误。

DataBaseHelper:

public class GREDatabaseHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "gre_db.db";
private static final int DATABASE_VERSION = 1;

public static final String TABLE_NAME_ENGLISH = "english_table";
public static final String COLUMN_NAME_WORD = "word_column";
public static final String COLUMN_NAME_SYNONYM = "synonym_column";
public static final String COLUMN_NAME_DESCRIPTION = "descrption_column";
public static final String COLUMN_NAME_IMAGEPATH = "image_column";

GREDatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

    String sqlQueryToCreateEnglishTable = "CREATE TABLE IF NOT EXISTS "
            + TABLE_NAME_ENGLISH + " (" + COLUMN_NAME_WORD
            + "TEXT PRIMARY KEY, " + COLUMN_NAME_SYNONYM + "TEXT, "
            + COLUMN_NAME_DESCRIPTION + "TEXT, " + COLUMN_NAME_IMAGEPATH
            + "TEXT " + ");";
    db.execSQL(sqlQueryToCreateEnglishTable);

    Log.d("Table_Created", "english_table");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

@Override
public void onOpen(SQLiteDatabase db) {
    super.onOpen(db);
}

void addWord(EngFieldsClass obj) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(COLUMN_NAME_WORD, obj.getengWord());
    cv.put(COLUMN_NAME_SYNONYM, obj.getengSynonyms());
    cv.put(COLUMN_NAME_DESCRIPTION, obj.getengDescription());
    cv.put(COLUMN_NAME_IMAGEPATH, obj.getengImage());
    // INSERT ROWS
    db.insertOrThrow(TABLE_NAME_ENGLISH, null, cv);
    db.close();
}

}

InsertRecord查询:

                   String word=editTextWord.getText().toString();
        String synonym= editText Synonym.getText().toString();
        String description= editText Description.getText().toString();
        String imgpath=textViewImagePath.getText().toString();

        final GREDatabaseHelper db= new GREDatabaseHelper(this);
        db.addWord(new EngFieldsClass(word,synonym,description,imgpath));

错误:

08-26 19:03:36.230: D/Table_Created(1350): english_table
08-26 19:03:36.239: I/Database(1350): sqlite returned: error code = 1, msg = table english_table has no column named image_column
08-26 19:03:36.249: D/AndroidRuntime(1350): Shutting down VM
08-26 19:03:36.259: W/dalvikvm(1350): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-26 19:03:36.269: E/AndroidRuntime(1350): FATAL EXCEPTION: main
08-26 19:03:36.269: E/AndroidRuntime(1350): android.database.sqlite.SQLiteException: table english_table has no column named image_column: , while compiling: INSERT INTO english_table(image_column, descrption_column, synonym_column, word_column) VALUES(?, ?, ?, ?);

1 个答案:

答案 0 :(得分:3)

String sqlQueryToCreateEnglishTable = "CREATE TABLE IF NOT EXISTS "
            + TABLE_NAME_ENGLISH + " (" + COLUMN_NAME_WORD
            + " TEXT PRIMARY KEY, " + COLUMN_NAME_SYNONYM + " TEXT, "
            + COLUMN_NAME_DESCRIPTION + " TEXT, " + COLUMN_NAME_IMAGEPATH
            + " TEXT " + ");";

将此用作创建表查询。请注意,您需要在TEXT之前有空格,如“TEXT”。这将使它成为TEXT类型的列。否则该列将是image_columnTEXT