1

I am building an application with database but a problem arises when I am retrieving the values from the database it returns empty (i.e. returned cursor has 0 tuples) and I am sure that database contains some information into it. I am providing my code please help if you noticed any error.

Fragment where user inputs data to database:

    public class FCar extends Fragment implements AdapterView.OnItemSelectedListener {

    public FCar() {
    }

    CarDataBaseAdapter carDataBaseAdapter;
    SessionManagement session;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

//        inflating the view
        final View rootView = inflater.inflate(R.layout.fragment_car, container, false);
//        declaring stuff, not sure why it has to be final
        final Spinner carSpinner;
        final EditText editTextDistance, editTextMin;
        Button btnSubmitCar;

//      create new car database
        carDataBaseAdapter = new CarDataBaseAdapter(getActivity());
        carDataBaseAdapter = carDataBaseAdapter.open();
        SQLiteDatabase x = carDataBaseAdapter.getDatabaseInstance();

        session = new SessionManagement(getActivity().getApplicationContext());

//        display existing tables
/*
        Cursor hi = x.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
        hi.moveToFirst();
        int f = hi.getCount();
        String g = Integer.toString(f);
        Toast.makeText(getActivity().getApplicationContext(), g, Toast.LENGTH_LONG).show();
        while ( !hi.isAfterLast() ) {
            String h = (hi.getString( hi.getColumnIndex("name")) );
            hi.moveToNext();
            Toast.makeText(getActivity().getApplicationContext(), h, Toast.LENGTH_LONG).show();
        }
*/

        // Get References of Views
        carSpinner = (Spinner) rootView.findViewById(R.id.spinner_car_type);
        editTextDistance = (EditText) rootView.findViewById(R.id.editTextcarDistance);
        editTextMin = (EditText) rootView.findViewById(R.id.editTextcarMin);

        // Spinner click listener
        carSpinner.setOnItemSelectedListener(this);

//        set default spinner selection
        carSpinner.setSelection(0);

        // Spinner Drop down elements
        List<String> categories = new ArrayList<>();
        categories.add("Standard");
        categories.add("Truck");
        categories.add("Electric/Hybrid");

        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, categories);

        // Drop down layout style - list view with radio button
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        carSpinner.setAdapter(dataAdapter);

//      identifying the button
        btnSubmitCar = (Button) rootView.findViewById(R.id.button_car_submit);
        btnSubmitCar.setOnClickListener(new View.OnClickListener() {

                                            public void onClick(View v) {

//              take username entry from login / sign up form via method in FProfile
//                String currentUser = getSingleEntry();
                                                String username = session.getUsername();

                                                SimpleDateFormat formata = new SimpleDateFormat("MM/dd/yyyy", Locale.CANADA);
                                                String date = formata.format(new Date());

                                                String type = carSpinner.getSelectedItem().toString();

//                convert distance to integer
                                                int distance = 0;
                                                try {
                                                    distance = Integer.parseInt(editTextDistance.getText().toString());
                                                } catch (NumberFormatException nfe) {
                                                    System.out.println("Could not parse distance " + nfe);
                                                }
//                convert minutes to integer
                                                int time = 0;
                                                try {
                                                    time = Integer.parseInt(editTextMin.getText().toString());
                                                } catch (NumberFormatException nfe) {
                                                    System.out.println("Could not parse minutes" + nfe);
                                                }

                                                // Save the Data in Database
                                                carDataBaseAdapter.insertEntry(username, date, type, distance, time);
//                                              link back to input menu
                                                Intent intent = new Intent(getActivity(), MainActivity.class);

                                                intent.putExtra("caller", "Input");
                                                startActivity(intent);

                                            }
                                        }
        );
        return rootView;
    }

This is my query:

public Cursor getCarEntry(String username, String Day, String Type) {

    Cursor carCursor = db.query(true, "CAR", new String[]{"distance, time"}, "USERNAME=? AND DATE=? AND TYPE=?", new String[]{username, Day, Type}, null, null, null, "50");
    if (carCursor.getCount() < 1) {
        carCursor.close();
    }
        carCursor.moveToFirst();
        for (int i = 0; i < carCursor.getCount(); i++) {
            int distance = carCursor.getInt(carCursor.getColumnIndex("DISTANCE"));
            int time = carCursor.getInt(carCursor.getColumnIndex("TIME"));
        }
        return carCursor;

Then I retrieve it in a fragment

            carStandardCursors[i] = carDataBaseAdapter.getCarEntry(currentUsername, searchDate, "Standard");

Then I made a loop to perform math on the cursors

        for (int i = 0; i < 7; i++) {
        Cursor ccursor = carStandardCursors[i];
        carStandardTotal = CalculateCarStandard(ccursor);
        carStandard[dex] = carStandardTotal;

And I found that the values in carStandardCursors[i] were all zero.

Aurasphere
  • 3,841
  • 12
  • 44
  • 71
Sam
  • 23
  • 3

2 Answers2

0

Use Facebook Stetho Library to check whether the database contains Values or not.Then only you can get an idea.

stetho library

Rajesh.k
  • 2,327
  • 1
  • 16
  • 19
  • I used a DatabaseManager to view my database, and there is data in there. – Sam Apr 24 '17 at 23:33
  • Run with a breakpoint immediately after the query. You can then get the actual value of the interpreted query (the query string) which you can cut and paste into the DB manager. – MikeT Apr 25 '17 at 06:40
0

the problem is here,

    public Cursor getCarEntry(String username, String Day, String Type) {

Cursor carCursor = db.query(true, "CAR", **new String[]{"distance, time"}**, "USERNAME=? AND DATE=? AND TYPE=?", new String[]{username, Day, Type}, null, null, null, "50");
if (carCursor.getCount() < 1) {
    carCursor.close();
}
    carCursor.moveToFirst();
    for (int i = 0; i < carCursor.getCount(); i++) {
        int distance = carCursor.getInt(carCursor.getColumnIndex("DISTANCE"));
        int time = carCursor.getInt(carCursor.getColumnIndex("TIME"));
    }
    return carCursor;

change it to

   public Cursor getCarEntry(String username, String Day, String Type) {

Cursor carCursor = db.query(true, "CAR", new String[]{"DISTANCE", "TIME"}, "USERNAME=? AND DATE=? AND TYPE=?", new String[]{username, Day, Type}, null, null, null, "50");
if (carCursor.getCount() < 1) {
    return carCursor;
}else{
     if (carCursor!= null) {
        carCursor.moveToNext();
    }

    return carCursor;
}

and then get this cursor at your method call and then at their

   Cursor call = classObject.getCarEntry(username, Day, Type);

   int distance = call .getInt(carCursor.getColumnIndex("DISTANCE"));
   int time = call .getInt(carCursor.getColumnIndex("TIME"));

if more than one data then use while loop and ArrayAdapter

Akash Dubey
  • 1,508
  • 17
  • 34
  • Hmm, that doesn't seem to be the problem, its still giving me the exact same thing. Thanks for the reply though! – Sam Apr 24 '17 at 14:49