SQLiteException:"occured" 附近:语法错误(代码 1)

SQLiteException: near "occured": syntax error (code 1)

我试图在我的数据库中的 table 中插入一行,但我被卡住了。插入函数返回 -1 并且 Logcat 显示以下错误:

05-06 20:58:45.446 13631-13631/com.example.android.disasterreporter E/SQLiteLog: (1) near "occured": syntax error

05-06 20:58:45.447 13631-13631/com.example.android.disasterreporter E/SQLiteDatabase: Error inserting disaster=3 last occured=5 location=loc predictedtime=2 prediction votes=1 votes for past=7 _id=2 predictedseverity=1 past severity=1

android.database.sqlite.SQLiteException: near "occured": syntax error (code 1): , while compiling: INSERT INTO disasters(disaster,last occured,location,predictedtime,prediction votes,votes for past,_id,predictedseverity,past severity) VALUES (?,?,?,?,?,?,?,?,?)

这是合同class:

public final class DisasterContract {

private DisasterContract() {}

public static final class DisasterEntry implements BaseColumns {

    public final static String TABLE_NAME = "disasters";

    public final static String _ID = BaseColumns._ID;
    public final static String COLUMN_DISASTER = "disaster";
    public final static String COLUMN_PAST_SEVERITY = "past severity";
    public final static String COLUMN_LOCATION = "location";
    public final static String COLUMN_LAST_OCCURED = "last occured";
    public final static String COLUMN_VOTES_FOR_PAST = "votes for past";
    public final static String COLUMN_PREDICTION_TIME = "predictedtime";
    public final static String COLUMN_PREDICTION_SEVERITY = "predictedseverity";
    public final static String COLUMN_PREDICTION_VOTES = "prediction votes";

    public static final int LOW = 0;
    public static final int MEDIUM = 1;
    public static final int HIGH = 2;
    public static final int EARTHQUAKE = 0;
    public static final int FLOOD = 1;
    public static final int LANDSLIDE = 2;
    public static final int TERROR_ATTACK = 3;

}}

这是我的帮手class:

public class DisasterDBHelper extends SQLiteOpenHelper {

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

public DisasterDBHelper (Context context) {
    super(context,DATABASE_NAME, null, DATABASE_VERSION);

}

@Override
public void onCreate(SQLiteDatabase db) {
    String SQL_CREATE_TABLE = "CREATE TABLE " + DisasterEntry.TABLE_NAME
            + " ( " + DisasterEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + DisasterEntry.COLUMN_DISASTER + " INTEGER NOT NULL, "
            + DisasterEntry.COLUMN_LOCATION + " TEXT NOT NULL, "
            + DisasterEntry.COLUMN_LAST_OCCURED + " INTEGER NOT NULL, "
            + DisasterEntry.COLUMN_PAST_SEVERITY + " INTEGER NOT NULL DEFAULT 1, "
            + DisasterEntry.COLUMN_VOTES_FOR_PAST + " INTEGER NOT NULL DEFAULT 1,"
            + DisasterEntry.COLUMN_PREDICTION_SEVERITY
            + " INTEGER NOT NULL DEFAULT 1,"
            + DisasterEntry.COLUMN_PREDICTION_TIME
            + " INTEGER NOT NULL DEFAULT 1,"
            + DisasterEntry.COLUMN_PREDICTION_VOTES
            + " INTEGER NOT NULL DEFAULT 1 );";
    db.execSQL(SQL_CREATE_TABLE);
}

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

}
}

这是显示错误的 SQL 语句:

ContentValues values = new ContentValues();

                                        values.put(DisasterContract.DisasterEntry._ID,2);
                values.put(DisasterContract.DisasterEntry.COLUMN_DISASTER, DisasterContract.DisasterEntry.TERROR_ATTACK);
    values.put(DisasterContract.DisasterEntry.COLUMN_LOCATION,"loc");
    values.put(DisasterContract.DisasterEntry.COLUMN_LAST_OCCURED, 5);
    values.put(DisasterContract.DisasterEntry.COLUMN_PAST_SEVERITY, DisasterContract.DisasterEntry.MEDIUM);
    values.put(DisasterContract.DisasterEntry.COLUMN_VOTES_FOR_PAST, 7);
    values.put(DisasterContract.DisasterEntry.COLUMN_PREDICTION_SEVERITY,1);
    values.put(DisasterContract.DisasterEntry.COLUMN_PREDICTION_TIME,2);
    values.put(DisasterContract.DisasterEntry.COLUMN_PREDICTION_VOTES,1);
          long newRowId = db.insert(DisasterContract.DisasterEntry.TABLE_NAME, null, values);
          Log.d("Row ID",""+newRowId);

SQL 列名称中不应包含空格。 将 COLUMN_LAST_OCCURED 从 last occured 更改为 last_occured,看看它是否适合您。

COLUMN_VOTES_FOR_PAST、COLUMN_PAST_SEVERITY 和 COLUMN_PREDICTION_VOTES 相同。