游标问题

时间:2017-07-09 00:42:30

标签: java android sqlite error-handling

需要从两个表中获取信息。如果它是一个游标或几个游标,那无关紧要。我只是想了解如何在几个表中进行正确的查询。试着这样做

public class ActivityTwo extends AppCompatActivity implements View.OnClickListener {


final String LOG_TAG = "myLogs";

Button btnAddCustomer;
Button btnAddItem;
Button btnRead;

EditText editNameField;
EditText editEmailField;
EditText editItemField;
EditText editPriceField;

 DBHelper dbHelperTwo;

private Cursor customers;
private Cursor orders;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_two);

    editNameField = (EditText) findViewById(R.id.edit_ac_two_name);
    editEmailField = (EditText) findViewById(R.id.edit_ac_two_email);
    editItemField = (EditText) findViewById(R.id.edit_item);
    editPriceField = (EditText) findViewById(R.id.edit_price);

    dbHelperTwo = new DBHelper(this);

    btnAddCustomer = (Button) findViewById(R.id.btn_add_customer);
    btnAddItem = (Button) findViewById(R.id.btn_add_item);
    btnRead = (Button) findViewById(R.id.btn_read_activity_two);

    btnAddItem.setOnClickListener(this);
    btnAddCustomer.setOnClickListener(this);
    btnRead.setOnClickListener(this);


}

@Override
public void onClick(View view) {

    Log.d(LOG_TAG, "--- We are inside on click: ---");

    ContentValues cv = new ContentValues();

    String name = editNameField.getText().toString();
    String email = editEmailField.getText().toString();
    String item = editItemField.getText().toString();

    String price = editPriceField.getText().toString();
    //        int intPrice=Integer.parseInt(price);

    SQLiteDatabase db = dbHelperTwo.getWritableDatabase();

    switch (view.getId()){
        case R.id.btn_add_customer:
            Log.d(LOG_TAG, "--- Insert in customers: ---");

            cv.put("name", name);
            cv.put("email", email);

            long rowID = db.insert("customers", null, cv);
            Log.d(LOG_TAG, "row inserted, ID = " + rowID);
            break;
        case R.id.btn_add_item:
            Log.d(LOG_TAG, "--- Insert in items: ---");

            cv.put("item", item);
            cv.put("price", price);

            long rowIDtwo = db.insert("orders", null, cv);
            Log.d(LOG_TAG, "row inserted, ID = " + rowIDtwo);
            break;
        case R.id.btn_read_activity_two:
            Log.d(LOG_TAG, "--- Rows in customers: ---");

            customers = db.rawQuery("SELECT * FROM customers", null);
            orders = db.rawQuery("SELECT * FROM orders", null);

            if (customers.moveToFirst()){

                Log.d(LOG_TAG, "--- Start finding data: ---");

                int idColIndex = customers.getColumnIndex("id");
                int nameColIndex = customers.getColumnIndex("name");
                int emailColIndex = customers.getColumnIndex("email");

                do {
                    Log.d(LOG_TAG,
                            "ID = " + customers.getInt(idColIndex) +
                                    ", name = " + customers.getString(nameColIndex) +
                                    ", email = " + customers.getString(emailColIndex)  );
                                        } while (customers.moveToNext());
            } else {
                Log.d(LOG_TAG, "0 rows");
                customers.close();
                break;
            }

            if (orders.moveToFirst()){

                Log.d(LOG_TAG, "--- Start finding data: ---");

                int idOrdersColIndex = orders.getColumnIndex("id");
                int itemsOrdersColIndex = orders.getColumnIndex("items");
                int priceOrdersColIndex = orders.getColumnIndex("price");
                int customersIdOrdersColIndex = orders.getColumnIndex("customers_id");

                do {
                    Log.d(LOG_TAG,
                            "ID = " + orders.getInt(idOrdersColIndex) +
                                    ", name = " + orders.getString(itemsOrdersColIndex) +
                                    ", customers_id = " + orders.getString(customersIdOrdersColIndex) +
                                    ", email = " + orders.getString(priceOrdersColIndex));
                } while (orders.moveToNext());
            } else {
                Log.d(LOG_TAG, "0 rows");
                orders.close();
                break;
            }
    }
}

class DBHelper extends SQLiteOpenHelper {

    // Constructor
    public DBHelper(Context context) {
        super(context, "customers", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d(LOG_TAG, "--- onCreate database ---");

        db.execSQL("create table customers ("
                + "id integer primary key autoincrement,"
                + "name text,"
                + "email text"+ ");");

        db.execSQL("create table orders ("
                + "id integer primary key autoincrement,"
                + "item text, price text,"
                + "customers_id int," +
                "FOREIGN KEY (customers_id) REFERENCES customers(id));");

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
} }

但它给了我错误

E/CursorWindow: Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 4 columns.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.android.myapplication3, PID: 3598
              java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
                  at android.database.CursorWindow.nativeGetString(Native Method)
                  at android.database.CursorWindow.getString(CursorWindow.java:438)
                  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
                  at com.example.android.myapplication3.ActivityTwo$override.onClick(ActivityTwo.java:149)
                  at com.example.android.myapplication3.ActivityTwo$override.access$dispatch(ActivityTwo.java)
                  at com.example.android.myapplication3.ActivityTwo.onClick(ActivityTwo.java:0)
                  at android.view.View.performClick(View.java:5198)
                  at android.view.View$PerformClick.run(View.java:21147)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

0 个答案:

没有答案