按任何字段搜索SQLite Android Studio

时间:2015-11-09 14:20:44

标签: android sqlite android-studio

我希望通过任意字段(而不仅仅是rollno)查看/搜索,并显示结果,例如此处所有功能的视图:

这样可以显示多个结果

package com.azim;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MyApp extends Activity implements OnClickListener
{
    EditText editRollno,editName,editMarks;
    Button btnAdd,btnDelete,btnModify,btnView,btnViewAll,btnShowInfo;
    SQLiteDatabase db;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        editRollno=(EditText)findViewById(R.id.editRollno);
        editName=(EditText)findViewById(R.id.editName);
        editMarks=(EditText)findViewById(R.id.editMarks);
        btnAdd=(Button)findViewById(R.id.btnAdd);
        btnDelete=(Button)findViewById(R.id.btnDelete);
        btnModify=(Button)findViewById(R.id.btnModify);
        btnView=(Button)findViewById(R.id.btnView);
        btnViewAll=(Button)findViewById(R.id.btnViewAll);
        btnShowInfo=(Button)findViewById(R.id.btnShowInfo);
        btnAdd.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnModify.setOnClickListener(this);
        btnView.setOnClickListener(this);
        btnViewAll.setOnClickListener(this);
        btnShowInfo.setOnClickListener(this);
        db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name VARCHAR,marks VARCHAR);");
    }
    public void onClick(View view)
    {
        if(view==btnAdd)
        {
            if(editRollno.getText().toString().trim().length()==0||
               editName.getText().toString().trim().length()==0||
               editMarks.getText().toString().trim().length()==0)
            {
                showMessage("Error", "Please enter all values");
                return;
            }
            db.execSQL("INSERT INTO student VALUES('"+editRollno.getText()+"','"+editName.getText()+
                       "','"+editMarks.getText()+"');");
            showMessage("Success", "Record added");
            clearText();
        }
        if(view==btnDelete)
        {
            if(editRollno.getText().toString().trim().length()==0)
            {
                showMessage("Error", "Please enter Rollno");
                return;
            }
            Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+editRollno.getText()+"'", null);
            if(c.moveToFirst())
            {
                db.execSQL("DELETE FROM student WHERE rollno='"+editRollno.getText()+"'");
                showMessage("Success", "Record Deleted");
            }
            else
            {
                showMessage("Error", "Invalid Rollno");
            }
            clearText();
        }
        if(view==btnModify)
        {
            if(editRollno.getText().toString().trim().length()==0)
            {
                showMessage("Error", "Please enter Rollno");
                return;
            }
            Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+editRollno.getText()+"'", null);
            if(c.moveToFirst())
            {
                db.execSQL("UPDATE student SET name='"+editName.getText()+"',marks='"+editMarks.getText()+
                        "' WHERE rollno='"+editRollno.getText()+"'");
                showMessage("Success", "Record Modified");
            }
            else
            {
                showMessage("Error", "Invalid Rollno");
            }
            clearText();
        }
        if(view==btnView)
        {
            if(editRollno.getText().toString().trim().length()==0)
            {
                showMessage("Error", "Please enter Rollno");
                return;
            }
            Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+editRollno.getText()+"'", null);
            if(c.moveToFirst())
            {
                editName.setText(c.getString(1));
                editMarks.setText(c.getString(2));
            }
            else
            {
                showMessage("Error", "Invalid Rollno");
                clearText();
            }
        }
        if(view==btnViewAll)
        {
            Cursor c=db.rawQuery("SELECT * FROM student", null);
            if(c.getCount()==0)
            {
                showMessage("Error", "No records found");
                return;
            }
            StringBuffer buffer=new StringBuffer();
            while(c.moveToNext())
            {
                buffer.append("Rollno: "+c.getString(0)+"\n");
                buffer.append("Name: "+c.getString(1)+"\n");
                buffer.append("Marks: "+c.getString(2)+"\n\n");
            }
            showMessage("Student Details", buffer.toString());
        }
        if(view==btnShowInfo)
        {
            showMessage("Student Management Application", "Developed By Azim");
        }
    }
    public void showMessage(String title,String message)
    {
        Builder builder=new Builder(this);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.show();
    }
    public void clearText()
    {
        editRollno.setText("");
        editName.setText("");
        editMarks.setText("");
        editRollno.requestFocus();
    }
}

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/myLayout"
             android:stretchColumns="0"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent">
        <TextView android:text="@string/title"
                  android:layout_x="110dp"
                  android:layout_y="10dp"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"/>
        <TextView android:text="@string/roll_no"
                  android:layout_x="30dp"
                  android:layout_y="50dp"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"/>
        <EditText android:id="@+id/editRollno"
                  android:inputType="number"     
                  android:layout_x="150dp"
                  android:layout_y="50dp"
                  android:layout_width="150dp"
                  android:layout_height="40dp"/>
        <TextView android:text="@string/name"
                  android:layout_x="30dp"
                  android:layout_y="100dp"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"/>
        <EditText android:id="@+id/editName"     
                  android:inputType="text"   
                  android:layout_x="150dp"
                  android:layout_y="100dp"
                  android:layout_width="150dp"
                  android:layout_height="40dp"/>
        <TextView android:text="@string/marks"
                  android:layout_x="30dp"
                  android:layout_y="150dp"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"/>
        <EditText android:id="@+id/editMarks"    
                  android:inputType="number"     
                  android:layout_x="150dp"
                  android:layout_y="150dp"
                  android:layout_width="150dp"
                  android:layout_height="40dp"/>
        <Button   android:id="@+id/btnAdd"
                  android:text="@string/add"
                  android:layout_x="30dp"
                  android:layout_y="200dp"
                  android:layout_width="100dp"
                  android:layout_height="40dp"/>
        <Button   android:id="@+id/btnDelete"
                  android:text="@string/delete"
                  android:layout_x="150dp"
                  android:layout_y="200dp"
                  android:layout_width="100dp"
                  android:layout_height="40dp"/>n
        <Button   android:id="@+id/btnModify"
                  android:text="@string/modify"
                  android:layout_x="30dp"
                  android:layout_y="250dp"
                  android:layout_width="100dp"
                  android:layout_height="40dp"/>
        <Button   android:id="@+id/btnView"
                  android:text="@string/view"
                  android:layout_x="150dp"
                  android:layout_y="250dp"
                  android:layout_width="100dp"
                  android:layout_height="40dp"/>
        <Button   android:id="@+id/btnViewAll"
                  android:text="@string/view_all"
                  android:layout_x="30dp"
                  android:layout_y="300dp"
                  android:layout_width="100dp"
                  android:layout_height="40dp"/>
        <Button   android:id="@+id/btnShowInfo"
                  android:text="@string/show_info"
                  android:layout_x="150dp"
                  android:layout_y="300dp"
                  android:layout_width="100dp"
                  android:layout_height="40dp"/>
</AbsoluteLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MyApp!</string>
    <string name="app_name">StudentDetailsApp</string>
    <string name="title">Student Details</string>
    <string name="roll_no">Enter Rollno: </string>
    <string name="name">Enter Name: </string>
    <string name="marks">Enter Marks: </string>
    <string name="add">Add</string>
    <string name="delete">Delete</string>
    <string name="modify">Modify</string>
    <string name="view">View</string>
    <string name="view_all">View All</string>
    <string name="show_info">Show Information</string>
</resources>

1 个答案:

答案 0 :(得分:0)

只需添加多个条件,如下所示:

更新(添加确切的列名并从EditText获取值)

    if(view==btnView)
    {
        if(editRollno.getText().toString().trim().length()==0)
        {
            showMessage("Error", "Please enter Rollno");
            return;
        }
        Cursor c=db.query(true, SQLITE_TABLE, new String[] {
            rollno,
            name,
            marks}, 
            rollno + "=?" + " OR "  +
            name + "=?" + " OR "  +
            marks + "=?", 
            new String[] {editRollno.getText().toString(),
            editName.getText().toString(),
            editMarks.getText().toString()},
            null, null, null, null);
        if(c.moveToFirst())
        {
            editName.setText(c.getString(1));
            editMarks.setText(c.getString(2));
        }
        else
        {
            showMessage("Error", "Invalid Rollno");
            clearText();
        }
    }

请考虑参加Android课程。