Sqlite 实现简单的增删改查主界面:功能实现:1)增加数据:2)全部显示3)清除显示4)全部删除5):ID删除6)ID查询:7)ID更新:主要代码段:DBOpenHelper.javapackage com.chen.dao;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import android.util.Log;public class DBOpenHelper extends SQLiteOpenHelper { private static final int VERSION=2;//版本private static final String DBNAME="user.db";//数据库名字//创建数据库public DBOpenHelper(Context context) {super(context, DBNAME, null, VERSION);}@Override/** 创建表*/public void onCreate(SQLiteDatabase db) {db.execSQL("create table if not exists u_user(_id integer primary key,name varchar(20),age integer,tall varchar(5))");}//版本被更新时执行@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}}UserDAO.javapackage com.chen.dao;import java.util.ArrayList;import java.util.List;import er;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;public class UserDAO {private DBOpenHelper helper;//写入,不然会是出错,是空指针public UserDAO(Context context){helper=new DBOpenHelper(context);}/*** 添加用户信息*/public void add(User user){SQLiteDatabase db=helper.getWritableDatabase();String sql="Insert into u_user(_id,name,age,tall) values(?,?,?,?)";db.execSQL(sql, new Object[]{user.getId(),user.getName(),user.getAge(),user.getTall()});db.close();}/*** 删除用户信息*/public void delete(Integer...id){if(id.length>0){StringBuffer sb=new StringBuffer();for(int i=0;i<id.length;i++){sb.append("?").append(",");}sb.deleteCharAt(sb.length()-1);SQLiteDatabase database=helper.getWritableDatabase();String sql="delete from u_user where _id in ("+sb+")";database.execSQL(sql, (Object[])id);}}/*** 删除表里的全部数据*/public void delelteall(){SQLiteDatabase database=helper.getWritableDatabase();String sql = "delete from u_user";database.execSQL(sql);}/*** 用户修改*/public void update(User user){SQLiteDatabase db=helper.getWritableDatabase();//写入数据库中注意!!!!不能放在外面String sql="update u_user set name=?,age=?,tall=? where _id=?";db.execSQL(sql, new Object[]{user.getName(),user.getAge(),user.getTall(),user.getId()});}/*** 查找用户信息*/public User find(int userid){SQLiteDatabase db=helper.getWritableDatabase();//写入数据库中注意!!!!不能放在外面String sql="select _id,name,age,tall from u_user where _id=?";Cursor cursor=db.rawQuery(sql, new String[]{String.valueOf(userid)});if(cursor.moveToNext()){return new User(cursor.getInt(cursor.getColumnIndex("_id")),cursor.getString(cursor.getColumnIndex("name")),cursor.getInt(cursor.getColumnIndex("age")),cursor.getString(cursor.getColumnIndex("tall")));}return null;}/*** 显示用户*/public Cursor select() {SQLiteDatabase db = helper.getReadableDatabase();Cursor cursor = db.query("u_user",null, null, null, null,null, "_id desc");return cursor;}}MainActivity.javapackage com.chen.database;/*** writer:ManMan*Email:*******************/import erDAO;import er;import android.app.Activity;import android.database.Cursor;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.ListView;import android.widget.SimpleCursorAdapter;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener{ private static final String TAG = "Add";private EditText edname,edage,edtall,id;private Button add,deleteshow,show,iddelete,idupdate,idshow,deleteall;private TextView tedatashow;private ListView datashow;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.main);edname = (EditText)findViewById(R.id.edname);edage = (EditText)findViewById(R.id.edage);edtall = (EditText)findViewById(R.id.edtall);id = (EditText)findViewById(R.id.id);tedatashow = (TextView)findViewById(R.id.tedatashow);datashow = (ListView)findViewById(R.id.datashow);add = (Button)findViewById(R.id.add);deleteshow = (Button)findViewById(R.id.deleteshow);show = (Button)findViewById(R.id.show);iddelete = (Button)findViewById(R.id.iddelete);idupdate = (Button)findViewById(R.id.idupdate);idshow = (Button)findViewById(R.id.idshow);deleteall = (Button)findViewById(R.id.deleteall);add.setOnClickListener(this);deleteshow.setOnClickListener(this);show.setOnClickListener(this);iddelete.setOnClickListener(this);idupdate.setOnClickListener(this);idshow.setOnClickListener(this);deleteall.setOnClickListener(this);}public void onClick(View v) {/** 添加数据*/if(v==add){if(!id.getText().toString().equals(null)&&!edname.getText().toString().equals(null)&&!edage .getText().toString().equals(null)&&!edtall.getText().toString().equals(null)){try{UserDAO userdao = new UserDAO(MainActivity.this);User user = new User(Integer.valueOf(id.getText().toString()), edname.getText().toString(),Integer.valueOf(edage.getText().toString()), edtall.getText().toString());userdao.add(user);Toast.makeText(MainActivity.this, "成功添加!", Toast.LENGTH_LONG).show();tedatashow.setText("新添数据为:"+"\n"+"ID:"+Integer.valueOf(id.getText().toString())+","+"姓名:"+edname.getText().toString()+","+"年龄:"+Integer.valueOf(edage.getText().toString())+","+"身高:"+edtall.getText().toString()+",");//设置为空edage.setText("");edname.setText("");edtall.setText("");id.setText("");}catch (Exception e) {Log.i(TAG, "出错了");Log.i(TAG, e.getMessage());}}else if(id.getText().toString().equals(null)){Toast.makeText(MainActivity.this, "ID不能为空!", Toast.LENGTH_LONG) .show();}}/** 清除显示*/if(v==deleteshow){tedatashow.setText("");}/** 全部显示*/if(v==show){try{UserDAO userdao = new UserDAO(MainActivity.this);Cursor cursor = userdao.select();/** 构建Listview适配器*/SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, yout.showuser, cursor, new String[]{"_id","name","age","tall" },newint[]{R.id.textView1,R.id.textView2,R.id.textView3,R.id.textView4});datashow.setAdapter(adapter);}catch (Exception f) {Log.e("err",f.getMessage());Log.e("err",null, f.getCause());Toast.makeText(MainActivity.this, "显示不了", Toast.LENGTH_LONG).show();}}/** ID删除*/if(v==iddelete){UserDAO userDAO=new UserDAO(MainActivity.this);//注意不能放在外面userDAO.delete(Integer.valueOf(id.getText().toString()));Toast.makeText(MainActivity.this, "您成功删除了"+Integer.valueOf(id.getText().toString()), Toast.LENGTH_LONG).show();id.setText("");}/** 全部删除*/if(v==deleteall){UserDAO userDAO=new UserDAO(MainActivity.this);//注意不能放在外面userDAO.delelteall();Toast.makeText(MainActivity.this, "您已经把数据全部删除了", Toast.LENGTH_LONG).show();}/** ID更新*/if(v==idupdate){UserDAO userDAO=new UserDAO(MainActivity.this);//注意不能放在外面User user=userDAO.find(Integer.valueOf(id.getText().toString()));user.setName(edname.getText().toString());user.setAge(Integer.valueOf(edage.getText().toString()));user.setTall(edtall.getText().toString());userDAO.update(user);Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_LONG).show();}/** ID查询*/if(v==idshow){UserDAO userDAO=new UserDAO(MainActivity.this);//注意不能放在外面User user=userDAO.find(Integer.valueOf(id.getText().toString()));tedatashow.setText("ID:"+user.getId()+" "+"姓名:"+user.getName()+" "+"年龄:"+user.getAge()+" "+"身高"+user.getTall());}/***/}}User.javapackage com.chen.modl;public class User {private int id;private String name;private int age;private String tall;/** 构造方法*/public User(){super();}public User(int id,String name,int age,String tall){ this.id=id;=name;this.age=age;this.tall=tall;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) { = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getTall() {return tall;}public void setTall(String tall) {this.tall = tall;}//复写toString()方法进行查找public String toString(){return"id:"+id+"name:"+name+"age"+age+"tall"+tall;}}Main.xml<?xml version="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><LinearLayoutandroid:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"><!--姓名 --><TextViewandroid:text="姓名:"android:layout_width="wrap_content"android:layout_height="wrap_content"/><EditTextandroid:id="@+id/edname"android:layout_height="wrap_content"android:layout_width="278dp"/></LinearLayout><LinearLayoutandroid:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"><!--年龄 --><TextViewandroid:text="年龄:"android:layout_width="wrap_content"android:layout_height="wrap_content"/><EditTextandroid:id="@+id/edage"android:layout_width="278dp"/></LinearLayout><!--身高 --><LinearLayoutandroid:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content" ><TextViewandroid:text="身高:"android:layout_width="wrap_content"android:layout_height="wrap_content" /><EditTextandroid:id="@+id/edtall"android:layout_height="wrap_content"android:layout_width="278dp"/></LinearLayout><!--按钮 --><LinearLayoutandroid:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content" ><Buttonandroid:id="@+id/add"android:text="添加数据"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="3dip"/><Buttonandroid:id="@+id/show"android:text="全部显示"android:layout_width="wrap_content"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/deleteshow"android:text="清除显示"android:layout_width="wrap_content"/><Buttonandroid:id="@+id/deleteall"android:text="全部删除"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout><!--按钮 --><LinearLayoutandroid:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"><TextViewandroid:text="ID:"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="3dip"android:layout_weight="1"/><EditTextandroid:id="@+id/id"android:layout_width="75dip"android:layout_height="wrap_content"android:layout_marginLeft="3dip"android:layout_weight="2"/><Buttonandroid:id="@+id/iddelete"android:text="ID删除"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dip"android:layout_weight="1"/><Buttonandroid:id="@+id/idshow"android:text="ID查询"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"/><Buttonandroid:id="@+id/idupdate"android:text="ID更新"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"/></LinearLayout><TextViewandroid:text="数据库数据显示:"android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/tedatashow"android:layout_width="fill_parent"android:layout_height="wrap_content"android:textSize="15dip"/><ListViewandroid:id="@+id/datashow"android:layout_width="fill_parent"android:layout_height="wrap_content"android:textSize="15dip"/></LinearLayout>。