我有 2460 行可以通过单击插入到我的 sqlite 数据库中,我的问题:有什么方法可以改进此代码以提高效率并获得更好的性能?
在这种情况下保留 DBHelper 是否很好,因为我需要在具有大量查询的 ListView 中显示我的数据?
你觉得怎么样?
谢谢你,抱歉我的英语不好
我的类(class):
public class MainActivity <ButtonListener> extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnSimple = (Button) findViewById(R.id.buttonviewrec1);
btnSimple.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent (v.getContext(),ViewTeam.class);
startActivityForResult(intent, 0);
}
});
Button btnasearch = (Button) findViewById(R.id.searchbtn1);
btnasearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String tmname1= "spurs";
String tmopponent1= "lal";
String tmdate1= "26/12/2014 12:00";
String tmname2= "Maavs";
String tmopponent2= "Raps";
String tmdate2= "22/12/2014 03:30";
String tmname3= "Clips";
String tmopponent3= "Bulls";
String tmdate3= "19/12/2014 01:30";
TeamModel pm;
DBHelper db;
db = new DBHelper(getApplicationContext());
db.getWritableDatabase();
pm = new TeamModel();
pm.teamname=tmname1;
pm.teamopponent=tmopponent1;
pm.teamdate=tmdate1;
Log.i("teamname,teamopponent,teamdate", "" + pm.teamname + ""
+ pm.teamopponent + "" + pm.teamdate);
db.addTeam(pm);
pm.teamname=tmname2;
pm.teamopponent=tmopponent2;
pm.teamdate=tmdate2;
Log.i("teamname,teamopponent,teamdate", "" + pm.teamname + ""
+ pm.teamopponent + "" + pm.teamdate);
db.addTeam(pm);
pm.teamname=tmname3;
pm.teamopponent=tmopponent3;
pm.teamdate=tmdate3;
Log.i("teamname,teamopponent,teamdate", "" + pm.teamname + ""
+ pm.teamopponent + "" + pm.teamdate);
db.addTeam(pm);
}
});
}
我的DBHelper
public class DBHelper extends SQLiteOpenHelper{
public DBHelper(Context context) {
super(context, DATABASENAME, null, 33);
c = context;
}
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE if not exists teamstable(id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "teamname"
+ " TEXT,"
+ "teamopponent"
+ " TEXT,"
+ "teamdate" + " TEXT)");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + teamstable);
onCreate(db);
}
//Add record
public void addTeam(TeamModel teamitem) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("teamname", teamitem.teamname);
contentValues.put("teamopponent", teamitem.teamopponent);
contentValues.put("teamdate", teamitem.teamdate);
db.insert("teamstable", null, contentValues);
db.close();
}
我的 TeamModel 类
public class TeamModel {
public String teamname="", teamopponent="", teamdate="";
public String getTeamname() {
return teamname;
}
public void setTeamname(String teamname) {
this.teamname = teamname;
}
public String getTeamopponent() {
return teamopponent;
}
public void setTeamopponent(String teamopponent) {
this.teamopponent = teamopponent;
}
public String getTeamdate() {
return teamdate;
}
public void setTeamdate(String teamdate) {
this.teamdate = teamdate;
} }
最佳答案
好吧,你应该使用事务,它们会极大地提高性能。我还建议您重构您的代码。首先,我将为您的实体创建 DAO:
public interface TeamDAOProtocol {
void addTeam(ArrayList<TeamModel> entities);
}
public class TeamDAO implements TeamDAOProtocol {
void addTeam(ArrayList<TeamModel> entities) {
if (entities == null) return;
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction();
for (TeamModel entity : entities)
try {
ContentValues contentValues = new ContentValues();
contentValues.put("teamname", entity.getTeamname());
contentValues.put("teamopponent", entity.getTeamopponent());
contentValues.put("teamdate", entity.getTeamdate());
db.insert("teamstable", null, contentValues);
db.setTransactionSuccessful();
} catch(Exception e) {
//Error in between database transaction
e.printStackTrace();
}
db.endTransaction();
db.close();
}
}
还可以通过 getter/setter 使用 TeamModel 的属性,因为您的“pm.teamopponent”会破坏封装。
关于java - 这个 SQLITE db.insert 可以组合或改进吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27303300/