SQLite列错误:表XXX没有名为YYY的列

时间:2017-11-18 00:42:21

标签: android database sqlite

我已经查看过以下内容,并且发现没有任何内容符合我的问题(据我所知):

android.database.sqlite.SQLiteException: table X has no column named Y: , while compiling: INSERT INTO

Android : Table has no column named "variable name" MySql Database error

Error compiling insert into: No column named ID

Android SQLite issue - table ... has no column named

https://sqlite.org/foreignkeys.html

我收到以下SQLite错误:

SQLiteDatabase: Error inserting zipCode=23456 address2= city=state address1=123 petName=qwerty phoneNumber=null vetName=zaw emailAddress=
                                             android.database.sqlite.SQLiteException: table vets has no column named petName (code 1): , while compiling: INSERT INTO vets (zipCode,address2,city,address1,petName,phoneNumber,vetName,emailAddress) VALUES (?,?,?,?,?,?,?,?)

我觉得它与VetTable中的外键有关,但我的研究并没有帮助我理解正在发生的事情。

VetTable定义如下:

public class VetTable {
    public static final String TABLE_VETS = "vets";
    public static final String VET_ID = "vetId";
    public static final String PET_ID = "petId";
    public static final String VET_NAME = "vetName";
    public static final String ADDRESS1 = "address1";
    public static final String ADDRESS2 = "address2";
    public static final String CITY = "city";
    public static final String STATE = "state";
    public static final String ZIP_CODE = "zipCode";
    public static final String PHONE_NUMBER = "phoneNumber";
    public static final String EMAIL_ADDRESS = "emailAddress";

public static final String SQL_CREATE =
        "CREATE TABLE " + TABLE_VETS + "(" +
                VET_ID + " INTEGER PRIMARY KEY UNIQUE, " +
                PET_ID + " INTEGER, " +
                VET_NAME + " TEXT, " +
                ADDRESS1 + " TEXT, " +
                ADDRESS2 + " TEXT, " +
                CITY + " TEXT, " +
                STATE + " TEXT, " +
                ZIP_CODE + " TEXT, " +
                PHONE_NUMBER + " TEXT, " +
                EMAIL_ADDRESS + " TEXT, " +
                "FOREIGN KEY(" + PET_ID + ") " +
                "REFERENCES pets(petId));";

    public static final String SQL_DELETE =
            "DROP TABLE" + TABLE_VETS;
}

DBHelper代码的相关代码如下:

public void addVetInfo(String name, String address1, String address2, String city, String state, String zip, String phone, String email){
    try {
        db = this.getWritableDatabase();
        values.put(VET_NAME, name);
        values.put(ADDRESS1, address1);
        values.put(ADDRESS2, address2);
        values.put(CITY, city);
        values.put(CITY, state);
        values.put(ZIP_CODE, zip);
        values.put(PHONE_NUMBER, phone);
        values.put(EMAIL_ADDRESS, email);
        db.insert(TABLE_VETS, null, values);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

可以看出petName不是VetTable中的一部分,也不是VetTable中包含的,也不是addVetInfo中参数列表的一部分。那么,SQLite的做法是什么让它认为petName应该是VetTable中的一列?

3 个答案:

答案 0 :(得分:1)

当您尝试将数据插入到不存在列名的表中,或者您尝试插入的模式已被最新的sequelize.js代码更改时,可能会发生这种情况。

可能的问题示例:

  • 重命名的列名(例如:更早的ID和现在的UUID
  • 新的架构设计或完整的表架构更改
  • 尝试将具有错误字段名称(例如:IDS:1234)的数据插入具有列名称(例如:ID:1234)的表中

答案 1 :(得分:0)

对于遇到此SQLite错误问题的其他人来说,似乎在我的情况下,由CL指出我的帖子中有重复的列名。当我纠正并重新编写代码时,它工作正常,没有错误。

因此,请确保您的列名对于insert语句是正确的。

答案 2 :(得分:-1)

看起来你的values变量已经在对象中有了这个值,请检查:

public void addVetInfo(String name, String address1, String address2, String city, String state, String zip, String phone, String email){
    try {
        db = this.getWritableDatabase();
        //INITIALIZE YOUR VARIABLE VALUES HERE
        ContentValues values = new ContentValues();

    //if(name == "Enter vet name") name = null;
    values.put(VET_NAME, name);
    //if(address1 == "Enter address 1" || address1 == " ") address1 = null;
    values.put(ADDRESS1, address1);
    //if(address2 == "Enter address 2") address2 = null;
    values.put(ADDRESS2, address2);
    //if(city == "Enter city") city = null;
    values.put(CITY, city);
    //if(state == "Enter state") state = null;
    values.put(CITY, state);
    //if(zip == "Enter zip") zip = null;
    values.put(ZIP_CODE, zip);
    //if(phone == "Enter phone number") phone = null;
    values.put(PHONE_NUMBER, phone);
    //if(email == "Enter email address") email = null;
    values.put(EMAIL_ADDRESS, email);
    db.insert(TABLE_VETS, null, values);
} catch (SQLException e) {
    e.printStackTrace();
}
}