Android studio 数据库增删改查

 

主活动:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val dbHelper = MyDatabaseHelper(this,"School.db",3)
        val button :Button = findViewById(R.id.button)
        button.setOnClickListener{
            dbHelper.writableDatabase
        }
        val button1:Button = findViewById(R.id.button2)

        button1.setOnClickListener{
            val intent = Intent(this,xryActivity1::class.java)
            startActivity(intent)
        }

        val button2:Button = findViewById(R.id.button3)

        button2.setOnClickListener{
            val intent = Intent(this,xryActivity2::class.java)
            startActivity(intent)
        }
    }
}

插入:

class xryActivity1 : AppCompatActivity(), View.OnClickListener {
    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_xry1)
        val button4:Button = findViewById(R.id.button4)
        button4.setOnClickListener(this)

    }

    override fun onClick(v:View?){
        val dbHelper = MyDatabaseHelper(this,"School",3)
        val editText1 :EditText = findViewById(R.id.editTextid)
        val editText2 :EditText = findViewById(R.id.editTextname)
        val editText3 :EditText = findViewById(R.id.editTextcollege)
        val editText4 :EditText = findViewById(R.id.editTextage)
        val editText5 :EditText = findViewById(R.id.editTextphone)
        val db = dbHelper.writableDatabase
        when(v?.id){
            R.id.button4 ->{

                val inputText1 = editText1.text.toString()
                val inputText2 = editText2.text.toString()
                val inputText3 = editText3.text.toString()
                val inputText4 = editText4.text.toString()
                val inputText5 = editText5.text.toString()
                val values = ContentValues().apply {
                    put("id",inputText1)
                    put("name",inputText2)
                    put("college",inputText3)
                    put("phone",inputText5)
                    put("age",inputText4)

                }
                db.insert("Student",null,values)
                Toast.makeText(this,"插入成功!",Toast.LENGTH_SHORT).show()


                val intent = Intent(this,MainActivity::class.java)
                startActivityForResult(intent,1)
            }
        }

    }
}

插入界面:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text = "添加学生记录"
        android:textSize="30sp"
        android:layout_gravity="center" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="80dp"
        android:layout_marginRight="80dp"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="学号:"
            android:textSize="20sp"/>

        <EditText
            android:id="@+id/editTextid"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:textSize="20sp"
            tools:ignore="SpeakableTextPresentCheck"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="80dp"
        android:layout_marginRight="80dp"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名:"
            android:textSize="20sp"/>

        <EditText
            android:id="@+id/editTextname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:textSize="20sp"
            tools:ignore="SpeakableTextPresentCheck"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="80dp"
        android:layout_marginRight="80dp"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="学院:"
            android:textSize="20sp"/>

        <EditText
            android:id="@+id/editTextcollege"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:textSize="20sp"
            tools:ignore="SpeakableTextPresentCheck"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="80dp"
        android:layout_marginRight="80dp"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="年龄:"
            android:textSize="20sp"/>

        <EditText
            android:id="@+id/editTextage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:textSize="20sp"
            tools:ignore="SpeakableTextPresentCheck"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="80dp"
        android:layout_marginRight="80dp"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="电话:"
            android:textSize="20sp"/>

        <EditText
            android:id="@+id/editTextphone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:textSize="20sp"
            tools:ignore="SpeakableTextPresentCheck"
            />
    </LinearLayout>

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text = "插入"
        android:layout_gravity="center"
        />


</LinearLayout>

查询:

class xryActivity2 :AppCompatActivity(),AdapterView.OnItemClickListener{
     private val list= ArrayList<student>()

    @SuppressLint("Range")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recyclerview)
        val dbHelper = MyDatabaseHelper(this,"School",3)
        val db = dbHelper.writableDatabase

        val listview:ListView = findViewById(R.id.listView)
        val adapter = StuAdapter(R.layout.item,list,this)
        listview.adapter = adapter


      listview.setOnItemClickListener(this)


        val cursor = db.query("Student",null,null,null,null,null,null)
        if(cursor.moveToFirst()){
            do {
                val name = cursor.getString(cursor.getColumnIndex("name"))
                val id = cursor.getInt(cursor.getColumnIndex("id"))
                val college = cursor.getString(cursor.getColumnIndex("college"))
                val stu=student(name, id, college)
                list.add(stu)
            }while(cursor.moveToNext())
        }
        cursor.close()



    }
    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {



        val stus = list[position]


        val intent = Intent(this,xryActivity3::class.java)
        intent.putExtra("stu_id",stus.id.toString())

       startActivity(intent)

    }


}

查询界面:

<?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">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>
<?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="80dp"
    android:orientation="vertical">
    <TextView
        android:id="@+id/studentname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="20sp"
        android:paddingTop="5sp"
        android:text = "name"
        android:textSize="30sp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dp">

        <TextView
            android:id="@+id/studentid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="20sp"
            android:text="id"
            android:textSize="23sp"
            />

        <TextView
            android:id="@+id/studentcollege"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:paddingLeft="180sp"
            android:paddingRight="20sp"
            android:textSize="23sp"
            android:text="学院"/>
    </LinearLayout>





