2 zxd 72 zxd_72 于 2013.04.22 17:52 提问

局部变量初始化的问题
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 = "contextsManager";
// Locations table name
private static final String TABLE_LOCATIONLABLES = "locationLables";
// LOCATIONLABLES Table Columns names
private static final String KEY_LOCID = "loc_id";
private static final String KEY_LOCNAME = "loc_name";
public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String TABLE_LOCATIONLABLES = "CREATE TABLE " + TABLE_LOCATIONLABLES + "("
            + KEY_LOCID + " INTEGER PRIMARY KEY," + KEY_LOCNAME + " TEXT,"
            +  ")";
    db.execSQL(TABLE_LOCATIONLABLES);   
}

提示说的是局部变量TABLE_LOCATIONLABLES可能没有被初始化?可是我初始化了的呀,这个错误如何发生的呢?

2个回答

baimy1985
baimy1985   2013.04.22 21:24
已采纳
String TABLE_LOCATIONLABLES = "CREATE TABLE " + TABLE_LOCATIONLABLES + "("

等号两各有一个,而且String TABLE_LOCATIONLABLES这个还是新定义的,计算机迷糊了,它不认为你上面已经定义了

zxd_72
zxd_72 谢谢你的提醒,我没注意到,在 oncreate(db) 里重命名 字符串 TABLE_LOCATIONLABLES 就可以。
4 年多之前 回复
zxd_72
zxd_72   2013.04.23 10:30

在 oncreate(db) 里重命名 字符串 TABLE_LOCATIONLABLES

@Override
    public void onCreate(SQLiteDatabase db) {
        String mQuery = "CREATE TABLE " + TABLE_LOCATIONLABLES + "("
                + KEY_LOCID + " INTEGER PRIMARY KEY," + KEY_LOCNAME + " TEXT,"
                +  ")";
        db.execSQL(mQuery);   
    }
Csdn user default icon
上传中...
上传图片
插入图片