增删改查


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/huangdou"
    android:orientation="vertical"
   >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">
        <ImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:background="@drawable/katong"
            />
    </LinearLayout>
  
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">
  
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名:"
            android:textSize="35dp"/>
        <EditText
            android:id="@+id/e_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入姓名"
            android:textSize="35dp"
            />
  
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">
  
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="电话:"
            android:textSize="35dp"/>
        <EditText
            android:id="@+id/e_phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入手机号码"
            android:textSize="35dp"
            />
  
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp">
  
        <Button
            android:id="@+id/insert"
            android:layout_marginLeft="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="添加"
            android:textSize="34dp"
            android:drawableLeft="@drawable/bianse1"
            android:onClick="add"
            />
        <Button
            android:id="@+id/selet"
            android:layout_marginLeft="40dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="查询"
            android:textSize="34dp"
            android:drawableLeft="@drawable/bianse2"
            android:onClick="search"
            />
    </LinearLayout>
  
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp">
  
        <Button
            android:id="@+id/update"
            android:layout_marginLeft="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="修改"
            android:textSize="34dp"
            android:drawableLeft="@drawable/bianse3"
            android:onClick="update"
           />
        <Button
            android:id="@+id/drop"
            android:layout_marginLeft="40dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="删除"
            android:textSize="34dp"
            android:drawableLeft="@drawable/bianse4"
            android:onClick="delete"
           />
    </LinearLayout>
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/tv_show"
        android:layout_marginTop="20dp"
        android:textSize="20sp"/>
  
</LinearLayout>




package com.example.zj;
  
import androidx.appcompat.app.AppCompatActivity;
  
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
  
import java.util.ArrayList;
import java.util.List;
  
public class HdouActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hdou);
    }
  
    public void add(View view) {
        MyHelper myHelper = new MyHelper(this);
        SQLiteDatabase db = myHelper.getWritableDatabase();
        String name = ((EditText) findViewById(R.id.e_name)).getText()
                .toString();
        int phone = Integer.parseInt(((EditText) findViewById(R.id.e_phone))
                .getText().toString());
        db.execSQL("insert into stu (name,phone) values(?,?)", new Object[]{
                name, phone});
        Toast.makeText(this, "ok", Toast.LENGTH_SHORT).show();
    }
  
    public void delete(View view) {
        MyHelper myHelper = new MyHelper(this);
        SQLiteDatabase db = myHelper.getWritableDatabase();
        db.execSQL("delete from stuinfo where name=?", new Object[]{2});
        Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
    }
  
    public void update(View view) {
        MyHelper myHelper = new MyHelper(this);
        SQLiteDatabase db = myHelper.getWritableDatabase();
        db.execSQL("update stu set name=? where name=?", new Object[]{
                "micky", 3});
        Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
    }
  
    List<Student> list = new ArrayList<Student>();
  
    public void search(View view) {
        System.out.println(list.size() + "在search里");
        MyHelper myHelper = new MyHelper(this);
        SQLiteDatabase db = myHelper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from stu", null);
        if (cursor.getCount() != 0) {
            //每循环一次,创建一个学生对象并添加到集合中
            while (cursor.moveToNext()) {
//              s += cursor.getInt(0) + "   " + cursor.getString(1) + "   "
//                      + cursor.getInt(2) + "\n";
                Student s1 = new Student();
                s1.setName(cursor.getString(0));
                s1.setPhone(cursor.getInt(1));
                list.add(s1);
            }
        }
        ListView lv = (ListView) findViewById(R.id.tv_show);
        lv.setAdapter(new myadapter());
    }
  
    private class myadapter extends BaseAdapter {
        @Override
        public int getCount() {
            return list.size();
        }
  
        @Override
        public Object getItem(int i) {
            return null;
        }
  
        @Override
        public long getItemId(int i) {
            return 0;
        }
  
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
              View view1=View.inflate(HdouActivity.this,R.layout.list_item,null);
            TextView tvname=(TextView)view.findViewById(R.id.tv_name);
            TextView tvphone=(TextView) view.findViewById(R.id.tv_phone);
  
            System.out.println(list.get(i).getName());
            tvname.setText(list.get(i).getName());
            tvphone.setText(list.get(i).getName()+"");
  
            return view;
        }
    }
}

package com.example.zj;
  
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
  
import androidx.annotation.Nullable;
  
public class MyHelper extends SQLiteOpenHelper {
  
  
    public MyHelper(@Nullable Context context) {
        super(context,"itcase.db", null, 1);
    }
  
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table stu(name varchar(20) primary key,phone int(10))");
    }
  
    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
  
    }
}

package com.example.zj;
  
public class Student {
    private String name;
    private int phone;
  
  
    public String getName(){
        return name;
    }
  
    public void setName(String name){
        this.name=name;
    }
    public int getPhone(){
        return phone;
    }
  
    public void setPhone(int phone) {
        this.phone = phone;
    }
}

 

posted @ 2020-11-20 15:11  青衫故人1118  阅读(53)  评论(0编辑  收藏  举报