SQLite数据库操作封装

jopen 11年前
     <pre class="brush:java; toolbar: true; auto-links: false;">public class mydaHelper extends SQLiteOpenHelper{  //构造函数,这个是必须有的包括四个参数,分别是容器,数据库的名字,CursorFactory暂时不用管,版本号   public mydaHelper(Context context, String name, CursorFactory factory,    int version) {   super(context, name, factory, version);     }  // 自己写的一个含有三个参数的构造函数,它其实就是调用上面的构造函数  public mydaHelper(Context context, String name,    int version) {   this(context, name, null, version);     }  //同样这是自己写的一个含有两个参数的构造函数,来自于对上面的调用  public mydaHelper(Context context, String name) {   this(context, name,1);     }  //创建数据库时调用,会建立一个表  public void onCreate(SQLiteDatabase db) {      System.out.println("create database");   db.execSQL("create table user(id int,name text)");  }   //更新数据库时被调用  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {   System.out.println("update a Database");     }  }</pre>使用代码:    <pre class="brush:java; toolbar: true; auto-links: false;">public class Activity01 extends Activity { private Button button1; private Button button2; private Button button3; private Button button4; private Button button5; private TextView textview; private EditText  editText1; private EditText editText2;      public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);  //实例化按钮和文本以及编辑框         textview = (TextView) this.findViewById(R.id.textView1);         button1 = (Button) this.findViewById(R.id.button1);         button1.setText("create");         button2 =(Button) this.findViewById(R.id.button2);         button2.setText("insert");         button3 =(Button) this.findViewById(R.id.button3);         button4 =(Button) this.findViewById(R.id.button4);         button3.setText("更新");         button4.setText("查询");         button5 = (Button) this.findViewById(R.id.button5);         editText1 = (EditText) this.findViewById(R.id.editText1);         editText2 = (EditText) this.findViewById(R.id.editText2);         editText1.setText("");         editText2.setText("");         editText1.setEnabled(true);         editText2.setEnabled(true);                button1.setOnClickListener(new Button.OnClickListener(){          public void onClick(View v){  //调用SQLiteOpenHelper构造函数           mydaHelper dbh = new mydaHelper(Activity01.this,"my database");   //通过对mydaHelper的调用创建数据库并来执行CreateTable方法创建一个user表           SQLiteDatabase db = dbh.getReadableDatabase();          }         });        button2.setOnClickListener(new Button.OnClickListener(){         public void onClick(View v){   //通过ContentValues来向数据库里写数据          ContentValues contentValues = new ContentValues();          contentValues.put("id", editText1.getText().toString());          contentValues.put("name", editText2.getText().toString());          mydaHelper dbh = new mydaHelper(Activity01.this,"my database");  得到一个可写入的数据库          SQLiteDatabase db = dbh.getWritableDatabase();  将数据写入          db.insert("user", null, contentValues);         }        });        button3.setOnClickListener(new Button.OnClickListener(){         public void onClick(View v){          mydaHelper dbh= new mydaHelper(Activity01.this,"my database");          ContentValues contentValues = new ContentValues();          contentValues.put("name", editText2.getText().toString());          SQLiteDatabase db= dbh.getWritableDatabase();  更新数据库通过"id=?",new String[]{editText1.getText().toString()}确定要更新的位置          db.update("user", contentValues, "id=?",new String[]{editText1.getText().toString()} );         }        });        button4.setOnClickListener(new Button.OnClickListener(){         public void onClick(View v){          String n = "";          mydaHelper dbh = new mydaHelper(Activity01.this,"my database");          SQLiteDatabase db=  dbh.getReadableDatabase();     查询数据库几个参数分别是数据库的名字,要返回的内容,确定内容的标志比如id,标志内容,其他的不做介绍          Cursor cursor = db.query("user", null, null,  null, null,null, null            );  //通过指针的移动来确定是不是查询完毕          while(cursor.moveToNext()){          得到内容           int id = cursor.getInt(cursor.getColumnIndex("id"));           String name = cursor.getString(cursor.getColumnIndex("name"));           n += id+"  :  "+name+"\n";                          }      显示内容          textview.setText(n);                  }        });        button5.setOnClickListener(new Button.OnClickListener(){         public void onClick(View v){          mydaHelper dbh = new mydaHelper(Activity01.this,"my database");          SQLiteDatabase db = dbh.getWritableDatabase();  删除表中的内容几个参数分别是数据库名字,要删除的列,列的内容来确定那一列          db.delete("user", "id=?", new String[]{editText1.getText().toString()});         }        });         } }</pre>    <br />