-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseHelper.java
More file actions
50 lines (43 loc) · 1.58 KB
/
DatabaseHelper.java
File metadata and controls
50 lines (43 loc) · 1.58 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
package com.example.jason.a4x4;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by JASON on 1/9/2019.
*/
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "score.db";
public DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, 1);
//onUpgrade(this.getWritableDatabase(), 1, 1); //reset db
}
@Override
public void onCreate(SQLiteDatabase db){
//Create table- scoreTable
//Pkey - ID , int
//column1 - score , int
db.execSQL("CREATE TABLE scoreTable (ID INTEGER PRIMARY KEY AUTOINCREMENT, SCORE LONG)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("DROP TABLE IF EXISTS scoreTable");
onCreate(db);
}
public boolean insert(long score){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("SCORE", score);
long result = db.insert("scoreTable", null, contentValues);
if(result != -1){
return true;
}
return false;
}
public Cursor getTopScores(){ //return top 10 scores
SQLiteDatabase db = this.getWritableDatabase();
Cursor score = db.rawQuery("SELECT SCORE FROM scoreTable LIMIT 10", null);
return score;
}
}