Sasya Sasya - 4 months ago 7
Android Question

how to get attribute table from max(id) in sqllite android?

I would like to check whether a record exists or not.

Here is what I've tried:

FormMain.java

public class FormMain extends FragmentActivity{
public Button list,export,exit, resume;
public String longitude,latitude,estate,block,ha;
private Intent i;
private SQLiteDatabase database;
MySQLiteHelper db;
private Tikos hamadetail;
private ArrayList<HashMap<String, String>> dataList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = new MySQLiteHelper(this);
db.getReadableDatabase();

dataList= new ArrayList<HashMap<String, String>>();
//resume=(Button)findViewById(R.id.bu)

final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to resume census ?")
.setCancelable(false)
.setPositiveButton("Resume", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {

int ID = db.getHighestID();
// String id1 = String.valueOf(id);
Toast.makeText(getBaseContext(), "LastID: " + ID, Toast.LENGTH_SHORT).show();

List<Tikos> tikos = new LinkedList<Tikos>();
tikos= db.getLastIdData();


// looping through All data

for(Tikos temp_vg:tikos){
temp_vg.getId();
temp_vg.gettype();
temp_vg.getdate();
temp_vg.getestate();
temp_vg.getblock();


// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();

// adding each child node to HashMap key => value
map.put("id", String.valueOf(temp_vg.getId()));
map.put("estate", temp_vg.getestate());
map.put("block", temp_vg.getblock());
map.put("ha", temp_vg.getha());

// adding HashList to ArrayList
dataList.add(map);
Toast.makeText(getBaseContext(), "Last estate: " + estate, Toast.LENGTH_SHORT).show();
Toast.makeText(getBaseContext(), "Last block: " + block, Toast.LENGTH_SHORT).show();
Toast.makeText(getBaseContext(), "Last ha: " + ha, Toast.LENGTH_SHORT).show();

Intent i = new Intent(FormMain.this, MainActivity.class);
i.putExtra("id", id);
i.putExtra("estate", estate);
i.putExtra("block", block);
i.putExtra("ha", ha);

startActivity(i);
//finish();
}}
})
.setNegativeButton("New Census", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//dialog.cancel();
showInputNameDialog();


//finish();
}
});
final AlertDialog alert = builder.create();
alert.show();


MySqliteHelper.Java

public List<Tikos> getLastIdData() {
List<Tikos> hama = new LinkedList<Tikos>();

// 1. build the query
String query = "SELECT estate,block,ha FROM " + TABLE_TIKOS+" ORDER BY MAX(id)";

// 2. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);

// 3. go over each row, build book and add it to list
Tikos tikos1 = null;
if (cursor.moveToFirst()) {
do {
tikos1 = new Tikos();

tikos1.setId(Integer.parseInt(cursor.getString(0)));
tikos1.setestate(cursor.getString(1));
tikos1.setblock(cursor.getString(2));
tikos1.setha(cursor.getString(3));


// Add vgm
hama.add(tikos1);
} while (cursor.moveToNext());
}

return hama;
}

public Cursor getAllDataLastId () {

SQLiteDatabase db=this.getWritableDatabase();

Cursor mCursor = db.query("SELECT estate,block, ha FROM tikos",
new String[] {KEY_ESTATE,KEY_BLOCK,KEY_HA},
null, null, null, null, null);

if (mCursor != null) {
mCursor.moveToFirst();
}
db.close();
return mCursor;
}

public int getHighestID() {
SQLiteDatabase db = this.getReadableDatabase();
final String MY_QUERY = "SELECT MAX(id) FROM " + TABLE_TIKOS;
Cursor cur = db.rawQuery(MY_QUERY, null);
cur.moveToFirst();
int id = cur.getInt(0);
cur.close();
return id;
}


logcat :

07-28 14:54:39.424: E/AndroidRuntime(4896): FATAL EXCEPTION: main
07-28 14:54:39.424: E/AndroidRuntime(4896): java.lang.NumberFormatException: Invalid int: "tyrrr"
07-28 14:54:39.424: E/AndroidRuntime(4896): at java.lang.Integer.invalidInt(Integer.java:138)
07-28 14:54:39.424: E/AndroidRuntime(4896): at java.lang.Integer.parse(Integer.java:375)
07-28 14:54:39.424: E/AndroidRuntime(4896): at java.lang.Integer.parseInt(Integer.java:366)
07-28 14:54:39.424: E/AndroidRuntime(4896): at java.lang.Integer.parseInt(Integer.java:332)
07-28 14:54:39.424: E/AndroidRuntime(4896): at com.titikkosong.MySQLiteHelper.getLastIdData(MySQLiteHelper.java:157)
07-28 14:54:39.424: E/AndroidRuntime(4896): at com.titikkosong.FormMain$1.onClick(FormMain.java:184)
07-28 14:54:39.424: E/AndroidRuntime(4896): at android.view.View.performClick(View.java:4475)
07-28 14:54:39.424: E/AndroidRuntime(4896): at android.view.View$PerformClick.run(View.java:18786)
07-28 14:54:39.424: E/AndroidRuntime(4896): at android.os.Handler.handleCallback(Handler.java:730)
07-28 14:54:39.424: E/AndroidRuntime(4896): at android.os.Handler.dispatchMessage(Handler.java:92)
07-28 14:54:39.424: E/AndroidRuntime(4896): at android.os.Looper.loop(Looper.java:137)
07-28 14:54:39.424: E/AndroidRuntime(4896): at android.app.ActivityThread.main(ActivityThread.java:5419)
07-28 14:54:39.424: E/AndroidRuntime(4896): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 14:54:39.424: E/AndroidRuntime(4896): at java.lang.reflect.Method.invoke(Method.java:525)
07-28 14:54:39.424: E/AndroidRuntime(4896): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
07-28 14:54:39.424: E/AndroidRuntime(4896): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
07-28 14:54:39.424: E/AndroidRuntime(4896): at dalvik.system.NativeStart.main(Native Method)


But it doesn't get it to the catch estate, block, and ha from max(id) to display the toast.

What am I doing wrong in here?

Answer

Is this your full code? Because the following line should produce an exception but your don't seem to be catching it?

String query = "SELECT  estate,block,ha FROM " + TABLE_TIKOS+" ORDER BY MAX(id)";

This will result in a

Query Error: misuse of aggregate: max() Unable to execute statement

To use ORDER BY MAX(id) in this manner your query would need a GROUP BY clause. Try something like this:

String query = "SELECT  estate,block,ha FROM " + TABLE_TIKOS
   +" GROUP BY some_column ORDER BY MAX(id)";
Comments