table [...]没有名为[...] SQLite java

时间:2016-12-13 10:54:28

标签: java android sqlite android-studio

我知道有一些关于这方面的话题,我试着按照那里给出的所有答案,但它对我的情况没有帮助。我不断收到SecondPhone列不存在的错误

  

E / SQLiteLog:(1)表Contacts没有名为SecondPhone的列   E / SQLiteDatabase:插入SecondPhone = Phone =时出错   PhotoUri = android.resource://project.contactmanager/drawable/untitled_1.png   Email =地址=姓名=测试                                                                     android.database.sqlite.SQLiteException:table Contacts没有列   名为SecondPhone(代码1):,编译时:INSERT INTO   联系人(SecondPhone,电话,PhotoUri,电子邮件,地址,姓名)价值观   (?,?,?,?,?,?)

这是我收到的错误消息的一部分

这是我的代码的一部分。我希望问题在这里,但可能在其他地方

public class DatabaseSQL extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 2;
    //setting variables for future database columns
private static final String DATABASE_NAME = "ContactManagerLite",
        TABLE_CONTACTS = "Contacts",
        KEY_NAME = "Name",
        KEY_PHONE = "Phone",
        KEY_SECONDPHONE = "SecondPhone",
        KEY_EMAIL = "Email",
        KEY_ADDRESS = "Address",
        KEY_PHOTOURI = "PhotoUri",
        KEY_ID = "ID";

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

@Override
public void onCreate(SQLiteDatabase db){                       //creates a table upon starting the application
    db.execSQL("CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT" + KEY_PHONE + " TEXT" + KEY_SECONDPHONE + " TEXT" + KEY_EMAIL + " TEXT" + KEY_ADDRESS + " TEXT" + KEY_PHOTOURI + " TEXT)");

}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){                       //if something changes the old table is being deleted and new one is being created with new data
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
    onCreate(db);
}
public void createContact(contactList contact){        //variables from getters in contactList are being set as values for database
    SQLiteDatabase db= getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_NAME, contact.getListName());
    values.put(KEY_PHONE, contact.getListPhone());
    values.put(KEY_SECONDPHONE, contact.getSecondPhone());
    values.put(KEY_EMAIL, contact.getEmail());
    values.put(KEY_ADDRESS, contact.getAddress());
    values.put(KEY_PHOTOURI, contact.getProfilePhotoUri().toString());

    db.insert(TABLE_CONTACTS, null, values);
    db.close();
}

我正在使用AndroidStudio

3 个答案:

答案 0 :(得分:1)

它的拼写错误,您没有分隔任何列名。将(,)放在每列的数据类型之后

KEY_NAME + " TEXT," + KEY_PHONE + " TEXT," + KEY_SECONDPHONE + " TEXT," +
KEY_EMAIL + " TEXT," + KEY_ADDRESS + " TEXT," + KEY_PHOTOURI + " TEXT)"

完成后,重新安装您的应用

答案 1 :(得分:0)

您在此commas

中缺少create table query
db.execSQL("CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT" + KEY_PHONE + " TEXT" + KEY_SECONDPHONE + " TEXT" + KEY_EMAIL + " TEXT" + KEY_ADDRESS + " TEXT" + KEY_PHOTOURI + " TEXT)");

这应该是

db.execSQL("CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT," + KEY_PHONE + " TEXT," + KEY_SECONDPHONE + " TEXT," + KEY_EMAIL + " TEXT," + KEY_ADDRESS + " TEXT," + KEY_PHOTOURI + " TEXT)");

(您必须重新运行查询才能进行更新,因此请在运行前删除手机上的应用数据) 希望这有帮助,让我知道

答案 2 :(得分:0)

您没有在每列之后添加逗号。试试这个

db.execSQL("CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT," + KEY_PHONE + " TEXT," + KEY_SECONDPHONE + " TEXT," + KEY_EMAIL + " TEXT," + KEY_ADDRESS + " TEXT," + KEY_PHOTOURI + " TEXT)");

清除数据或重新安装应用程序后运行项目

希望这会对你有所帮助。