2017-02-14 116 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);