在SQLite Android中创建新列

时间:2012-05-17 18:57:16

标签: android database sqlite

我有一个DatabaseHandler类,在我的表中我有一个id,name和phonenumber。我想添加另一个列 - “age”,但每次我尝试添加到该新列时,我都会收到一条错误消息,指出该列不存在。 我该如何为此添加另一列?

以下是一些代码:

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 个答案:

答案 0 :(得分: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);
}

这可能会对你有帮助。

答案 1 :(得分:0)

您应该使用ALTER TABLE命令,如下所述:http://www.sqlite.org/lang_altertable.html

答案 2 :(得分:0)

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

db.execSQL(ALTER TABLE_CONTACTS ADD AGE INTEGER);

答案 3 :(得分:0)

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

" TEXT,"

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