SQLiteException:near"出现":语法错误(代码1)

时间:2018-05-06 16:01:19

标签: android android-sqlite android-sql

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

  

05-06 20:58:45.446 13631-13631 / com.example.android.disasterreporter E / SQLiteLog:(1)near"出现":语法错误

     

05-06 20:58:45.447 13631-13631 / com.example.android.disasterreporter E / SQLiteDatabase:插入灾难时出错= 3最后出现= 5位置= loc predicttime = 2预测票数= 1票过去= 7 _id = 2 predictseverity = 1过去的严重性= 1

     

android.database.sqlite.SQLiteException:near"出现&#34 ;:语法错误(代码1):,编译时:INSERT INTO灾难(灾难,上次发生,位置,预测时间,预测投票,投票过去,_id,预测严重性,过去的严重程度)价值观(?,?,?,?,?,?,?,?,?)

这是合同类:

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;

}}

这是我的助手班:

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

1 个答案:

答案 0 :(得分:2)

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

相同的COLUMN_VOTES_FOR_PAST,COLUMN_PAST_SEVERITY和COLUMN_PREDICTION_VOTES。