请参阅下面的解决方案,我还提供了在Kotlin中实现CRUD操作的分步过程:
使用SQITE进行CRUD操作
转到AndroidManifest.html
添加这些权限-
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
标签下方和标签上方
喜欢这里
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Androidstudio"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
}
class MainActivity : AppCompatActivity() {
后
添加此行
private lateinit var db:SQLiteDatabase
setContentView(R.layout.activity_main)
后
添加此行
db = openOrCreateDatabase("employeemanager.db", MODE_PRIVATE,null)
创建表
val createTableQuery = "CREATE TABLE IF NOT EXISTS employee(employeeid INT PRIMARY KEY, name TEXT, phonenumber INT, country TEXT)"
db?.execSQL(createTableQuery)
插入表格
val insertQuery = "INSERT INTO employee VALUES("+idtext+",'"+nametext+"',"+phonenumber+",'"+country+"')"
db?.execSQL(insertQuery)
Toast.makeText(this@MainActivity, "Inserted Data Successfully!",Toast.LENGTH_SHORT).show()
将提取的数据添加到旋转器
val fetchdata = "SELECT DISTINCT country FROM employee"
val cursor = db?.rawQuery(fetchdata,null)
val countries = ArrayList<String>()
while(cursor!!.moveToNext()) {
val country1 = cursor.getString(0)
countries.add(country1)
}
cursor?.close()
var spinner = findViewById<Spinner>(R.id.spinner)
val spinneradapter = ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,countries)
spinneradapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner.adapter = spinneradapter
spinner.onItemSelectedListener = object : OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
selectedItem = parent.getItemAtPosition(position).toString();
Toast.makeText(this@SecondActivity, "$selectedItem",Toast.LENGTH_SHORT).show()
}
override fun onNothingSelected(parent: AdapterView<*>) {
}
}
Select 数据并显示
search.setOnClickListener {
val searchcountrydata = "SELECT * FROM employee WHERE country =" +"'$selectedItem'";
val cursor2 = db?.rawQuery(searchcountrydata,null)
val displayString = StringBuilder()
while(cursor2!!.moveToNext()) {
val id = cursor2.getString(0)
val name = cursor2.getString(1)
val phonenumber = cursor2.getString(2)
val country = cursor2.getString(3)
displayString.append("ID: $id, Name: $name, PhoneNumber: $phonenumber, Country: $country" +"\n")
}
display2.text = displayString.toString()
}
排序数据
sort.setOnClickListener {
val newquery = "SELECT * FROM employee ORDER BY employeeid DESC"
val cursor3 = db?.rawQuery(newquery,null)
val displayString2 = StringBuilder()
while(cursor3!!.moveToNext()) {
val id = cursor3.getString(0)
val name = cursor3.getString(1)
val phonenumber = cursor3.getString(2)
val country = cursor3.getString(3)
displayString2.append("ID: $id, Name: $name, PhoneNumber: $phonenumber, Country: $country" +"\n")
}
display2.text = displayString2.toString()
}
更新数据
updatebutton.setOnClickListener {
val empval = empid.text.toString()
val countryval = country.text.toString()
val updatequery = "UPDATE employee SET country =" + "'$countryval'"+" WHERE employeeid = $empval"
db?.execSQL(updatequery)
Toast.makeText(this@ThirdActivity, "Updated Successfully",Toast.LENGTH_SHORT).show()
}
删除日期
delete.setOnClickListener {
val rollval = rollno.text.toString();
val deletequery = "DELETE FROM STUDENT WHERE ROLLNO=$rollval";
db?.execSQL(deletequery)
Toast.makeText(this@MainActivity,"Deleted Data Successfully!",Toast.LENGTH_SHORT).show()
}
在数据中找到最大值和最小值
maxandmin.setOnClickListener {
val selectquery1 = "SELECT productid, name, price FROM PRODUCTS p WHERE p.price IN (SELECT MAX(price) FROM PRODUCTS)"
val selectquery2 = "SELECT productid, name, price FROM PRODUCTS p WHERE p.price IN (SELECT MIN(price) FROM PRODUCTS)"
val cursor1 = db?.rawQuery(selectquery1, null)
val cursor2 = db?.rawQuery(selectquery2, null)
val str = StringBuilder()
str.append("Maximum Priced Product Details: \n")
while(cursor1!!.moveToNext()) {
val id = cursor1.getString(0)
val name = cursor1.getString(1)
val price = cursor1.getString(2)
str.append("Product ID: $id, Product Name: $name, Product Price: $price")
}
str.append("\n\nMinimum Priced Product Details: \n")
while(cursor2!!.moveToNext()) {
val id = cursor2.getString(0)
val name = cursor2.getString(1)
val price = cursor2.getString(2)
str.append("Product ID: $id, Product Name: $name, Product Price: $price")
}
查找总计数
str.append("\n\nTotal No Of Products: ")
val findtotproducts = "SELECT COUNT(*) FROM PRODUCTS"
val totprice = "SELECT SUM(PRICE) FROM PRODUCTS"
val cursor4 = db?.rawQuery(findtotproducts,null)
val cursor5 = db?.rawQuery(totprice, null)
while(cursor4!!.moveToNext()) {
val countprod = cursor4.getString(0)
str.append(countprod)
}
查找总价
str.append("\n\nTotal Price of Products: ")
while(cursor5!!.moveToNext()) {
val countprod2 = cursor5.getString(0)
str.append(countprod2)
}
display.text = str;