user5425093 user5425093 - 1 month ago 5
Java Question

SQLiteDatabase: Android studio custom auto incrementation

I am very new to Android studio, and SQLiteDatabases. I need help with the following:

The database, successfully creates and can display the values. However, I am trying to create a custom Unique ID for each patient when created. And the ID should be in-between 000000-999999 (6 Digit Unique ID).

The way the app works is:

1. User can "add patient"
2. User enters firstName and lastName
3. User than selects a department from a spinner
4. once the user has completed the above steps, he/she can click "Add Patient"
5. Then the values are displayed in a TabbedView, however the textView is horizontal rather than vertical.
6. and its displayed in a ListView


I have been able to achieve all of the steps above. I just now need help with creating a unique id for the patients. Any help would greatly be appreciated

Please look below for the code:

PatientDbHelper.java // Creates the tables and database

package tanav.sharma;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
* Created by Tanav on 11/4/2016.
*/
public class PatientDbHelper extends SQLiteOpenHelper {


private static final String DATABASE_NAME = "PATIENTINFO.db";
private static final int DATABASE_VERSION = 1;

private static final String CREATE_QUERY =
"CREATE TABLE "+PatientInfo.NewPatientInfo.TABLE_NAME+" ("+ PatientInfo.NewPatientInfo.PATIENT_FNAME+ " TEXT,"+PatientInfo.NewPatientInfo.PATIENT_LNAME+" TEXT,"+PatientInfo.NewPatientInfo.PATIENT_DEPARTMENT+" TEXT);";

public PatientDbHelper(Context context){

super(context,DATABASE_NAME,null,DATABASE_VERSION);
Log.e("DATABASE OPERATIONS","Database created / opened ...");
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_QUERY);
Log.e("DATABASE OPERATIONS","Table created...");

}

public void addInformations(String fname, String lname, String department, SQLiteDatabase db){

ContentValues contentValues = new ContentValues();
contentValues.put(PatientInfo.NewPatientInfo.PATIENT_FNAME,fname);
contentValues.put(PatientInfo.NewPatientInfo.PATIENT_LNAME,lname);
contentValues.put(PatientInfo.NewPatientInfo.PATIENT_DEPARTMENT,department);

db.insert(PatientInfo.NewPatientInfo.TABLE_NAME, null,contentValues);

Log.e("DATABASE OPERATIONS","One in row inserted...");
}

public Cursor getInformations(SQLiteDatabase db){
Cursor cursor;
String[] projections = {PatientInfo.NewPatientInfo.PATIENT_FNAME, PatientInfo.NewPatientInfo.PATIENT_LNAME, PatientInfo.NewPatientInfo.PATIENT_DEPARTMENT};
cursor = db.query(PatientInfo.NewPatientInfo.TABLE_NAME,projections,null,null,null,null,null);
return cursor;
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
}


PatientInfo.java // this is names of the columns and etc..

package tanav.sharma;

/**
* Created by Tanav on 11/4/2016.
*/
public class PatientInfo {

public static abstract class NewPatientInfo{

//public static final String PATIENT_ID = "patient_id";
public static final String PATIENT_FNAME = "patient_fname";
public static final String PATIENT_LNAME = "patient_lname";
public static final String PATIENT_DEPARTMENT = "patient_department";

public static final String TABLE_NAME = "patient_info";
}
}


PatientInfoProvider.java

package tanav.sharma;

/**
* Created by Tanav on 11/4/2016.
*/

public class PatientInfoProvider {

private String fname;
private String lname;
private String departments;

public PatientInfoProvider(String fname, String lname, String departments){

this.fname = fname;
this.lname = lname;
this.departments = departments;
}

public String getLname() {
return lname;
}

public void setLname(String lname) {
this.lname = lname;
}

public String getDepartments() {
return departments;
}

public void setDepartments(String departments) {
this.departments = departments;
}

public String getFname() {
return fname;
}

public void setFname(String fname) {
this.fname = fname;
}
}


The following file is to display the values in a listView with a customText view.

PatientAdapter.java

