2017-02-14 182 views
0

我有一個表中有一列,我已經設置該列的值默認爲0;但每當我訪問該值,它無法訪問。下面的代碼,我已經嘗試過。Android數據庫提取數據庫值

public void getmaxoid() { 
     SQLiteDatabase database = dataforcategorylistview.getReadableDatabase(); 
     String[] columns = {"order_id"}; 
     Cursor cursors = database.rawQuery("Select max(order_id) from sales_item", null); 
     if (cursors.moveToNext()) { 
      convertedorderid = cursors.getInt(cursors.getColumnIndexOrThrow("order_id")); 

      if (convertedorderid==0) { 
       int oid = 0; 
       //int oid = Integer.parseInt(OrderIdFromService); 
       serverorderid = String.valueOf(convertedorderid++); 
       orderidset.setText(serverorderid); 
      } else { 

       serverorderid = String.valueOf(convertedorderid++); 
       orderidset.setText(serverorderid); 
      } 
      cursors.close(); 
     } 

    } And this is my table: 
String salesitem = "CREATE TABLE `sales_item` (`sales_id` integer NOT NULL,`order_id` numeric DEFAULT 0,`menu_id` nvarchar(500) NOT NULL,`qty` integer NOT NULL,`rate` numeric NOT NULL,`total_amount` numeric NOT NULL,`w_id` numeric NOT NULL,`kot_id` integer NOT NULL,PRIMARY KEY(`sales_id`))"; 

回答

0

查詢更改爲

Cursor cursors = database.rawQuery("Select max(order_id) as max_order_id from sales_item", null); 

convertedorderid = cursors.getInt(cursors.getColumnIndexOrThrow("max_order_id")); 
+0

感謝您提供解決方案 – Dipak

0

遊標中列名是一樣的東西max(order_id)。如果你只有一列,你可能只是硬編碼列索引:

convertedorderid = cursors.getInt(0); 

無論如何,它會更容易使用helper function讀取查詢的結果值:

long max_id = DatabaseUtils.longForQuery(database, 
     "Select max(order_id) from sales_item", null);