-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighScoreDB.java
More file actions
139 lines (106 loc) · 4.35 KB
/
Copy pathHighScoreDB.java
File metadata and controls
139 lines (106 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.smiths;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class HighScoreDB {
private static final int DATABASE_VERSION = 1;
private static final String SCORE_TABLE_NAME = "highscore";
private static final String SCORE_TABLE_CREATE = "CREATE TABLE "
+ SCORE_TABLE_NAME
+ " (_id INTEGER PRIMARY KEY autoincrement, "
+ "name TEXT NOT NULL, score INTEGER NOT NULL)";
private static final String DATABASE_NAME = "scores.db";
// The index (key) column name for use in where clauses.
public static final String KEY_ID = "_id";
// The name and column index of each column in your database.
public static final String KEY_NAME = "name";
public static final String KEY_SCORE = "score";
public static final int NAME_COLUMN = 1;
public static final int NUMBER_COLUMN = 2;
public static final int SCORE_COLUMN = 3;
SQLiteDatabase db;
private final Context ctx;
private final HighScoreDbHelper dbHelper;
//Constructor
public HighScoreDB(Context context) {
this.ctx = context;
ctx.deleteDatabase(SCORE_TABLE_NAME);
dbHelper = new HighScoreDbHelper(context);
db = dbHelper.getWritableDatabase();
}
public void close() {
if (db != null) {
db.close();
}
}
public void createRow(String name, int score) {
ContentValues intialValue = new ContentValues();
intialValue.put("name", name);
intialValue.put("score", score);
db.insertOrThrow(SCORE_TABLE_NAME, null, intialValue);
}
public void deleteRow(long rowId) {
db.delete(SCORE_TABLE_NAME, "_id=" + rowId, null);
}
public Cursor GetAllRows() {
try {
return db.query(SCORE_TABLE_NAME, new String[] { "_id", "name", "score" }, null,
null, null, null, "score DESC");
} catch (SQLException e) {
Log.i("Error on query", e.toString());
return null;
}
}
public void updateRow(long _id, String name, String score) {
ContentValues args = new ContentValues();
args.put("name", name);
args.put("score", score);
db.update(SCORE_TABLE_NAME, args, "_id=" + _id, null);
}
//inner class
private static class HighScoreDbHelper extends SQLiteOpenHelper {
public HighScoreDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(SCORE_TABLE_CREATE);
} catch (SQLException e) {
Log.i("Error", "Error making database");
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + SCORE_TABLE_NAME);
onCreate(db);
}
}
public void insertDefaultData(){
deleteRow(0);
deleteRow(1);
deleteRow(2);
deleteRow(3);
deleteRow(4);
deleteRow(5);
deleteRow(6);
deleteRow(7);
deleteRow(8);
deleteRow(9);
db.execSQL("INSERT INTO " + SCORE_TABLE_NAME +" Values ('0','player1',25000);");
db.execSQL("INSERT INTO " + SCORE_TABLE_NAME +" Values ('1','player1',15000);");
db.execSQL("INSERT INTO " + SCORE_TABLE_NAME +" Values ('2','player1',10000);");
db.execSQL("INSERT INTO " + SCORE_TABLE_NAME +" Values ('3','player1',7000);");
db.execSQL("INSERT INTO " + SCORE_TABLE_NAME +" Values ('4','player1',5000);");
db.execSQL("INSERT INTO " + SCORE_TABLE_NAME +" Values ('5','player1',2000);");
db.execSQL("INSERT INTO " + SCORE_TABLE_NAME +" Values ('6','player1',1000);");
db.execSQL("INSERT INTO " + SCORE_TABLE_NAME +" Values ('7','player1',750);");
db.execSQL("INSERT INTO " + SCORE_TABLE_NAME +" Values ('8','player1',500);");
db.execSQL("INSERT INTO " + SCORE_TABLE_NAME +" Values ('9','player1',200);");
}
}