2014-03-01 90 views
1

在我的應用程序中我存儲數據到SQLite,現在我想從SQLite將數據提取到活動從SQLite提取數據只包含單個數據行

根據要求,我只需要一次存儲單個數據,我的表將只包含不超過一行的單個數據行。

,所以我想,如果表中有數據行,然後在的onCreate形式數據並顯示獲取(..)LoginActivity.java

獲取:

The method SelectData(String) in the type myDBClass is not applicable for the arguments() 

myDBClass.java:

// Select Data 
    public String[] SelectData(String strOperatorID) { 
     // TODO Auto-generated method stub 

     try { 
      String arrData[] = null;  

      SQLiteDatabase db; 
      db = this.getReadableDatabase(); // Read Data 

      Cursor cursor = db.query(TABLE_NAME, new String[] { "*" }, 
         "OperatorID=?", 
         new String[] { String.valueOf(strOperatorID) }, null, null, null, null); 

       if(cursor != null) 
       { 
        if (cursor.moveToFirst()) { 
         arrData = new String[cursor.getColumnCount()]; 

         arrData[0] = cursor.getString(0); // DeviceID 
         arrData[1] = cursor.getString(1); // EmailID 
         arrData[2] = cursor.getString(2); // Event 
         arrData[3] = cursor.getString(3); // Operator 
         arrData[4] = cursor.getString(4); // EventOperator 
        } 
       } 

       cursor.close(); 
       db.close(); 
       return arrData; 

     } catch (Exception e) { 
      return null; 
     } 
    } 

LoginActivity.java:-

public class LoginActivity extends Activity { 
    ................. 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
     setContentView(R.layout.activity_login); 

     btnLogout = (Button) findViewById(R.id.btnLogout); 
     btnCamera = (Button) findViewById(R.id.btnCamera); 
     btnGallery = (Button) findViewById(R.id.btnGallery); 

      txtDeviceID = (TextView) findViewById(R.id.txtDeviceID); 
      txtEmailID = (TextView) findViewById(R.id.txtEmailID);  
      txtEvent = (TextView) findViewById(R.id.txtEvent); 
      txtOperative = (TextView) findViewById(R.id.txtOperative); 
      txtEventOperator = (TextView) findViewById(R.id.txtEventOperator); 

      Intent intent = getIntent(); 
      deviceID = intent.getStringExtra("deviceID"); 
      emailID = intent.getStringExtra("emailID"); 
      event = intent.getStringExtra("name"); 
      operative = intent.getStringExtra("firstName"); 

      txtDeviceID.setText(deviceID); 
      txtEmailID.setText(emailID); 
      txtEvent.setText(event); 
      txtOperative.setText(operative); 
      txtEventOperator.setText(event + " " + operative); 

      strEvent = txtEvent.getText().toString(); 
      strOperative = txtOperative.getText().toString(); 

       // Dialog 
       final AlertDialog.Builder adb = new AlertDialog.Builder(this); 
       AlertDialog ad = adb.create(); 

       // new Class DB 
       final myDBClass myDb = new myDBClass(this); 

       // Save Data 
       long saveStatus = myDb.InsertData(
          txtDeviceID.getText().toString(), 
          txtEmailID.getText().toString(), 
          txtEvent.getText().toString(), 
          txtOperative.getText().toString(), 
          txtEventOperator.getText().toString() 
          ); 

       if(saveStatus <= 0) 
       { 
        ad.setMessage("Error!! "); 
        ad.show(); 
        return; 
       } 

       // Show Data 
       String arrData[] = myDb.SelectData(); 
       if(arrData != null) 
       { 
        txtDeviceID.setText(arrData[1]); 
        txtEmailID.setText(arrData[2]); 
        txtEvent.setText(arrData[3]); 
        txtOperative.setText(arrData[4]); 
        txtEventOperator.setText(arrData[5]); 
       } 

if(txtEvent.getText().toString().equals("") && txtOperative.getText().toString().equals("")) 
       { 
        Intent intentCall = new Intent(LoginActivity.this, LicenseListActivity.class); 
        startActivity(intentCall); 
       } 
       } 
+0

post full stacktrace –

+0

爲什麼如果你只打算存儲一行數據庫?它只是最低效率和最傻的設計,你可以做。 –

+0

@kalyanpvs無法運行我的程序,因爲獲取:類型myDBClass中的方法SelectData(String)不適用於參數() – Sun

回答

2

從OP要求.. 改變你的方法是這樣..

public String[] SelectData() { 
    // TODO Auto-generated method stub 

    try { 
     String arrData[] = new String[5]; 

     SQLiteDatabase db; 
     db = this.getReadableDatabase(); // Read Data 

     Cursor cursor = db.query(TABLE_NAME, null, null, null, null, 
       null, null); 

     if (cursor != null) { 
      if (cursor.moveToFirst()) { 

       arrData[0] = cursor.getString(0); // DeviceID 
       arrData[1] = cursor.getString(1); // EmailID 
       arrData[2] = cursor.getString(2); // Event 
       arrData[3] = cursor.getString(3); // Operator 
       arrData[4] = cursor.getString(4); // EventOperator 
      } 
     } 

     cursor.close(); 
     db.close(); 
     return arrData; 

    } catch (Exception e) { 
     return null; 
    } 
} 
+0

爲什麼我們使用新的String [5];代替null? – Sun

+0

ok ..我還沒有看到這一行arrData = new String [cursor.getColumnCount()];我認爲你在空對象上執行 –

+0

@Moon這也不是好主意,如果光標包含一行,它會拋出ArraryIndexOutOf綁定異常 –

0

SelectData方法接受一個字符串參數(strOperatorID),但你不帶參數調用它,所以這顯然不能被發現。

您應該以尊重的Java命名約定爲方法的方式(即不開始大寫字母)

0
public String[] SelectData() { 
    // TODO Auto-generated method stub 

    try { 
     String arrData[] = new String[5]; 

     SQLiteDatabase db; 
     db = this.getReadableDatabase(); // Read Data 

     Cursor cursor = db.query(TABLE_NAME, null, null, null, null, 
       null, null); 

     if (cursor != null) { 
      if (cursor.moveToFirst()) { 
do{ 

       arrData[0] = cursor.getString(0); // DeviceID 
       arrData[1] = cursor.getString(1); // EmailID 
       arrData[2] = cursor.getString(2); // Event 
       arrData[3] = cursor.getString(3); // Operator 
       arrData[4] = cursor.getString(4); // EventOperator 
      } while (cur.moveToNext()); 
} 
     } 


     return arrData; 

    } catch (Exception e) { 

     return null; 
    }finally{ 
    cursor.close(); 
     db.close(); 
}