朋友们,我想通过下面的程序在我的应用程序中添加新列是我的代码
public class DatabaseConnectionAPI extends OrmLiteSqliteOpenHelper {
public DatabaseConnectionAPI(Context context) {
super(context, DB_PATH + DB_NAME, null, 1);
this.myContext = context;
}
public void openDataBase() throws SQLException {
try {
db.close();
} catch (Exception e) {
System.out.println("no database connected to close");
}
// Open the database
String myPath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
onUpgrade(db, connectionSource, 1, 2);
System.out.println("Databse opened...." + db);
}
@Override
public synchronized void close() {
if (db != null)
db.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase arg0, ConnectionSource arg1) {
System.out.println("Create DB");
try {
TableUtils.createTable(connectionSource, Property_Master.class);
} catch (SQLException e) {
Log.e(DatabaseConnectionAPI.class.getName(), "Can't create database", e);
throw new RuntimeException(e);
} catch (java.sql.SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2,
int arg3) {
try {
ArrayList<String> allSql = new ArrayList<String>();
switch(arg2)
{
case 1:
allSql.add("ALTER TABLE " + "property_master" + " ADD COLUMN " + " p_type " + "VARCHAR");
}
for (String sql : allSql) {
db.execSQL(sql);
}
} catch (SQLException e) {
Log.e(DatabaseConnectionAPI.class.getName(), "exception during onUpgrade", e);
throw new RuntimeException(e);
}
}
}
在上面的代码上运行时,它给我错误,如下所示
android.database.sqlite.SQLiteException: duplicate column name: p_type: ALTER TABLE property_master ADD COLUMN p_type VARCHAR
任何想法我该如何解决?