0

I have a DatabaseHandler class and within my table i have an id, name, and phonenumber. I want to add another column - "age", yet every time I try to add to that new column I get an error saying the column doesn't exist. How would I add another column to this?

Here's some code:

public class DatabaseHandler extends SQLiteOpenHelper {


// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "contactsManager";

// Contacts table name
private static final String TABLE_CONTACTS = "contacts";

// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_PH_NO = "phone_number";

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

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_PH_NO + " TEXT" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

/**
 * All CRUD(Create, Read, Update, Delete) Operations
 */

// Adding new contact
void addContact(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, contact.getName()); // Contact Name
    values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone

    // Inserting Row
    db.insert(TABLE_CONTACTS, null, values);
    db.close(); // Closing database connection
}

// Getting All Contacts
public List<Contact> getAllContacts() {
    List<Contact> contactList = new ArrayList<Contact>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Contact contact = new Contact();
            contact.setID(Integer.parseInt(cursor.getString(0)));
            contact.setName(cursor.getString(1));
            contact.setPhoneNumber(cursor.getString(2));
            // Adding contact to list
            contactList.add(contact);
        } while (cursor.moveToNext());
    }

    // return contact list
    return contactList;
}
4

4 回答 4

1

添加新列很有用:

db.execSQL(ALTER TABLE_CONTACTS ADD AGE INTEGER);

或者你可以做另一件事,即

private static final int DATABASE_VERSION = 2;// change the database version    
private static final String KEY_AGE = "age";

// Creating Tables
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_PH_NO + " TEXT"
            + KEY_AGE + " TEXT" +")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

这可能会对您有所帮助。

于 2013-08-27T05:51:44.557 回答
0

当您尝试添加 KEY_AGE 时,您需要在文本后放置一个逗号!喜欢

" TEXT,"

所以你的代码需要看起来像KEY_NAME + " TEXT," + KEY_AGE + " TEXT," + KEY_PH_NO + " TEXT" 清除你的应用数据,它必须工作!

于 2015-01-25T23:02:21.657 回答
0

您应该使用此处所述的 ALTER TABLE 命令:http ://www.sqlite.org/lang_altertable.html

于 2012-05-17T19:28:51.440 回答
0

在需要添加列的位置使用此行:

db.execSQL(ALTER TABLE_CONTACTS ADD AGE INTEGER);
于 2013-08-27T05:23:58.343 回答