获取第一行数据(如果存在于SQLite中)

时间:2014-03-01 07:06:14

标签: android sqlite

我正在编写一个应用程序,我需要获取第一行数据,如果存在于SQLite数据库表中,我已编写代码将数据存储到数据库中,但现在我想将数据库中的第一行数据提取到TextView(s)中

助手类: -

    // 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;
         }
    }   

正如您所看到的,我已编写代码来获取基于OperatorID =的数据?但现在我只需要将第一行数据提取到textviews中。

那么请告诉我在上面的代码中需要提交的更改,如果存在则获取第一行数据,否则不存在toast。

LoginActivity.java:

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

使用上面的代码我将数据存储到数据库中,同样地,我想从数据库中获取数据,并希望在用户访问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);
                }
          }

1 个答案:

答案 0 :(得分:8)

这样做

Cursor cursor = db.query(TABLE_NAME, new String[] { "*" }, 
                    null,
                    null, 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;