使用Android自带的数据库sqlite进行建库建表,并完成数据的增删改查

本程序是由两个界面组成,一个是登陆界面,一个是显示界面。
1.首先编写数据库的建表代码,创建MyDatabaseHelper类,并在oncreate方法中创建两个数据库表。其中下述代码中的usertable是用户表,aaatable是行程表,代码如下:

public class MyDatabaseHelper extends SQLiteOpenHelper {
    public static final String CREATE_usertable="create table usertable("+"id integer primary key,"
                                        +"name text,"
                                        +"password text)";
    public static final String CREATE_aaatable="create table aaatable("+"aid integer primary key,"
            +"aname text,"
            +"amessage text,"
            +"atime text,"
            +"id integer)";
    private Context mContext;
    public MyDatabaseHelper(Context context , String name, SQLiteDatabase.CursorFactory factory,int version){
        super(context,name,factory,version);
        mContext=context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_usertable);
        db.execSQL(CREATE_aaatable);
        Toast.makeText(mContext,"create aaaaaaa",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}

2,编写登陆界面,在界面中放入两个EditView和一个button,本块很简单,不附代码了,主要是写两个EditView用于接收用户id和密码,然后一个确认的按钮

3.编写MainActivity函数,在函数中首先执行建立数据库代码,再调用MyDatabaseHelper的oncreate方法,再通过函数往数据库中插入表。再通过按钮点击事件,调用数据库的查询功能,查询数据库中是否有这条数据,有就跳到下一个页面,无就Toast一个密码错误的提示。
其中adddata()是往数据库表里插入数据的,因此我们运行过一遍之后就把他注释掉,或者删掉,否则会报主键不唯一的错误,代码如下:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private MyDatabaseHelper dbHelper;
    private EditText ed1,ed2;
    private Button bt1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ed1 = (EditText) findViewById(R.id.ed1);
        ed2 = (EditText) findViewById(R.id.ed2);
        bt1 = (Button) findViewById(R.id.bt1);

        dbHelper = new MyDatabaseHelper(this, "16204204.db", null, 1);
        dbHelper.getWritableDatabase();
        bt1.setOnClickListener(this);
        //adddata();

    }
    @Override
    public void onClick(View view) {
         String name=ed1.getText().toString();
         String password=ed2.getText().toString();
            SQLiteDatabase db=dbHelper.getWritableDatabase();
            String sql = "select * from usertable where id=? and password=?";
            Cursor cursor = db.rawQuery(sql, new String[] {name, password});
            if (cursor.moveToFirst()) {
                Bundle data=new Bundle();
                data.putString("id",name);
                Intent intent =new Intent(MainActivity.this,Main2Activity.class);
                intent.putExtra("id",name);
                startActivity(intent);
                cursor.close();
            }
            else{
                Toast.makeText(this, "密码输入错误,请重新输入", Toast.LENGTH_SHORT).show();
            }
    }
    public void adddata(){
        //添加数据
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        ContentValues values =new ContentValues();
        values.put("id",20);
        values.put("name","jsc");
        values.put("password","123456");
        db.insert("usertable",null,values);
        values.clear();     //第一条

        values.put("id",21);
        values.put("name","yzy");
        values.put("password","123");
        db.insert("usertable",null,values);     //第二条

        ContentValues values1 =new ContentValues();
        values1.put("aid",1);
        values1.put("aname","上海");
        values1.put("amessage","旅游");
        values1.put("atime","1.1");
        values1.put("id",20);
        db.insert("aaatable",null,values1);
        values1.clear();     //第一条

        values1.put("aid",2);
        values1.put("aname","北京");
        values1.put("amessage","办公");
        values1.put("atime","1.1");
        values1.put("id",20);
        db.insert("aaatable",null,values1);
        values1.clear();     //第二条

        values1.put("aid",3);
        values1.put("aname","广州");
        values1.put("amessage","旅游");
        values1.put("atime","1.3");
        values1.put("id",21);
        db.insert("aaatable",null,values1);
        values1.clear();     //第一条

        values1.put("aid",4);
        values1.put("aname","深圳");
        values1.put("amessage","办公");
        values1.put("atime","1.2");
        values1.put("id",21);
        db.insert("aaatable",null,values1);   //第二条
    }
}

4.编写第二个页面的xml,本页面有所有的功能的按钮和编辑栏,并且添加ListView,因为ListView可以较好得显示数据,我们采用ListView,所以第一个有ListView和增,改,删数据库的按钮和输入框;

5.编写text_item.xml,本块代码主要为ListView的显示;

6.编写usertable实体类,用于对ListView的数据写入的使用:

public class usertable {
    int aid;
    String aname;
    String amessage;
    String time;
    int id;
    public usertable(){
    }
    public usertable(int aid,String aname,String amessage,String time,int id){
        this.aid=aid;
        this.aname=aname;
        this.amessage=amessage;
        this.time=time;
        this.id=id;
    }

    public int getAid() {
        return aid;
    }

    public int getId() {
        return id;
    }

    public String getAmessage() {
        return amessage;
    }

    public String getAname() {
        return aname;
    }

    public String getTime() {
        return time;
    }

    public void setAid(int aid) {
        this.aid = aid;
    }

    public void setAmessage(String amessage) {
        this.amessage = amessage;
    }

