【发布时间】:2021-03-25 14:53:16
【问题描述】:
所以我最近开始接触 Android 编程,并一直在关注this 教程,了解如何在 SQLite 数据库中插入、更新、删除和查看数据。现在,我还想在这个 SQLite 数据库中添加搜索功能,我可以在其中搜索名称(我使用的列是名称、联系人和出生日期),如果搜索的名称与数据库中的现有名称匹配,在应用程序中显示数据库中的此行/条目。我认为这可以通过与查看/更新数据库类似的方式完成,所以我尝试以这些作为参考提出解决方案,但是在尝试了很多对我来说似乎合理的方法之后,我仍然没有没有让它工作,所以我非常感谢任何帮助!我觉得我已经接近了我的一些尝试,但逻辑上的某些东西并没有完全点击。
MainActivity.java:
package com.example.sqliteapplication;
import ...
public class MainActivity extends AppCompatActivity {
EditText name, contact, dob;
Button insert, update, delete, view, search;
DBHelper DB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = findViewById(R.id.name);
contact = findViewById(R.id.contact);
dob = findViewById(R.id.dob);
insert = findViewById(R.id.btnInsert);
update = findViewById(R.id.btnUpdate);
delete = findViewById(R.id.btnDelete);
view = findViewById(R.id.btnView);
search = findViewById(R.id.btnSearch);
DB = new DBHelper(this);
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String nameTXT = name.getText().toString();
String contactTXT = contact.getText().toString();
String dobTXT = dob.getText().toString();
Boolean checkinsertdata = DB.insertuserdata(nameTXT, contactTXT, dobTXT);
if(checkinsertdata==true)
Toast.makeText(MainActivity.this, "New Entry Inserted", Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this, "New Entry Not Inserted", Toast.LENGTH_SHORT).show();
} });
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String nameTXT = name.getText().toString();
String contactTXT = contact.getText().toString();
String dobTXT = dob.getText().toString();
Boolean checkupdatedata = DB.updateuserdata(nameTXT, contactTXT, dobTXT);
if(checkupdatedata==true)
Toast.makeText(MainActivity.this, "Entry Updated", Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this, "New Entry Not Updated", Toast.LENGTH_SHORT).show();
} });
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String nameTXT = name.getText().toString();
Boolean checkudeletedata = DB.deletedata(nameTXT);
if(checkudeletedata==true)
Toast.makeText(MainActivity.this, "Entry Deleted", Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this, "Entry Not Deleted", Toast.LENGTH_SHORT).show();
} });
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Cursor res = DB.getdata();
if(res.getCount()==0){
Toast.makeText(MainActivity.this, "No Entry Exists", Toast.LENGTH_SHORT).show();
return;
}
StringBuffer buffer = new StringBuffer();
while(res.moveToNext()){
buffer.append("Name :"+res.getString(0)+"\n");
buffer.append("Contact :"+res.getString(1)+"\n");
buffer.append("Date of Birth :"+res.getString(2)+"\n\n");
}
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setCancelable(true);
builder.setTitle("User Entries");
builder.setMessage(buffer.toString());
builder.show();
} });
}
}
DBHelper.java:
package com.example.sqliteapplication;
import ...
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, "Userdata.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase DB) {
DB.execSQL("create Table Userdetails(name TEXT primary key," +
"contact TEXT, dob TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase DB, int oldVersion, int newVersion) {
DB.execSQL("drop Table if exists Userdetails");
}
public Boolean insertuserdata(String name, String contact, String dob) {
SQLiteDatabase DB = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("contact", contact);
contentValues.put("dob", dob);
long result = DB.insert("Userdetails",
null, contentValues);
if (result == -1) {
return false;
} else {
return true;
}
}
public Boolean updateuserdata(String name, String contact, String dob) {
SQLiteDatabase DB = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("contact", contact);
contentValues.put("dob", dob);
Cursor cursor = DB.rawQuery(
"Select * from Userdetails where name = ?", new String[]{name});
if (cursor.getCount() > 0) {
long result = DB.update("Userdetails",
contentValues, "name=?",
new String[]{name});
if (result == -1) {
return false;
} else {
return true;
}
} else {
return false;
}
}
public Boolean deletedata(String name) {
SQLiteDatabase DB = this.getWritableDatabase();
Cursor cursor = DB.rawQuery(
"Select * from Userdetails where name = ?", new String[]{name});
if (cursor.getCount() > 0) {
long result = DB.delete("Userdetails", "name=?",
new String[]{name});
if (result == -1) {
return false;
} else {
return true;
}
} else {
return false;
}
}
public Cursor getdata () {
SQLiteDatabase DB = this.getWritableDatabase();
Cursor cursor = DB.rawQuery(
"Select * from Userdetails ", null);
return cursor;
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
android:padding="10dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/texttitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Please enter details below"
android:textSize="24dp"
android:layout_marginTop="20dp"
/>
<EditText
android:id="@+id/name"
android:hint="Name"
android:textSize="24dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/texttitle"
android:inputType="textPersonName"
/>
<EditText
android:id="@+id/contact"
android:hint="Contact"
android:textSize="24dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:inputType="number"
/>
<EditText
android:id="@+id/dob"
android:hint="Date of Birth"
android:textSize="24dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/contact"
android:inputType="number"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnInsert"
android:textSize="24dp"
android:text="Insert New Data"
android:layout_marginTop="30dp"
android:layout_below="@+id/dob"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnUpdate"
android:textSize="24dp"
android:text="Update Data"
android:layout_below="@+id/btnInsert"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnDelete"
android:textSize="24dp"
android:text="Delete Data"
android:layout_below="@+id/btnUpdate"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnView"
android:textSize="24dp"
android:text="View Data"
android:layout_below="@+id/btnDelete"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnSearch"
android:textSize="24dp"
android:text="Search Data"
android:layout_below="@+id/btnView"
/>
</RelativeLayout>
对于一些不好的解释/没有意义的东西,我提前道歉,我才几周时间,肯定有很多东西要学!谢谢
【问题讨论】:
-
您如何使用搜索按钮?
标签: java android sqlite android-sqlite