Android SQLite数据库:添加到数据库时出现语法错误

时间:2014-12-31 11:31:59

标签: android database sqlite android-studio syntax-error

您好我在向我的数据库添加数据时遇到此错误但我无法弄清楚原因。我认为问题出在addData() - Method中,但是到目前为止我没有尝试修复它。

“E / SQLiteLog:(1)”Aktivität“附近:语法错误”

以及:

“android.database.sqlite.SQLiteException:near”Aktivität“:语法错误(代码1):,编译时:INSERT INTO MedDatenTabelle(Schmerz,Broteinheiten,Messzeit,Wasser,Diastole,Müdigkeit,Puls,Symptom,letzteAktivität ,Gewicht,Kalorien,Bemerkungen,Systole,Temperatur,Blutzucker)VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)“

public class DatabaseHandler extends SQLiteOpenHelper {


    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_NAME= "MedDatenManager";

    private static final String TABLE_MEDDATEN = "MedDatenTabelle";


    private static final String KEY_ID = "id";
    private static final String KEY_SYST = "Systole";
    private static final String KEY_DIAST = "Diastole";
    private static final String KEY_PULS = "Puls";
    private static final String KEY_BLUTZ = "Blutzucker";
    private static final String KEY_TEMP = "Temperatur";
    private static final String KEY_GEW = "Gewicht";
    private static final String KEY_KAL = "Kalorien";
    private static final String KEY_BE ="Broteinheiten";
    private static final String KEY_WASSER = "Wasser";
    private static final String KEY_TIME = "Messzeit";
    private static final String KEY_SCHMERZ = "Schmerz";
    private static final String KEY_MUD = "Müdigkeit";
    private static final String KEY_AKTI = "letzte Aktivität";
    private static final String KEY_SYMP = "Symptom";
    private static final String KEY_BEM = "Bemerkungen";

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

    @Override
    public void onCreate(SQLiteDatabase database) {
        String CREATE_TABLE_MEDDATEN = "CREATE TABLE " + TABLE_MEDDATEN + " ("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_SYST + " TEXT,"
            + KEY_DIAST + " TEXT," + KEY_PULS + " TEXT," + KEY_BLUTZ + " TEXT,"
            + KEY_TEMP + " TEXT," + KEY_GEW + " TEXT," + KEY_KAL + " TEXT,"
            + KEY_BE + " TEXT," + KEY_WASSER + " TEXT," + KEY_TIME + " TEXT,"
            + KEY_SCHMERZ + " TEXT," + KEY_MUD + " TEXT," + KEY_AKTI + " TEXT,"
            + KEY_SYMP + " TEXT," + KEY_BEM + " TEXT" + " )";
        database.execSQL(CREATE_TABLE_MEDDATEN);
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
        database.execSQL("DROP TABLE IF EXISTS " + TABLE_MEDDATEN);

        onCreate(database);
    }

    public void addData (MEDDATEN meddaten){
        SQLiteDatabase database = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(KEY_SCHMERZ,meddaten.getSchmerz());
        values.put(KEY_BE,meddaten.getBroteinheiten());
        values.put(KEY_TIME,meddaten.getMesszeit());
        values.put(KEY_WASSER,meddaten.getWasser());
        values.put(KEY_MUD,meddaten.getMüdigkeit());
        values.put(KEY_DIAST,meddaten.getDiastole());
        values.put(KEY_PULS,meddaten.getPuls());
        values.put(KEY_SYMP,meddaten.getSymptome());
        values.put(KEY_AKTI,meddaten.getLetzteAkt());
        values.put(KEY_GEW,meddaten.getGewicht());
        values.put(KEY_KAL,meddaten.getKalorien());
        values.put(KEY_BEM,meddaten.getBemerkungen());
        values.put(KEY_TEMP,meddaten.getTemperatur());
        values.put(KEY_SYST,meddaten.getSystole());
        values.put(KEY_BLUTZ,meddaten.getBlutzucker());

        database.insert(TABLE_MEDDATEN, null, values);
        database.close();

        }




    }


}

1 个答案:

答案 0 :(得分:2)

您的错误在此处(字段名称中的空格,没有限定符):

private static final String KEY_AKTI = "letzte Aktivität"

private static final String KEY_AKTI = "letzte_Aktivität"

代替。


如果空格强制,请尝试:

private static final String KEY_AKTI = "'letzte Aktivität'"

或者

private static final String KEY_AKTI = "[letzte Aktivität]"

或者

private static final String KEY_AKTI = "`letzte Aktivität`"

参考:https://www.sqlite.org/lang_keywords.html