我在升级SQLite时遇到问题。我正在使用SQLiteOpenHelper,我只添加了一列,更改了数据库版本,但问题是,新列未显示(未制作)。据我所知,OnCreate和OnUpgrade被正确调用,但我不知道为什么没有新表。我只在表格“smeri”中添加了最后一栏“在线”
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 16;
// Database Name
private static final String DATABASE_NAME = "RasicaDB";
// Contacts table name
private static final String TABLE_NAME_mainpoints = "mainpoints";
private static final String TABLE_NAME_smeri = "smeri";
private static final String TABLE_NAME_povezave = "povezave";
// Contacts Table Columns names
private static final String KEY1_ID = "ID";
private static final String KEY1_X = "X";
private static final String KEY1_Y = "Y";
private static final String KEY2_ID = "ID";
private static final String KEY2_ime = "Ime";
private static final String KEY2_tezavnost = "Tezavnost";
private static final String KEY2_opis = "Opis";
private static final String KEY2_avtor = "Avtor";
private static final String KEY2_datum = "Datum";
private static final String KEY2_online = "Online";
private static final String KEY3_IDsmeri = "IDsmeri";
private static final String KEY3_IDtocke = "IDtocke";
private static final String KEY3_vrstaTocke = "vrstaTocke";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.e("log_tag", "ON CREATE!!!!!!!!");
String CREATE_mainpoints_TABLE = "CREATE TABLE " + TABLE_NAME_mainpoints + "("
+ KEY1_ID + " INTEGER PRIMARY KEY,"
+ KEY1_X + " INTEGER,"
+ KEY1_Y + " INTEGER" + ")";
db.execSQL(CREATE_mainpoints_TABLE);
String CREATE_smeri_TABLE = "CREATE TABLE " + TABLE_NAME_smeri + "("
+ KEY2_ID + " INTEGER PRIMARY KEY,"
+ KEY2_ime + " TEXT,"
+ KEY2_tezavnost + " TEXT,"
+ KEY2_opis + " TEXT,"
+ KEY2_avtor + " TEXT,"
+ KEY2_datum + " TEXT,"
+ KEY2_online + " INTEGER" + ")";
Log.e("log_tag", ""+CREATE_smeri_TABLE);
db.execSQL(CREATE_smeri_TABLE);
String CREATE_povezave_TABLE = "CREATE TABLE " + TABLE_NAME_povezave + "("
+ KEY3_IDsmeri + " INTEGER,"
+ KEY3_IDtocke + " INTEGER,"
+ KEY3_vrstaTocke + " INTEGER " + ")";
db.execSQL(CREATE_povezave_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
Log.e("log_tag", "ON UPGRADE!!!!!!!!");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_mainpoints);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_smeri);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_povezave);
// Create tables again
onCreate(db);
}
答案 0 :(得分:0)
确保将表名放在引号
中 db.execSQL("DROP TABLE IF EXISTS '" + TABLE_NAME_mainpoints +"'");
答案 1 :(得分:0)
您可以尝试添加列
db.execSQL("alter table smeri add column Online int");
答案 2 :(得分:0)
使用此代码:
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 16;
// Database Name
private static final String DATABASE_NAME = "RasicaDB";
// Contacts table name
private static final String TABLE_NAME_mainpoints = "mainpoints";
private static final String TABLE_NAME_smeri = "smeri";
private static final String TABLE_NAME_povezave = "povezave";
// Contacts Table Columns names
private static final String KEY1_ID = "ID";
private static final String KEY1_X = "X";
private static final String KEY1_Y = "Y";
private static final String KEY2_ID = "ID";
private static final String KEY2_ime = "Ime";
private static final String KEY2_tezavnost = "Tezavnost";
private static final String KEY2_opis = "Opis";
private static final String KEY2_avtor = "Avtor";
private static final String KEY2_datum = "Datum";
private static final String KEY2_online = "Online";
private static final String KEY3_IDsmeri = "IDsmeri";
private static final String KEY3_IDtocke = "IDtocke";
private static final String KEY3_vrstaTocke = "vrstaTocke";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.e("log_tag", "ON CREATE!!!!!!!!");
String CREATE_mainpoints_TABLE = "CREATE TABLE " + TABLE_NAME_mainpoints + "("
+ KEY1_ID + " INTEGER PRIMARY KEY,"
+ KEY1_X + " INTEGER,"
+ KEY1_Y + " INTEGER" + ")";
db.execSQL(CREATE_mainpoints_TABLE);
String CREATE_smeri_TABLE = "CREATE TABLE " + TABLE_NAME_smeri + "("
+ KEY2_ID + " INTEGER PRIMARY KEY,"
+ KEY2_ime + " TEXT,"
+ KEY2_tezavnost + " TEXT,"
+ KEY2_opis + " TEXT,"
+ KEY2_avtor + " TEXT,"
+ KEY2_datum + " TEXT,"
+ KEY2_online + " INTEGER" + ")";
Log.e("log_tag", ""+CREATE_smeri_TABLE);
db.execSQL(CREATE_smeri_TABLE);
String CREATE_povezave_TABLE = "CREATE TABLE " + TABLE_NAME_povezave + "("
+ KEY3_IDsmeri + " INTEGER,"
+ KEY3_IDtocke + " INTEGER,"
+ KEY3_vrstaTocke + " INTEGER " + ")";
db.execSQL(CREATE_povezave_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
try{
if(oldVersion<16){
db.execSQL("ALTER TABLE " + TABLE_NAME_smeri +" ADD column "+KEY2_online+ " INTEGER ");
}
}catch(SQLException e){
e.printStackTrace();
}
}
}