如何在SQLite Android中添加数据

时间:2014-02-16 15:03:56

标签: java android sqlite

我想在我的SQLite数据库中添加一个整数值。问题是int值在另一个类中,如何将一个值添加到我的数据库中,它表示总赢?我只想在里面添加一个数据?

public class Database extends SQLiteOpenHelper{

public Database(Context applicationContext) {
    super(applicationContext, "HighScores.db",null, 1);
}

@Override //creating database
public void onCreate(SQLiteDatabase database) {

    String query = "CREATE TABLE HighScores (totalWins INTEGER)";
    database.execSQL(query);
}

@Override //if the table exists then drop it
public void onUpgrade(SQLiteDatabase database, int arg1, int arg2) {
    String query = "DROP TABLE IF EXISTS HighScores";
    database.execSQL(query);
    onCreate(database);

}

//for inserting values
public void insertHighScores(HashMap<String, String> queryValues) {
    SQLiteDatabase database = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put("totalWins",queryValues.get("totalWins"));

    database.insert("HighScores", null, values);
    database.close();
}

//for updating values
public int updateHighScores(HashMap<String, String> queryValues){
    SQLiteDatabase database = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put("totalWins",queryValues.get("totalWins"));

    return database.update("HighScores", values, "totalWins" + " =?", new String[] 
            {queryValues.get("totalWins")});
}
//return all highscores
public ArrayList<HashMap<String,String>>getAllHighScores() { 
    ArrayList<HashMap<String, String>> highScoreArrayList = new ArrayList<HashMap<String, String>>();

    String selectQuery = "SELECT * FROM HighScores";

    SQLiteDatabase database = this.getWritableDatabase();

    Cursor MyCursor = database.rawQuery(selectQuery, null);

    if (MyCursor.moveToFirst()) {
        do{
            HashMap<String, String>highScoresMap = new HashMap<String, String>();
            highScoresMap.put("totalWins",MyCursor.getString(0));

            highScoreArrayList.add(highScoresMap);

        }while(MyCursor.moveToNext());
    }
    return highScoreArrayList;
}

   }

1 个答案:

答案 0 :(得分:0)

怎么样?

public class SomeOtherClass{


Database  myDatabase = new Database (this);

public void editHighScores(int myvalue){

    HashMap<String, String> queryValues = new HashMap<String, String>();
    queryValues.put("totalWins", myvalue);

    myDatabase.insertHighScores(queryValues);
}
}