2 znl 12 znl_12 于 2013.10.09 16:32 提问

column没有创建成功出现异常

下面是 DatabaseHelper 类:

public class DatabaseHelper extends SQLiteOpenHelper {

    static final String dbName="demoDB";

    public DatabaseHelper(Context context) {
            super(context, dbName, null, 1);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
            String sql = "CREATE TABLE IF NOT EXISTS players (" +
                                            "id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                                            "playerName TEXT)";
            db.execSQL(sql);

            ContentValues values = new ContentValues();

            values.put("playerName", "John");
            db.insert("players", "playerName", values);

            values.put("playerName", "George");
            db.insert("players", "playerName", values);

            values.put("firstName", "Marie");
            db.insert("players", "playerName", values);
            System.out.println("Hello");

    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS players");
            onCreate(db);
    }
}

在 main activity 中我获取数据的方法:

if (cursor!= null) {
            if (cursor.moveToFirst()) {
                do {
                     System.out.println(cursor.getString(cursor.getColumnIndexOrThrow("playerName")));            
                } while (cursor.moveToNext());
            }
        }

为什么column没有创建?异常如下:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mkyong.android/com.mkyong.android.Stats}: java.lang.IllegalArgumentException: column 'playerName' does not exist
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!