Mounir Elfassi Mounir Elfassi - 23 days ago 9
Android Question

Loading tables into Sqlite database

I need to know how to insert by default (when you load the app for the first time you find this data) data into my sqlite db, by example

gameidno:1 gamedate:30/11/2014 gameteam:Spurs

gameopponent: LAL

gameidno:2

gamedate:05/12/2014

gameteam:Spurs

gameopponent: Mavs

gameidno:3

gamedate:09/12/2014

gameteam:Spurs

gameopponent: Memphis

Here is my DBhelper:

public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE if not exists gamestable(id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "gameidno"
+ " INTEGER,"
+ "gamedate"
+ " TEXT,"
+ "gameteam"
+ " TEXT,"
+ "gameopponent" + " TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + gamestable);
onCreate(db);
}

public void addGame(GameModel gameitem) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();


contentValues.put("gameidno", gameitem.gameidno);
contentValues.put("gamedate", gameitem.gamedate );
contentValues.put("gameteam", gameitem.gameteam);
contentValues.put("gameopponent", gameitem.gameopponent);
db.insert("gamestable", null, contentValues);

db.close();
}


Here is my class:

public class AddGames extends Activity implements OnClickListener {

private Button btn_addrecord;
private EditText txtpname;
DatabaseHelper db;
GameModel pm;
private Spinner gamesSpinner;
private Spinner timeSpinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_reminder);

txtpname = (EditText) findViewById(R.id.myEditText);

gamesSpinner = (Spinner) findViewById(R.id.games_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.reminders_list, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
gamesSpinner.setAdapter(adapter);


timeSpinner = (Spinner) findViewById(R.id.time_spinner);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this,
R.array.time_list, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
timeSpinner.setAdapter(adapter1);


btn_addrecord = (Button) findViewById(R.id.button1);
btn_addrecord.setOnClickListener(this);

}

@Override
public void onClick(View v) {
DatabaseHelper db;
ProductModel pm;

// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:

if (txtpname.getText().toString().equals("")
|| gamesSpinner.getSelectedItem().toString().equals("")) {
Toast.makeText(AddGames.this, "Please add values..",
Toast.LENGTH_LONG).show();
} else {

db = new DatabaseHelper(getApplicationContext());
db.getWritableDatabase();
pm = new GamesModel();
pm.idno = (txtpname.getText().toString());
pm.gamename = gameSpinner.getSelectedItem().toString();
pm.reminderway = timeSpinner.getSelectedItem().toString();

Log.i("gameidno,gamename,productprice", "" + pm.idno + ""
+ pm.gamename + "" + pm.reminderway);
db.addGame(pm);
Toast.makeText(Addgame.this, "Record Added successfully.",
Toast.LENGTH_LONG).show();
finish();
}
break;

default:
break;
}

}


GameModel Class:

public class GameModel {
public String gameidno="", gamedate="", gameteam="", gameopponent="";

public String getgamedate() {
return gamedate;
}

public void setgamedate(String gamedate) {
this.gamedate = gamedate;
}

public String getgameteam() {
return gameteam;
}

public void setgameteam(String gameteam) {
this.gameteam = gameteam;
}


public String getgameopponent() {
return gameopponent;
}

public void setgameopponent(String gameopponent) {
this.gameopponent = gameopponent;
}


public String getgameIdno() {
return gameidno;
}

public void setgameIdno(String idno) {
this.gameidno = gameidno;
}


}

Answer

Just put this constructor in your GameModel Class.

 public GameModel (String gameidno,String gamedate,String gameteam,String gameopponent){
    this.gameidno= gameidno;
    this.gamedate= gamedate;
    this.gameteam= gameteam;
    this.gameopponent= gameopponent;
}

And in your activity from where you want to add values just use following code.

db.addGame(new GameModel(yourvalue, yourvalue,yourvalue,yourvalue));

I am sure it will add in your sqlite.

Comments