我想检查记录是否存在。
这是我尝试过的:
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)
但是它没有从最大(id)中捕获庄园,阻止和ha来显示吐司。
我在这里做错了什么?
答案 0 :(得分:2)
这是您的完整代码吗?因为以下行应该产生例外,但你似乎没有抓住它?
String query = "SELECT estate,block,ha FROM " + TABLE_TIKOS+" ORDER BY MAX(id)";
这将导致
查询错误:滥用聚合:max()无法执行语句
要以这种方式使用ORDER BY MAX(id),您的查询将需要GROUP BY子句。尝试这样的事情:
String query = "SELECT estate,block,ha FROM " + TABLE_TIKOS
+" GROUP BY some_column ORDER BY MAX(id)";
答案 1 :(得分:-1)
"SELECT MAX(id),estate,block,ha FROM " + TABLE_TIKOS + " where id=MAX(id)"