Android 将表加载到Sqlite数据库
举例来说,我需要知道如何在默认情况下(当您第一次加载应用程序时发现此数据)将数据插入到我的sqlite数据库中 gameidno:1比赛日期:2014年11月30日游戏团队:热刺Android 将表加载到Sqlite数据库,android,database,sqlite,Android,Database,Sqlite,举例来说,我需要知道如何在默认情况下(当您第一次加载应用程序时发现此数据)将数据插入到我的sqlite数据库中 gameidno:1比赛日期:2014年11月30日游戏团队:热刺 游戏对手:拉尔 gameidno:2 游戏日期:2014年5月12日 游戏团队:热刺 游戏对手:小牛队 gameidno:3 比赛日期:2014年12月9日 游戏团队:热刺 游戏对手:孟菲斯 这是我的DBhelper: public void onCreate(SQLiteDatabase db) { db.
游戏对手:拉尔 gameidno:2
游戏日期:2014年5月12日
游戏团队:热刺
游戏对手:小牛队 gameidno:3
比赛日期:2014年12月9日
游戏团队:热刺
游戏对手:孟菲斯 这是我的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();
}
这是我的班级:
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;
}
}
您需要在oncreate方法中维护一个首选项变量,如下面所示:
public class AddGames extends Activity implements OnClickListener {
SharedPreferences pref;
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);
pref=getSharedPreferences("your_pref_name", MODE_PRIVATE);
boolean firstTime=pref.getBoolean("first_launch", false);
if(firstTime == false){
pm = new GamesModel();
pm.idno = "your_id";
pm.gamename = "your_game_name";
pm.reminderway = "your_reminder";
pm.addGame(pm);
Toast.makeText(Addgame.this, "Fisrt Record Added successfully.",
Toast.LENGTH_LONG).show();
SharedPreference.Editor ed=pref.edit();
pref.putBoolean("first_launch", true);
ed.commit();
}
}
只需将此构造函数放入您的
GameModel
类中
public GameModel (String gameidno,String gamedate,String gameteam,String gameopponent){
this.gameidno= gameidno;
this.gamedate= gamedate;
this.gameteam= gameteam;
this.gameopponent= gameopponent;
}
在您想要添加值的活动中,只需使用以下代码
db.addGame(new GameModel(yourvalue, yourvalue,yourvalue,yourvalue));
我相信它会添加到您的
sqlite
默认数据?我无法理解你嘿,当你第一次加载应用程序时,你会发现这个数据至少要阅读文档:@MounirElfassi你需要添加游戏模型类的构造函数。告诉我你是否需要与之相关的代码。请Guinaz告诉我怎么做
db.addGame(new GameModel(yourvalue, yourvalue,yourvalue,yourvalue));