Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将分数写入SQLite数据库?_Java_Android_Android Sqlite - Fatal编程技术网

Java 如何将分数写入SQLite数据库?

Java 如何将分数写入SQLite数据库?,java,android,android-sqlite,Java,Android,Android Sqlite,我是sqlite的新手。我要做的是…我有一个游戏,游戏结束后,它将时间结果存储在一个名为elapsedsons的双变量中。我想把结果放在db中,然后在我的专用活动中为用户显示前10名分数。我有一个HighScoreDb类,在这个类中我创建了一个db(我使用了一些我在网上找到的代码,但我认为它会达到我的目的)。这是密码 public class HighScoreDb { private static class HighScoreDbHelper extends SQLiteOpenHe

我是sqlite的新手。我要做的是…我有一个游戏,游戏结束后,它将时间结果存储在一个名为elapsedsons的双变量中。我想把结果放在db中,然后在我的专用活动中为用户显示前10名分数。我有一个HighScoreDb类,在这个类中我创建了一个db(我使用了一些我在网上找到的代码,但我认为它会达到我的目的)。这是密码

public class HighScoreDb {
    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);
    }

    }

    private static final int DATABASE_VERSION = 2;
    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 DOUBLE  NOT NULL)";
    private static final String DATABASE_NAME = "highscores.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;

    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("number", score);
    db.update(SCORE_TABLE_NAME, args, "_id=" + _id, null);
    }

}
从这个表中,我不需要列名,因为我只有10个分数。在我的游戏结束后,如何将该结果插入该数据库?我想我知道如何阅读,我会这样做:

HighScoreDb db = new HighScoreDb(this);
Cursor myCursor = db.GetAllRows();
myCursor.moveToPosition(0);
    String Row1Value2 = myCursor.getString(2);
myCursor.close();
db.close();
HighScoreDb db = new HighScoreDb(this);
我在游戏课上这样称呼它:

HighScoreDb db = new HighScoreDb(this);
Cursor myCursor = db.GetAllRows();
myCursor.moveToPosition(0);
    String Row1Value2 = myCursor.getString(2);
myCursor.close();
db.close();
HighScoreDb db = new HighScoreDb(this);

我不确定我是否理解你的问题。。。要在数据库中放入一些数据,只需正确使用下面编写的函数(我假设您的类工作正常),例如:

HighScoreDbHelper myDbHelper = new HighScoreDbHelper(yourContext);
myDbHelper.createRow(yourName, yourScore);

本教程是sqlite概念的良好开端:

打开数据库的方式很好

与其使用游标获取关于行的特定信息,不如在表中创建一个表示行的实体。使用光标中的信息创建这些对象的列表

public UserScore {
    String name;
    Integer score; // don't know why you would use double here.

    // getters + setters or make it immutable
}
我要提供的是HighScoreDb中添加新分数的一些方法。例如签名

public void addScore(UserScore userScore);
不要只考虑添加10个分数,而是将每个分数添加到数据库中。只要你想要前10名的分数。只需查询最后10个分数。否则,您将编写非常复杂的程序,这很容易通过查询完成。为此,请查看top-N查询

在helper中写入DB的示例如下(没有测试SQL,但应该是这种形式)


您是否尝试过仅使用
createRow(字符串名称、整数分数)
?如果是这样,为什么它不起作用?我注意到的一件事是,您在构造函数中打开数据库,并将其保持打开状态,直到调用
close()
。您最好只打开数据库,然后再读取或写入记录我尝试过的所有内容都在代码中。:)我们可以慢慢开始吗?我在游戏结束后设置了两个变量,insertName是一个字符串,elapsedSeconds是一个Int。如何将这些值插入我的数据库?那么你可以调用
createRow(insertName,elapsedSeconds)
,它应该可以工作。但是,您必须更改语句
SCORE\u TABLE\u CREATE
,以创建类型为
INTEGER
的列,而不是
DOUBLE
。或者您必须将
createRow
参数更改为
String,double
,或者
String,float
,因为我不知道SQLite是否可以处理double。但我建议使用整数,并将测量值更改为毫秒。这是时间测量的实际标准。好的,我将它改为int。还有一件事,调用createRow的最佳位置在哪里?我可以在游戏结束后,在我的游戏活动中立即调用吗?是的,调用不会造成任何延迟,只要你想保存值,就可以安全地在UI线程中调用它。我使用double,因为我的结果大约是34,54秒或12,68秒。如果你想使用2位小数,可以查看BigDecimal。这是我最不关心的问题。我需要弄清楚如何将结果写入,然后如何从中读取。就像我说的,SQLite不是我的强项。