    public void setAname(String aname) {
        this.aname = aname;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setTime(String time) {
        this.time = time;
    }
}

7.编写TextAdapter类,用于对ListView的适配类,代码如下:

public class TextAdapter extends ArrayAdapter{
    private int resourceId;
    public TextAdapter(Context context, int textViewResourceId, List objects){
        super(context,textViewResourceId,objects);
        resourceId=textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        usertable t=getItem(position);
        View view= LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
        TextView aid=(TextView)view.findViewById(R.id.aid);
        TextView aname=(TextView)view.findViewById(R.id.aname);
        TextView amessage=(TextView)view.findViewById(R.id.amessage);
        TextView time=(TextView)view.findViewById(R.id.time);
        TextView id=(TextView)view.findViewById(R.id.id);

        String a=""+t.getAid();
        String b=""+t.getId();

        aid.setText(a);
        aname.setText(t.getAname());
        amessage.setText(t.getAmessage());
        time.setText(t.getTime());
        id.setText(b);
        return view;
    }
}

8,编写第二个页面的Activity代码,主要有对数据的增删改功能,代码如下:

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
    private List textList=new ArrayList();
    private MyDatabaseHelper dbHelper;
    private EditText d1,a1,a2,a3,a4,a5,u1,u2,u3,u4;
    private Button delect,add,upata;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        dbHelper = new MyDatabaseHelper(this, "16204204.db", null, 1);
        dbHelper.getWritableDatabase();
        System.out.print("ssss");
        initmessage();

        TextAdapter adapter=new TextAdapter(Main2Activity.this,R.layout.text_item,textList);
        ListView listView=(ListView)findViewById(R.id.a);
        listView.setAdapter(adapter);

        delect=(Button)findViewById(R.id.delect);
        delect.setOnClickListener(this);
        add=(Button)findViewById(R.id.add);
        add.setOnClickListener(this);
        upata=(Button)findViewById(R.id.updata);
        upata.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.delect:
                clickdelect();
                break;
            case R.id.add:
                cilckadd();
                break;
            case R.id.updata:
                cilckupdata();
                break;
        }
    }

    private void cilckupdata() {
        u1=(EditText)findViewById(R.id.u1);
        String updata1=u1.getText().toString();
        u2=(EditText)findViewById(R.id.u2);
        String updata2=u2.getText().toString();
        u3=(EditText)findViewById(R.id.u3);
        String updata3=u3.getText().toString();
        u4=(EditText)findViewById(R.id.u4);
        String updata4=u4.getText().toString();

        SQLiteDatabase db=dbHelper.getWritableDatabase();
        ContentValues values =new ContentValues();
        values.put("aname",updata2);
        values.put("amessage",updata3);
        values.put("atime",updata4);
        db.update("aaatable",values,"aid=?",new String[]{updata1});
        Toast.makeText(this, "修改成功,请刷新或重新登陆", Toast.LENGTH_SHORT).show();

    }

    private void cilckadd() {
        a1=(EditText)findViewById(R.id.a1);
        String add1=a1.getText().toString();
        a2=(EditText)findViewById(R.id.a2);
        String add2=a2.getText().toString();
        a3=(EditText)findViewById(R.id.a3);
        String add3=a3.getText().toString();
        a4=(EditText)findViewById(R.id.a4);
        String add4=a4.getText().toString();
        a5=(EditText)findViewById(R.id.a5);
        String add5=a5.getText().toString();

        int i=Integer.parseInt(add1);
        int j=Integer.parseInt(add5);

        SQLiteDatabase db=dbHelper.getWritableDatabase();
        ContentValues values =new ContentValues();
        values.put("aid",i);
        values.put("aname",add2);
        values.put("amessage",add3);
        values.put("atime",add4);
        values.put("id",j);
        db.insert("aaatable",null,values);
        Toast.makeText(this, "添加成功,请刷新或重新登陆", Toast.LENGTH_SHORT).show();
    }

    private void clickdelect() {
        d1=(EditText)findViewById(R.id.d1);
        String a=d1.getText().toString();
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        db.delete("aaatable","aid=?",new String[]{ a});
        Toast.makeText(this, "删除成功,请刷新或重新登陆", Toast.LENGTH_SHORT).show();
    }


     public void initmessage(){
        Intent intent=getIntent();
        Bundle b=intent.getExtras();
        String name=b.getString("id");

        SQLiteDatabase db=dbHelper.getWritableDatabase();
        String sql = "select * from aaatable where id=? order by atime ";
        Cursor cursor = db.rawQuery(sql, new String[] {name});
        if (cursor.moveToFirst()) {
            do {
                int aid = cursor.getInt(cursor.getColumnIndex("aid"));
                String aname = cursor.getString(cursor.getColumnIndex("aname"));
                String amessage = cursor.getString(cursor.getColumnIndex("amessage"));
                String time = cursor.getString(cursor.getColumnIndex("atime"));
                int id = cursor.getInt(cursor.getColumnIndex("id"));
                usertable user = new usertable(aid,aname,amessage,time,id);
                textList.add(user);
            }while (cursor.moveToNext());
        }
        cursor.close();
    }


}

9.代码编辑完后运行代码,效果如下:
登陆密码错误的情况:
使用Android自带的数据库sqlite进行建库建表,并完成数据的增删改查_第1张图片

密码登陆正确的情况,显示该id下的行程记录,可在下面的功能中添加,修改和删除:
使用Android自带的数据库sqlite进行建库建表,并完成数据的增删改查_第2张图片

你可能感兴趣的:(使用Android自带的数据库sqlite进行建库建表,并完成数据的增删改查)