0

我正在尝试在数据库中插入一条记录,但出现错误。我没有得到什么问题。我通过删除 IMAGEPATH 列尝试了相同的代码,但它仍然为描述提供了相同的错误。

数据库助手:

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();
}

}

插入记录查询:

                   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(?, ?, ?, ?);
4

1 回答 1

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 " + ");";

Use this as your create table query. Note that you need to have space before TEXT like " TEXT". This will make it a column of type TEXT. Else the column will be image_columnTEXT

于 2012-08-26T13:57:35.587 回答