· 6 years ago · Mar 13, 2019, 11:20 PM
1public class DatabaseHelper extends SQLiteOpenHelper {
2
3 static final String dbName="demoDB";
4
5 public DatabaseHelper(Context context) {
6 super(context, dbName, null, 1);
7 }
8
9 @Override
10 public void onCreate(SQLiteDatabase db) {
11 String sql = "CREATE TABLE IF NOT EXISTS players (" +
12 "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
13 "playerName TEXT)";
14 db.execSQL(sql);
15
16 ContentValues values = new ContentValues();
17
18 values.put("playerName", "John");
19 db.insert("players", "playerName", values);
20
21 values.put("playerName", "George");
22 db.insert("players", "playerName", values);
23
24 values.put("firstName", "Marie");
25 db.insert("players", "playerName", values);
26 System.out.println("Hello");
27
28 }
29
30 @Override
31 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
32 db.execSQL("DROP TABLE IF EXISTS players");
33 onCreate(db);
34 }
35
36}
37
38if (cursor!= null) {
39 if (cursor.moveToFirst()) {
40 do {
41 System.out.println(cursor.getString(cursor.getColumnIndexOrThrow("playerName")));
42 } while (cursor.moveToNext());
43 }
44 }
45
46java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mkyong.android/com.mkyong.android.Stats}: java.lang.IllegalArgumentException: column 'playerName' does not exist
47
48nullColumnHack optional; may be null. SQL doesn't allow inserting a completely empty row without naming at least one column name. If your provided values is empty, no column names are known and an empty row can't be inserted. If not set to null, the nullColumnHack parameter provides the name of nullable column name to explicitly insert a NULL into in the case where your values is empty.