Android studio 数据库增删改查

这篇具有很好参考价值的文章主要介绍了Android studio 数据库增删改查。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

androidstudio数据库增删改查,android studio,android,数据库

 

主活动:

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:文章来源地址https://www.toymoban.com/news/detail-776046.html

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)
    }

}

到了这里,关于Android studio 数据库增删改查的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Android 数据库增删改查

    1、activity_main.xml 页面布局 代码 2、MainActivity.java 通过点击不同的按钮,进行不同的增删改查操作 3、UserDao.java 包含对数据库的增删改查方法 4、User.java 实体类对应着user表中的字段 5、SQLiteOpenHelper.java 创建表,更新表方法

    2023年04月08日
    浏览(21)
  • Android期末大作业:使用AndroidStudio开发图书管理系统APP(使用sqlite数据库)

    Android Studio开发项目图书管理系统项目视频展示: 点击进入图书管理系统项目视频 现在是一个信息高度发达的时代,伴随着科技的进步,文化的汲取,人们对于图书信息的了解与掌握也达到了一定的高度。尤其是学生对于知识的渴求更是与日俱增。图书馆作为学生学习知识的

    2024年02月08日
    浏览(41)
  • Android使用SQLite数据库实现基本的增删改查

    目录 一、创建activity_main和MainActivity界面 二、实现查询/删除功能创建activity_delete和DeleteActivity 三、实现添加功能创建activity_add和AddActivity  四、实现更新功能创建activity_update和UpdateActivity 五、创建user_data类、userInfo类和增加权限 总结 activity_main如图:  MainActivity如下 layout界面

    2024年02月08日
    浏览(38)
  • Android中SQLite数据库增删改查/使用ListView显示数据库内容(有完整源码)

    android作业笔记 编写SQLite数据库相关操作的代码,实现下图中的功能(第一排按钮布局没有调整屏幕大小适配…不过下面那一排加了 android:layout_weight=“1”) SQLite展示 先上源码:https://gitee.com/meng-fanyang/SQLiteWork 里边有三个分支,对应这不同的写法: master主分支是写的可以说

    2024年02月09日
    浏览(34)
  • [Android Studio]Android 数据存储--SQLite数据库存储

     🟧🟨🟩🟦🟪 Android Debug 🟧🟨🟩🟦🟪 Topic   发布安卓学习过程中遇到问题解决过程,希望我的解决方案可以对小伙伴们有帮助。 💯实战演练--基于SQLite数据库的通讯录实现数据的增删改查 1,创建程序 2,放置界面控件 3,编写界面交互代码 4, 核心方法讲解 5,数据库

    2024年02月08日
    浏览(37)
  • 【Android入门到项目实战--4.5】—— SQLite数据库存储实现增删改查

    目录 一、添加数据 二、更新数据 三、删除数据 四、查询数据 使用完整SQL语言操作数据库 1、添加数据  2、更新数据 3、删除数据  4、查询数据 前面学习了创建和升级数据库,本篇文章主要讲解SQLite数据库存储实现增删改查(CRUD)操作。         调用SQLiteOpenHelper的get

    2024年02月04日
    浏览(39)
  • Android Studio 学习记录-数据库

    目录 SQL的基本语法 1.数据定义语言 2.数据操纵语言 数据库管理器 SQLiteDatabase 数据库帮助器 SQLiteOpenHelper 优化记住密码功能         本文介绍Android的数据库存储方式-SQLite的使用方法,包括:SQLite用到了哪些SQL语法,如何使用数据库管理器操纵SQLite,如何使用数据库帮助器

    2024年02月07日
    浏览(41)
  • Android studio连接mysql数据库

      用Android studio连接Navicat Premium 16管理的mysql,我安装的是mysql5.7.19版本,用的包是mysql-connector-java-5.1.49-bin.jar。   在开始连接前请务必确保MySQL版本不是8.0及以上的版本!!否则会一直报错无法创建连接。   jar包也尽量选择低一些的版本,高版本我没试过,据说新版不

    2024年02月03日
    浏览(33)
  • Android studio 连接Mysql数据库

    android项目中的一些数据的存储通常可以放在数据库当中,因为数据库存储的数据比较大并且用起来也比较方便,我是在写学生宿舍管理系统时用到了数据库,在连接数据库的时候我也百度了许多方法,但都没有成功,后面是结合各个方面才连接成功,我简单分享下我的代码。

    2024年02月09日
    浏览(30)
  • Android Studio使用SQLite数据库

    1.能使用SQLiteDatabase类操作数据库与表 2.能使用SQLiteDatabaseHelper类操作数据库与表 无论是安卓应用还是苹果应用,都提供了本地轻量级数据库——SQLite,可以创建和删除数据库,还能对数据表进行增删改查操作。 SQLite由SQL编译器、内核、后端以及附件几个部分构成。SQLite通过

    2024年02月01日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包