2017-11-18 10 views
-1

我看着下面,在其他列,並發現匹配我的問題(據我可以告訴)什麼:SQLite的列錯誤:表XXX沒有名爲YYY

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 ,也不是addVetInfo中參數列表的一部分。那麼,SQLite做的是什麼讓它認爲petName應該是VetTable中的一個列?

+0

有兩個'put(CITY')調用,無論如何,顯示的代碼並不是實際得到執行的代碼 –

+0

CL,你是什麼意思?我沒有發佈什麼代碼? – Jeff

+0

CL,感謝您發現兩個「CITY」電話! – Jeff

回答

0

對於其他人誰是有這個SQLite的錯誤問題,看來,它在我的情況下有重複的是由於列的名稱由CL指出我的帖子。當我糾正並重新編碼時,它工作正常,沒有錯誤。

因此,請確保您的列名稱對於您的插入語句是正確的。

-1

看起來像你的價值觀變量已經有對象內部的價值,看看這個:

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(); 
} 
} 
+0

我發現錯誤是由於CL的評論指出我有重複的「CITY」引用導致的。我無法想象爲什麼SQLite會說它與「petName」有什麼關係。 – Jeff

相關問題