package tanav.sharma;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
* Created by Tanav on 11/4/2016.
*/
public class PatientAdapter extends ArrayAdapter {

List list = new ArrayList();


public PatientAdapter(Context context, int resource) {
super(context, resource);
}

static class LayoutHandler{
TextView FNAME,LNAME,Department;
}

@Override
public void add(Object object) {
super.add(object);
list.add(object);
}

@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int position) {
return list.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutHandler layoutHandler;
if(row == null){
LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.display_all_patients,parent,false);

layoutHandler = new LayoutHandler();
layoutHandler.FNAME = (TextView)row.findViewById(R.id.patientNameFirst);
layoutHandler.LNAME = (TextView)row.findViewById(R.id.patientNameLast);
layoutHandler.Department = (TextView) row.findViewById(R.id.patientDepartment);

row.setTag(layoutHandler);

}else{
layoutHandler = (LayoutHandler)row.getTag();

}

PatientInfoProvider patientInfoProvider = (PatientInfoProvider)this.getItem(position);
layoutHandler.FNAME.setText(patientInfoProvider.getFname());
layoutHandler.LNAME.setText(patientInfoProvider.getLname());
layoutHandler.Department.setText(patientInfoProvider.getDepartments());

return row;
}
}

Answer

The purpose of AUTOINCREMENT is to prevent the reuse of ID from previously deleted rows.

PatientInfo.java The ID column has to be the first column of the table:

public static final String PATIENT_ID = "_id";

PatientDbHelper.java

private static final String CREATE_QUERY = "CREATE TABLE " + PatientInfo.NewPatientInfo.TABLE_NAME + " ("
+ PatientInfo.NewPatientInfo.PATIENT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ PatientInfo.NewPatientInfo.PATIENT_FNAME+ " TEXT,"
+ PatientInfo.NewPatientInfo.PATIENT_LNAME+" TEXT,"
+ PatientInfo.NewPatientInfo.PATIENT_DEPARTMENT+" TEXT);";

in addInformations()

if ( id != 0 ) contentValues.put(PatientInfo.NewPatientInfo.PATIENT_ID,id);

PatientInfoProvider.java

private int id;

and

public void setId(int id) {
        this.id= id;
    }

or/and change the constructor

public PatientInfoProvider(int id, String fname, String lname, String departments){
this.id = id;
this.fname = fname;
this.lname = lname;
this.departments = departments; }

and you have to fill the ID field somewhere. If you need I could place the whole PatientInfoProvider.java

Okayy, So would my addInformation() look something like this?

public void addInformations(int id, String fname, String lname, String department, SQLiteDatabase db){

        ContentValues contentValues = new ContentValues();
        if ( id != 0 ) contentValues.put(PatientInfo.NewPatientInfo.PATIENT_ID, id);
        contentValues.put(PatientInfo.NewPatientInfo.PATIENT_FNAME,fname);
        contentValues.put(PatientInfo.NewPatientInfo.PATIENT_LNAME,lname);
        contentValues.put(PatientInfo.NewPatientInfo.PATIENT_DEPARTMENT,department);


        db.insert(PatientInfo.NewPatientInfo.TABLE_NAME, null,contentValues);

        Log.e("DATABASE OPERATIONS","One in row inserted...");
    }

Also it gives me the following error:

Error:(50, 28) error: method addInformations in class PatientDbHelper cannot be applied to given types;
required: int,String,String,String,SQLiteDatabase
found: String,String,String,SQLiteDatabase
reason: actual and formal argument lists differ in length

So i tried to fix it by applying the following lines to my code.

public void addPatient(View view){

    int id; // Added this line
    String fname = PatientFirstName.getText().toString();
    String lname = PatientLastName.getText().toString();
    String dept = departments.getSelectedItem().toString();

    patientDbHelper = new PatientDbHelper(this);
    sqLiteDatabase = patientDbHelper.getWritableDatabase();
    patientDbHelper.addInformations(id,fname,lname,dept,sqLiteDatabase); //added 'id' in arguments..
    Toast.makeText(getBaseContext(),"Data Saved",Toast.LENGTH_LONG).show();
    patientDbHelper.close();

}

However, the 'id' argument in addInformations comes underlined red and says Variable may have not been initialized