Android中结合OrmLite for android组件对SQLite的CRUD(增删改查)操作实例(二)
16lz
2021-01-24
4】包com.andyidea.db下DatabaseHelper.java源码:
- packagecom.andyidea.db;
- importjava.sql.SQLException;
- importAndroid.content.Context;
- importAndroid.database.sqlite.SQLiteDatabase;
- importAndroid.util.Log;
- importcom.andyidea.bean.Student;
- importcom.j256.ormlite.Android.apptools.OrmLiteSqliteOpenHelper;
- importcom.j256.ormlite.dao.Dao;
- importcom.j256.ormlite.support.ConnectionSource;
- importcom.j256.ormlite.table.TableUtils;
- publicclassDatabaseHelperextendsOrmLiteSqliteOpenHelper{
- privatestaticfinalStringDATABASE_NAME="ormlite.db";
- privatestaticfinalintDATABASE_VERSION=1;
- privateDao<Student,Integer>stuDao=null;
- publicDatabaseHelper(Contextcontext){
- super(context,DATABASE_NAME,null,DATABASE_VERSION);
- }
- /**
- *创建SQLite数据库
- */
- @Override
- publicvoidonCreate(SQLiteDatabasesqliteDatabase,ConnectionSourceconnectionSource){
- try{
- TableUtils.createTable(connectionSource,Student.class);
- }catch(SQLExceptione){
- Log.e(DatabaseHelper.class.getName(),"Unabletocreatedatbases",e);
- }
- }
- /**
- *更新SQLite数据库
- */
- @Override
- publicvoidonUpgrade(
- SQLiteDatabasesqliteDatabase,
- ConnectionSourceconnectionSource,
- intoldVer,
- intnewVer){
- try{
- TableUtils.dropTable(connectionSource,Student.class,true);
- onCreate(sqliteDatabase,connectionSource);
- }catch(SQLExceptione){
- Log.e(DatabaseHelper.class.getName(),
- "Unabletoupgradedatabasefromversion"+oldVer+"tonew"
- +newVer,e);
- }
- }
- publicDao<Student,Integer>getStudentDao()throwsSQLException{
- if(stuDao==null){
- stuDao=getDao(Student.class);
- }
- returnstuDao;
- }
- }
MainActivity.java源码:
[html]
- packagecom.andyidea.ormsqlite;
- importjava.sql.SQLException;
- importcom.andyidea.bean.Student;
- importcom.andyidea.db.DatabaseHelper;
- importcom.j256.ormlite.Android.apptools.OrmLiteBaseActivity;
- importcom.j256.ormlite.dao.Dao;
- importAndroid.content.Intent;
- importAndroid.os.Bundle;
- importAndroid.view.Menu;
- importAndroid.view.MenuItem;
- importAndroid.widget.EditText;
- publicclassMainActivityextendsOrmLiteBaseActivity<DatabaseHelper>{
- privateEditTextstuNO;
- privateEditTextstuName;
- privateEditTextstuAge;
- privateEditTextstuSex;
- privateEditTextstuScore;
- privateEditTextstuAddress;
- privateStudentmStudent;
- privateDao<Student,Integer>stuDao;
- privatefinalintMENU_ADD=Menu.FIRST;
- privatefinalintMENU_VIEWALL=Menu.FIRST+1;
- privatefinalintMENU_EDIT=Menu.FIRST+2;
- privateBundlemBundle=newBundle();
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- initializeViews();
- }
- /**
- *初始化UI界面
- */
- privatevoidinitializeViews(){
- stuNO=(EditText)findViewById(R.id.stuno);
- stuName=(EditText)findViewById(R.id.name);
- stuAge=(EditText)findViewById(R.id.age);
- stuSex=(EditText)findViewById(R.id.sex);
- stuScore=(EditText)findViewById(R.id.score);
- stuAddress=(EditText)findViewById(R.id.address);
- mBundle=getIntent().getExtras();
- if(mBundle!=null&&mBundle.getString("action").equals("viewone")){
- mStudent=(Student)getIntent().getSerializableExtra("entity");
- setStudentUIData(mStudent);
- }
- if(mBundle!=null&&mBundle.getString("action").equals("edit")){
- mStudent=(Student)getIntent().getSerializableExtra("entity");
- setStudentUIData(mStudent);
- }
- }
- @Override
- publicbooleanonPrepareOptionsMenu(Menumenu){
- if(mBundle!=null&&mBundle.getString("action").equals("viewone"))
- returnfalse;
- else
- returnsuper.onPrepareOptionsMenu(menu);
- }
- @Override
- publicbooleanonCreateOptionsMenu(Menumenu){
- if(mBundle!=null&&mBundle.getString("action").equals("edit")){
- menu.add(1,MENU_EDIT,0,"保存");
- }else{
- menu.add(0,MENU_ADD,0,"增加");
- menu.add(0,MENU_VIEWALL,0,"查看");
- }
- returnsuper.onCreateOptionsMenu(menu);
- }
- @Override
- publicbooleanonOptionsItemSelected(MenuItemitem){
- switch(item.getItemId()){
- caseMENU_ADD:
- try{
- stuDao=getHelper().getStudentDao();
- getStudentData();
- if(mStudent!=null){
- //创建记录项
- stuDao.create(mStudent);
- }
- }catch(SQLExceptione){
- e.printStackTrace();
- }
- break;
- caseMENU_VIEWALL:
- Intentintent=newIntent();
- intent.setClass(MainActivity.this,StudentListActivity.class);
- startActivity(intent);
- break;
- caseMENU_EDIT:
- try{
- getStudentData();
- stuDao=getHelper().getStudentDao();
- if(mStudent!=null){
- //更新某记录项
- stuDao.update(mStudent);
- }
- }catch(SQLExceptione){
- e.printStackTrace();
- }
- break;
- default:
- break;
- }
- returnsuper.onOptionsItemSelected(item);
- }
- /**
- *获取界面值(实体信息)
- */
- privatevoidgetStudentData(){
- mStudent=newStudent();
- mStudent.setStuNO(stuNO.getText().toString());
- mStudent.setName(stuName.getText().toString());
- mStudent.setAge(Integer.parseInt(stuAge.getText().toString()));
- mStudent.setSex(stuSex.getText().toString());
- mStudent.setScore(Double.parseDouble(stuScore.getText().toString()));
- mStudent.setAddress(stuAddress.getText().toString());
- }
- /**
- *赋值给UI界面
- *@paramstudent
- */
- privatevoidsetStudentUIData(Studentstudent){
- stuNO.setText(student.getStuNO());
- stuName.setText(student.getName());
- stuAge.setText(String.valueOf(student.getAge()));
- stuSex.setText(student.getSex());
- stuScore.setText(String.valueOf(student.getScore()));
- stuAddress.setText(student.getAddress());
- }
- }
更多相关文章
- android 界面应用锦集
- Android使用DrawLayout,ToolBar和ActionBarDrawerToggle实现抽屉
- android最新源码下载
- Android控件开发之Gallery
- okhttp源码学习分析一
- android音乐播放器(4)
- 音视频转码合成
- Android仿苹果关机界面实现代码
- Android创建和使用数据库详细指南(7)