2013-10-01 64 views
0

我想在Android的獲取自動增量列的值sqlite的

public void onCreate(SQLiteDatabase db) { 
     String CREATE_Order_Inner_TABLE = "CREATE TABLE " + TABLE_Order_Inner_Table + "(" 
       + Order_id + " AUTO_INCREMENT," 
       + Order_Date + " TEXT," 
       + Order_Time + " TEXT," 
       + Order_Total_Price + " TEXT" 
       + ")"; 



     db.execSQL(CREATE_Order_Inner_TABLE); 
    } 


    // Creating Tables 
    public void onCreate_previous_order(SQLiteDatabase db) { 

    } 

    // Upgrading database 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // Drop older table if existed 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_Order_Inner_Table); 

     // Create tables again 
     onCreate(db); 
    } 

    /** 
    * Storing user details in database 
    * */ 
    public int addInnerOrder(String Date, String time, String totalprice) { 
     SQLiteDatabase db = this.getWritableDatabase(); 

     ContentValues values = new ContentValues(); 

     values.put(Order_Date, Date); // Name 
     values.put(Order_Time, time); // Email 
     values.put(Order_Total_Price,totalprice); // Email 


     // Inserting Row 
     int id = (int) db.insert(TABLE_Order_Inner_Table, null, values); 

     db.close(); // Closing database connection 
     return id; 
    } 


    /** 
    * Getting user data from database 
    * */ 
    public ArrayList<HashMap<String, Object>> getHistroyDetails(){ 
     HashMap<String,String> user = new HashMap<String,String>(); 
     String selectQuery = "SELECT * FROM " + TABLE_Order_Inner_Table; 

     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 
     // Move to first row 
     cursor.moveToFirst(); 
//  if(cursor.getCount() > 0){ 
     while(!cursor.isAfterLast()) 
     {  
      HashMap<String, Object> map = new HashMap<String, Object>(); 
      DataHolderHistroy obj=new DataHolderHistroy(); 
      android.util.Log.i("cursor.getInt(cursor.getColumnIndex(Order_id))"," "+ cursor.getInt(cursor.getColumnIndex(Order_id))); 
      obj.setData(cursor.getInt(cursor.getColumnIndex(Order_id)),cursor.getString(cursor.getColumnIndex(Order_Date)),cursor.getString(cursor.getColumnIndex(Order_Total_Price)),cursor.getString(cursor.getColumnIndex(Order_Time))); 

      android.util.Log.i("cursor.getInt(22222222222"," "+ cursor.getString(cursor.getColumnIndex(Order_id))); 
      android.util.Log.i("cursor.gets(getStringgetString", cursor.getString(cursor.getColumnIndex(Order_Date))); 

      map.put(ITEMTITLEOrder, obj); 
      // adding HashList to ArrayList 
      OrdersList.add(map); 


      cursor.moveToNext(); 


     // } 


      } 
     cursor.close(); 
     db.close(); 
     // return user 
     return OrdersList; 
    } 






    public class DataHolderHistroy 

    { 
     int Id; 
     String Date; 
     String totalPrice; 
     String time; 

     public void setData(int id,String date,String totalprice, String time) 
     { 
      this.Id=id; 
      this.Date=date; 
      this.totalPrice=totalprice; 
      this.time=time; 
     } 

     public int Get_id() 
     {return Id;} 
     public String GetDate() 
     {return Date;} 
     public String Getprice() 
     {return totalPrice;} 
     public String Gettime() 
     {return time;} 


    } 




    } 

我logcat的自動增量列的值

10-01 17:03:49.820: I/cursor.getInt(cursor.getColumnIndex(Order_id))(23142): 0 
10-01 17:03:49.820: I/cursor.getInt(22222222222(23142): null 
10-01 17:03:49.820: I/cursor.gets(getStringgetString(23142): 01-Oct-2013 

我的問題是在這一點上

DataHolderHistroy obj=new DataHolderHistroy(); 
     android.util.Log.i("cursor.getInt(cursor.getColumnIndex(Order_id))"," "+ cursor.getInt(cursor.getColumnIndex(Order_id))); 
     obj.setData(cursor.getInt(cursor.getColumnIndex(Order_id)), 

cursor.getString(cursor.getColumnIndex(Order_Date)),cursor.getString(cursor.getColumnIndex(Order_Total_Price)),cursor.getString(cursor.getColumnIndex(Order_Time))); 

      android.util.Log.i 

("cursor.getInt(22222222222"," "+ cursor.getString(cursor.getColumnIndex(Order_id))); 
     android.util.Log.i("cursor.gets(getStringgetString", cursor.getString(cursor.getColumnIndex(Order_Date))); 

我主要問題是,我不能得到具有自動增量的列的值

+0

它的拼寫AUTOINCREMENT – njzk2

回答

3

值爲null,因爲您沒有在列中添加任何內容。

要使數據庫自動插入鍵值,請將AUTO_INCREMENT(對sqlite沒有特殊含義)的列數據類型更改爲INTEGER PRIMARY KEY AUTOINCREMENT

0

嘗試:

android.util.Log.i("cursor.getInt(22222222222"," "+ cursor.getInt(cursor.getColumnIndex(Order_id)));