综合上述两点,可以得出一个结论。如果数据库文件不存在,只有oncreate()被调用(该方法在创建数据库时被调用一次)。如果数据库文件存在,会调用onupdate()方法升级数据库,并更新版本号。
示例代码如下:
package hfuu.gmm; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.os.Bundle; import android.provider.ContactsContract; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class DB extends Activity { private MySQLiteHelper msql=null; private Button bt1=null; private Button bt2=null; private Button bt3=null; private Button bt4=null; private EditText ed1=null; private EditText ed2=null; private EditText ed3=null; private String text1=null; private String text2=null; private String text3=null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); msql=new MySQLiteHelper(this,"mydb.db",null,2); bt1=(Button)this.findViewById(R.id.button1); bt2=(Button)this.findViewById(R.id.button2); bt3=(Button)this.findViewById(R.id.button3); bt4=(Button)this.findViewById(R.id.button4); ed1=(EditText)this.findViewById(R.id.EditText1); ed2=(EditText)this.findViewById(R.id.EditText2); ed3=(EditText)this.findViewById(R.id.EditText3); bt1.setOnClickListener(new OnClickListener(){//增 @Override public void onClick(View arg0) { // TODO Auto-generated method stub ContentValues values=new ContentValues(); text1=ed1.getText().toString().trim(); text2=ed2.getText().toString().trim(); text3=ed3.getText().toString().trim(); values.put("_id", Integer.parseInt(text1)); values.put("name", text2); values.put("age", Integer.parseInt(text3)); msql.insert(values); Log.d("Infor", "添加成功!"); } }); bt2.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub msql.delete(Integer.parseInt(ed1.getText().toString())); Log.d("Infor", "删除成功!"); } }); bt3.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub msql.query(); } }); bt4.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub text1=ed1.getText().toString().trim(); text2=ed2.getText().toString().trim(); text3=ed3.getText().toString().trim(); msql.update(Integer.parseInt(text1),text2,Integer.parseInt(text3)); } }); } } class MySQLiteHelper extends SQLiteOpenHelper{ private static final String CREATE_TABLE_SQL="create table student(_id integer primary key autoincrement,name text,age integer)"; private static final String TBL_NAME="student"; private SQLiteDatabase db; public MySQLiteHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub Log.d("Infor", "建立数据库"); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub Log.d("Infor", "SQLite中OnCreate()调用"); this.db=db; db.execSQL(CREATE_TABLE_SQL); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub Log.d("Infor", "SQLite中OnUpgrade()调用"); } public void insert(ContentValues values){ SQLiteDatabase db=this.getWritableDatabase(); db.insert(TBL_NAME, null, values); db.close(); } public void delete(int id){ if(db!=null){ db=this.getWritableDatabase(); db.delete(TBL_NAME, "_id=?", new String[]{String.valueOf(id)}); } } public void query(){ db=this.getWritableDatabase(); Cursor c=db.query(TBL_NAME, null, null, null, null, null, null); if(c.moveToFirst()){ for(int i=0;i<c.getCount();i++){ int id=c.getInt(0); String name=c.getString(1); int age=c.getInt(2); Log.d("Infor", ""+id); Log.d("Infor", ""+name); Log.d("Infor", ""+age); c.moveToNext(); } } } public void update(int id,String name,int age){ db=this.getWritableDatabase(); String sql="update student set name='"+name+"' and age="+age+" where _id="+id; Log.d("Infor", sql); //db.execSQL(sql); ContentValues values=new ContentValues(); values.put("name", name); values.put("age", age); String []whereArgs={String.valueOf(id)}; String whereClause="_id=?"; db.update(TBL_NAME, values, whereClause, whereArgs); } public void close(){ if(db!=null) db.close(); } }