</LinearLayout>

修改和删除:

class xryActivity3 :AppCompatActivity() ,View.OnClickListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_xry3)
        val button1: Button = findViewById(R.id.button01)
        button1.setOnClickListener(this)
        val button2: Button = findViewById(R.id.button02)
        button2.setOnClickListener(this)

        val textView:TextView =findViewById(R.id.textViewid)
        textView.text=intent.getStringExtra("stu_id").toString()

    }


    override fun onClick(v: View?) {
        val dbHelper = MyDatabaseHelper(this, "School", 3)
        val id = intent.getStringExtra("stu_id")
        val textView:TextView =findViewById(R.id.textViewid)
        textView.text=id
        val editText1: EditText = findViewById(R.id.editTextname)
        val editText2: EditText = findViewById(R.id.editTextcollege)
        val editText3: EditText = findViewById(R.id.editTextage)
        val editText4: EditText = findViewById(R.id.editTextphone)
        val db = dbHelper.writableDatabase
        val values = ContentValues()
        when (v?.id) {
            R.id.button01 -> {
                val inputText1 = editText1.text.toString()
                val inputText2 = editText2.text.toString()
                val inputText3 = editText3.text.toString()
                val inputText4 = editText4.text.toString()
                values.put("name", inputText1)
                values.put("college", inputText2)
                values.put("age", inputText3)
                values.put("phone", inputText4)
                db.update("Student", values, "id = ?", arrayOf(id))
                Toast.makeText(this,"修改成功!",Toast.LENGTH_SHORT).show()
                val intent = Intent(this,xryActivity2::class.java)
                startActivityForResult(intent,1)
            }
            R.id.button02 -> {
                db.delete("Student","id = ?", arrayOf(id))
                Toast.makeText(this,"删除成功!",Toast.LENGTH_SHORT).show()
                val intent = Intent(this,xryActivity2::class.java)
                startActivityForResult(intent,1)
            }
        }

    }
}

界面:

<?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="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text = "编辑学生记录"
        android:textSize="30sp"
        android:layout_gravity="center" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="80dp"
        android:layout_marginRight="80dp"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="学号:"
            android:textSize="20sp"/>

        <TextView
            android:id="@+id/textViewid"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:text="40"
            android:textSize="20sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="80dp"
        android:layout_marginRight="80dp"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名:"
            android:textSize="20sp"/>

        <EditText
            android:id="@+id/editTextname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:textSize="20sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="80dp"
        android:layout_marginRight="80dp"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="学院:"
            android:textSize="20sp"/>

        <EditText
            android:id="@+id/editTextcollege"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:textSize="20sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="80dp"
        android:layout_marginRight="80dp"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="年龄:"
            android:textSize="20sp"/>

        <EditText
            android:id="@+id/editTextage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:textSize="20sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="80dp"
        android:layout_marginRight="80dp"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="电话:"
            android:textSize="20sp"/>

        <EditText
            android:id="@+id/editTextphone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:textSize="20sp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="80dp"
        android:layout_marginRight="80dp"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical">

        <Button
            android:id="@+id/button01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text = "修改"
            android:layout_gravity="center"
            />

        <Button
            android:id="@+id/button02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="60dp"
            android:text = "删除"
            android:layout_gravity="center"
            />
    </LinearLayout>



</LinearLayout>

student:

class student(var name:String,var id:Int,var college:String)

stuAdapter:

class StuAdapter(var resourceId:Int,var stulist:ArrayList<student>,var context: Context):BaseAdapter() {
    override fun getView(position: Int,convertView: View?,parent:ViewGroup):View{
        var v:View=LayoutInflater.from(context).inflate(R.layout.item,null)
        val model:student = stulist[position]
        val name:TextView = v.findViewById(R.id.studentname)
        val id:TextView = v.findViewById(R.id.studentid)
        val college:TextView = v.findViewById(R.id.studentcollege)
        name.text = model.name
        id.text = model.id.toString()
        college.text = model.college
        return v


    }
    override fun getCount(): Int {
        return stulist.size
    }

    override fun getItem(position: Int): student? {
        return stulist[position]
    }

    override fun getItemId(position: Int): Long {
        return 0
    }
    
}

database:

class MyDatabaseHelper(val context: Context,name:String,version:Int) : SQLiteOpenHelper(context,name,null,version) {
    private val createStudent = "create table Student (" +
            "id integer primary key," +
            "name text," +
            "college  text,"+
            "age integer,"+
            "phone integer)"

    override fun onCreate(db: SQLiteDatabase?) {
        db?.execSQL(createStudent)
        Toast.makeText(context,"Create succeeded",Toast.LENGTH_SHORT).show()
    }

    override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
        db?.execSQL("drop table if exists Student")
        onCreate(db)
    }

}

  • 2
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值