2015-11-09 57 views
0

我想查看/搜索按任何領域(而不僅僅是rollno)並顯示結果的觀點一樣,所有的功能在這裏所做的:搜索任何領域的SQLite的Android工作室

使一個以上的結果可以被顯示

包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> 

回答

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課程。

+1

嗨,感謝您的回答,我對android和sqlite非常陌生,並且想知道我可以將這些條件放入我當前的代碼中嗎? – Julie

+0

感謝,但它似乎沒有工作,在那裏我會保存: - 「// TODO:您的EditText的內容保存到變量:rollVar,nameVar,marksVar」 此外,KEY_ROLLNO ETC給出了一個錯誤。 謝謝 – Julie

+0

您是否測試過提供的解決方案?讓我知道 –