早上好,我最初的数据库是姓名和电子邮件。但是,我现在想要添加更多列,姓氏性别和出生日期是具体的。我开始使用KEY_LNAME =“lname”并添加所需的值也将数据库版本从2增加到3但这不起作用。为什么数据库不接受更改?
public class DatabaseAdapter {
static final String KEY_ROWID = "_id";
static final String KEY_NAME = "name";
static final String KEY_EMAIL = "email";
static final String KEY_LNAME = "lname";
static final boolean KEY_GENDER = false;
static final String DOB = "DD-MM-YYYY";
static final String TAG = "DBAdapter";
static final String DATABASE_NAME = "MyDB";
static final String DATABASE_TABLE = "contacts";
static final int DATABASE_VERSION = 3;
static final String DATABASE_CREATE =
"create table contacts (_id integer primary key autoincrement, "
+ "name text not null, email text not null);";
final Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;
public DatabaseAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
}
//---opens the database---
public DatabaseAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---insert a contact into the database---
public long insertContact(String name, String email, String lname)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_EMAIL, email);
initialValues.put(KEY_LNAME, lname);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//---deletes a particular contact---
public boolean deleteContact(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
//---retrieves all the contacts---
public Cursor getAllContacts()
{
return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
KEY_EMAIL, KEY_LNAME}, null, null, null, null, null);
}
//---retrieves a particular contact---
public Cursor getContact(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_NAME, KEY_EMAIL, KEY_LNAME}, KEY_ROWID + "=" + rowId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//---updates a contact---
public boolean updateContact(long rowId, String name, String email, String lname)
{
ContentValues args = new ContentValues();
args.put(KEY_NAME, name);
args.put(KEY_EMAIL, email);
args.put(KEY_LNAME, lname);
return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}
答案 0 :(得分:0)
首先:
使用新值更改DB_CREATE
字符串。
然后:
在onUpgrade
方法中,如果oldVersion为1或2(在您的特定情况下),则执行新查询。如果您只想在现有数据库中添加一些列,我建议您添加如下代码:
private final String ALTER_TABLE_ONE =
"ALTER TABLE " + TABLE_NAME +
" ADD " + YOUR_NEW_COLUMN + " TEXT;";
private final String ALTER_TABLE_TWO =
"ALTER TABLE " + TABLE_NAME +
" ADD " + YOUR_SECOND_NEW_COLUMS + " TEXT;";
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion == 1) {
...UPGRADE MORE COLUMNS OR SIMPLY DO WHAT YOU NEED...
} else if (oldVersion == 2) {
db.execSQL(ALTER_TABLE_ONE);
db.execSQL(ALTER_TABLE_TWO);
} else {
// fallback if coming from unexpected db version
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
}
每次添加新列时都必须致电execSQL
。