Android:table没有名为'columnname'的列

时间:2015-11-30 10:36:10

标签: android sqlite insert

我有错误:android.database.sqlite.SQLiteException:table aerzte没有名为_vorname的列

但是我的表有_vorname列。我找不到错误,请帮帮我。 这是代码:

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "data.db"; 
private static final String TABLE_Aerzte = "aerzte";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_Name = "_name";
private static final String COLUMN_Passwort = "_passwort";
private static final String COLUMN_Vorname = "_vorname";

//second table
private static final String TABLE_Patienten = "patienten";
private static final String COLUMN_patID = "_id";
private static final String COLUMN_patName = "_name";
private static final String COLUMN_patVorname = "_vorname";
private static final String COLUMN_patSVNR = "_svnr";



//third table
private static final String TABLE_Werte = "werte";
private static final String COLUMN_wertID = "_id";
private static final String COLUMN_wertpatName = "_name"; //foreign key -> patient (1:n) beziehung
private static final String COLUMN_wertDatzeit = "_datzeit";
private static final String COLUMN_wertTemperatur = "_temperatur";
private static final String COLUMN_wertMessort = "_messort";

 public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
     }


   //create tables:
       @Override
public void onCreate(SQLiteDatabase db) {
    //tabelle arzt
    String query = "CREATE TABLE  "+TABLE_Aerzte+"("+COLUMN_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+COLUMN_Passwort+" TEXT, "+COLUMN_Name+ " TEXT, " +COLUMN_Vorname+ "TEXT " + ");";
    db.execSQL(query);

    //tabelle patient
    String query2 = "CREATE TABLE  "+TABLE_Patienten+"("+COLUMN_patID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+COLUMN_patName+" TEXT,"+COLUMN_patVorname+" TEXT, "+COLUMN_patSVNR+" LONG" + ");";
    db.execSQL(query2);

    //tabelle wert
    String query3 = "CREATE TABLE  "+TABLE_Werte+"("+COLUMN_wertID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+COLUMN_wertMessort+" TEXT, "+COLUMN_wertTemperatur+ " INTEGER, "+COLUMN_wertDatzeit+" DATETIME, "+COLUMN_wertpatName+" TEXT, FOREIGN KEY" + ");";
    db.execSQL(query3);

}



//insert
 public void registerPhysician(Fieberwerte fieberwerte) { // public void registerPhysician(etname, etpass, etvorname)

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    //values.put(COLUMN_ID, 1);
    values.put(COLUMN_Passwort, fieberwerte.getPasswort());
    values.put(COLUMN_Name, fieberwerte.getName());
    values.put(COLUMN_Vorname, fieberwerte.getVorname());
    //db.insert(TABLE_Aerzte, null, values);
    db.insertWithOnConflict(TABLE_Aerzte, null, values, SQLiteDatabase.CONFLICT_REPLACE);
    //db.close();
}

我希望你们能帮助我解决这个错误。我多次检查代码,但我没有发现任何错误: - (

3 个答案:

答案 0 :(得分:4)

你在

错过了一个空格
" TEXT, " +COLUMN_Vorname+ "TEXT " + ");";

在列名称和类型之间添加空格

" TEXT, " +COLUMN_Vorname+ " TEXT " + ");";

答案 1 :(得分:0)

错误就在这里

//tabelle arzt
String query = "CREATE TABLE  "+TABLE_Aerzte+"("+COLUMN_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+COLUMN_Passwort+" TEXT, "+COLUMN_Name+ " TEXT, " +COLUMN_Vorname+ "TEXT " + ");";

你在COLUMN_Vorname之后错过了一个空格,在TEXT之前放了一个空格。

COLUMN_Vorname+ " TEXT " + "

执行此操作后,卸载应用程序并尝试

答案 2 :(得分:0)

您必须使用外键创建如下:

 String query3 = "CREATE TABLE  "+TABLE_Werte+"("+COLUMN_wertID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+COLUMN_wertMessort+" TEXT, "+COLUMN_wertTemperatur+ " INTEGER, "+COLUMN_wertDatzeit+" DATETIME, "+COLUMN_wertpatName+" TEXT," + " FOREIGN KEY ("+COLUMN_wertTemperatur+") REFERENCES "+TABLE_Patienten+"("+COLUMN_patID+"));";

这里是“COLUMN_wertTemperatur”这是创建表中的外键,它引用具有列名“COLUMN_patID”的“TABLE_Patienten”表的主键。