我在libGdx中使用数据库时遇到问题,首先我要创建这个类:
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataBaseHelper extends SQLiteOpenHelper {
public DataBaseHelper(Context context, String name,CursorFactory factory, int version)
{
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase _db)
{
_db.execSQL(DataBaseClass.DATABASE_RECORD);
}
@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion)
{
Log.w("TaskDBAdapter", "Upgrading from version " +_oldVersion + " to " +_newVersion + ", which will destroy all old data");
onCreate(_db);
}
}
和这堂课:
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Collections;
public class DataBaseClass {
static final String DATABASE_NAME = "my.database";
static final int DATABASE_VERSION = 1;
public static final int NAME_COLUMN = 1;
static final String DATABASE_RECORD = "create table "+"RECORD"+
"( " + "SCORE text, GAMES_PLAYED text); ";
public SQLiteDatabase db;
private final Context context;
private DataBaseHelper dbHelper;
public DataBaseClass(Context _context) {
context = _context;
dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public DataBaseClass open() throws SQLException {
db = dbHelper.getWritableDatabase();
return this;
}
public void close() {
db.close();
}
public SQLiteDatabase getDatabaseInstance() {
return db;
}
public void updateGamesPlayed(String game) {
ContentValues updatedValues = new ContentValues();
updatedValues.put("GAMES_PLAYED", game);
String where="GAMES_PLAYED = ?";
db.update("RECORD",updatedValues, where, new String[]{game});
}
public void updateBestScore(String score) {
ContentValues updatedValues = new ContentValues();
updatedValues.put("SCORE", score);
String where="SCORE = ?";
db.update("RECORD",updatedValues, where, new String[]{score});
}
}
但是当我在实现界面屏幕的我的主要类中调用DataBaseClass时,我遇到了问题:
DBClass = new DataBaseClass(CONTEXT);
这堂课的背景是什么?我尝试使用:"这"," getContext()",getActivity"," this.getActivity"等...但没有任何作用。
有人可以帮助我吗?
答案 0 :(得分:0)
这里的上下文指的是您当前的类上下文,您只需传递this
或principal.this