SQLITE游标查询有两个参数。 (一个用于表,一个用于列)

时间:2014-09-15 08:09:57

标签: android sql sqlite

我正在尝试运行下面的代码。但它只返回我表中的第一个数据。我是Android开发的新手。

public String getStudentname(String table, String studid)
{   
    Cursor cursor=mDb.query(table, null, null,  null, null, null, null);
    if(cursor.getCount()<1) // UserName does Not Exist
    {
        cursor.close();
        return "DOES NOT EXIST";
    }
    cursor.moveToFirst();
    String studname= cursor.getString(cursor.getColumnIndex("StudentName"));
    cursor.close();
    return studname;                
}

到目前为止我做了什么:

public String getStudentname(String table, String studid)
{
    Cursor cursor=mDb.query(table, null, " _id=?",  new String[]{studid},null, null, null);
    if(cursor.getCount()<1) // UserName does Not Exist
    {
        cursor.close();
        return "DOES NOT EXIST";
    }
    cursor.moveToFirst();
    String studcourse= cursor.getString(cursor.getColumnIndex("StudentName"));
    cursor.close();
    return studname;                
}

但它仍然无效。希望你能帮帮我。

这是logcat:

    09-15 17:18:16.424: E/AndroidRuntime(20504): java.lang.RuntimeException:
 Unable to resume activity {com.csu.eclassrecord/com.csu.eclassrecord.Student_Profile}:
 java.lang.IllegalArgumentException: the bind value at index 1 is null

这是我的Student_Profile的完整代码:

public class Student_Profile extends Activity implements OnClickListener {
    private LoginDataBaseAdapter loginDataBaseAdapter;
    Bitmap image;
    Bitmap bitmap;
    String picture_location;
    TextView b,c,d,e,f;
    private static final int ACTIVITY_EDIT=1;
    private StudentsDbAdapter studentsDbAdapter;
    ImageView targetImage;
    public static final String MyPREFERENCES = "MyPrefs" ;
    ListView listv;
    private String mRowId, studID;
    SimpleCursorAdapter cursorAdapter;
    Cursor cursor;
   // String table;


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.students_profile);
             // create a instance of SQLite Database
            loginDataBaseAdapter = new LoginDataBaseAdapter(this);
            loginDataBaseAdapter=loginDataBaseAdapter.open();

            studentsDbAdapter = new StudentsDbAdapter(this);
            studentsDbAdapter.open();


                b = (TextView) findViewById(R.id.studcourse);
           //   b.setText(course);

                c = (TextView) findViewById(R.id.studyear);
           //   c.setText(year);

                d = (TextView) findViewById(R.id.studnameview);
              //  d.setText(name);

                e = (TextView) findViewById(R.id.studemail);
             //   e.setText(email);

                f = (TextView) findViewById(R.id.studcontact);
             //   f.setText(contact);




                targetImage=(ImageView) findViewById(R.id.studpic);              
                targetImage.setOnClickListener(new ImageView.OnClickListener(){
                @Override
                public void onClick(View arg0) {
                    Intent   intent = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(intent, 0);
                }});

                showpic();




             }


        private void setRowIdFromIntent() {
            if (mRowId == null) {
                Bundle extras = getIntent().getExtras();            
                mRowId = extras != null ? extras.getString(loginDataBaseAdapter.KEY_secName) 
                                        : null;

            }

            if (studID == null) {
                Bundle extras = getIntent().getExtras();            
                studID = extras != null ? extras.getString(loginDataBaseAdapter.KEY_studID) 
                                        : null;

            }
        }

        @Override
        protected void onPause() {
            super.onPause();
            studentsDbAdapter.close();
            loginDataBaseAdapter.close();
        }

        @Override
        protected void onResume() {
            super.onResume();
            studentsDbAdapter.open(); 
            setRowIdFromIntent();
            populateFields();
        }




       private void populateFields()  {

       String name=studentsDbAdapter.getStudentname(mRowId, studID);
       d.setText(name);
       /**  String course=studentsDbAdapter.getStudentcourse(mRowId, studID);
          String year=studentsDbAdapter.getStudentYear(mRowId, studID);
         String contact=studentsDbAdapter.getStudentContact(mRowId, studID);
         String email=studentsDbAdapter.getStudentEmail(mRowId, studID);
           b.setText(course);
        c.setText(year);
        e.setText(email);
        f.setText(contact);
         **/

        }        

        @Override
        public void onActivityResult( int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);


            if (resultCode == Activity.RESULT_OK){
                Uri targetUri = data.getData();
                picture_location = targetUri.toString();

                Bitmap bitmap;
                try {

                    bitmap = BitmapFactory.decodeStream(this.getContentResolver().openInputStream(targetUri));  
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    byte[] byteArray = stream.toByteArray();
                    loginDataBaseAdapter.insertPhoto(byteArray);
                    showpic();


                } 
                catch (FileNotFoundException e){

                    e.printStackTrace();
                }

            }   
        }



        public void showpic() {

            LoginDataBaseAdapter db = loginDataBaseAdapter.open();
             boolean emptytab = false;
             boolean empty = db.checkPic(null, emptytab);

            //Cursor cursor = loginDataBaseAdapter.fetchProfileImageFromDatabase();  

             if(empty==false)  
             {  
                 Cursor cursor = loginDataBaseAdapter.fetchProfileImageFromDatabase();
                 cursor.moveToFirst();
                       byte[] data = cursor.getBlob(cursor.getColumnIndex("Path"));  
                        ByteArrayInputStream imageStream = new ByteArrayInputStream(data);  
                        Bitmap theImage = BitmapFactory.decodeStream(imageStream);  
                        targetImage.setImageBitmap(theImage);  
                cursor.close(); 
                    }  

            } 





        @Override
        public void onClick(View v) {
    Intent intent;

            switch (v.getId()) {
            case R.id.nameview:
                 intent = new Intent(this.getApplication(), UpdateAccount.class);
                startActivity(intent);
                break;

            default:
                break;
            }


        }


         public void onBackPressed() {
             Intent i = new Intent(getApplicationContext(), Students_Show.class);
               i.putExtra(loginDataBaseAdapter.KEY_secName, mRowId);
               startActivityForResult(i, 1);
             finish();


        return;
           }



        }

0 个答案:

没有答案