简体   繁体   English

添加SQLite条目不在onCreate方法中

[英]Adding SQLite entry not in onCreate method

The fact is that I am using SQLite to create a database which stores the puntuation of a game. 事实是,我正在使用SQLite创建一个数据库来存储游戏的点数。 I need a number, the puntuation, which will update when it must to. 我需要一个数字,标点符号,必须在需要时更新。 I created a class extending SQLiteOpenHelper, named MonedasSQLite. 我创建了一个扩展SQLiteOpenHelper的类,名为MonedasSQLite。

But the database is empty yet. 但是该数据库是空的。 To introduce information, in the onCreate method of the Game.java class, I write this: 为了介绍信息,在Game.java类的onCreate方法中,我这样写:

private MonedasSQLite base_monedas = new MonedasSQLite(this, "DBMonedas", null, 1);
base_monedas.insertarReg(0,0);
base_monedas.cerrar();

Where insertarReg is: 其中insertarReg是:

public void insertarReg(Integer cod, Integer mon) {
    ContentValues valores = new ContentValues();
    valores.put("codigo", cod);
    valores.put("money", mon);
    this.getWritableDatabase().insert("monedas", null, valores);
}

And cerrar() is just for closing: cerrar()仅用于关闭:

public void cerrar() {
    this.close();
}

Doing so, everytime the user goes into the layout with the Game.java assigned, a new row of values (0,0) is being created. 这样做时,每次用户进入分配了Game.java的布局时,都会创建新的一行值(0,0)。 This way, when the user has entered 5 times there, the content of the database is (imagine the current puntuation is 100): 这样,当用户在那里输入了5次时,数据库的内容为(假设当前标点为100):

(0,100) (0,100) (0,100) (0,100) (0,0) --> As I am updating the (1st column=0 value) when the user does something OK, all the rows are updated. (0,100)(0,100)(0,100)(0,100)(0,0)->当用户正在执行某些操作时,我正在更新(第一列= 0值)时,所有行都将更新。 The last one not yet, because it has just been created. 最后一个还没有,因为它刚刚被创建。

What I am looking for is how can I create an entry of (0,0) out of the onCreate(), so that my database only has a row. 我要寻找的是如何从onCreate()中创建(0,0)的条目,以便我的数据库只有一行。

Hope somebody could help me! 希望有人能帮助我! Hope too that I was clear in my explanation, if not please ask and I will write more code or information needed. 也希望我的解释是明确的,否则请提出要求,我将编写更多的代码或信息。

Thanks in advance!!! 提前致谢!!!

I would advise you to take a look at this tutorial http://www.vogella.com/articles/AndroidSQLite/article.html 我建议您看一下本教程http://www.vogella.com/articles/AndroidSQLite/article.html

and

http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/ http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

Its very simple and will help you create a good solid and well built SQL TABLE. 它非常简单,将帮助您创建一个良好的,坚固的SQL TABLE。

hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM