好吧,这是我的 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);
}
}
在我的主要活动中,这就是我获取数据的方法。
if (cursor!= null) {
if (cursor.moveToFirst()) {
do {
System.out.println(cursor.getString(cursor.getColumnIndexOrThrow("playerName")));
} while (cursor.moveToNext());
}
}
为什么没有创建列?我得到了这个例外。
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mkyong.android/com.mkyong.android.Stats}: java.lang.IllegalArgumentException: column 'playerName' does not exist