这个SQLITE db.insert可以组合或改进吗?

时间:2014-12-04 20:22:04

标签: java android sqlite listview

我有2460行可以通过点击插入我的sqlite数据库中,我的问题是:有什么方法可以提高此代码的效率并获得更好的性能?

保持DBHelper在这种情况下表现良好,我需要在Listview中显示我的数据并进行大量查询吗?

你觉得怎么样?

谢谢,对不起我的英文

我的课程:

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;
} }

1 个答案:

答案 0 :(得分:1)

嗯,你应该使用交易,它们会非常提高性能。另外我建议你重构你的代码。首先,我会为您的实体创建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”打破了封装。