获取AutoIncrement Column Sqlite的值

时间:2013-10-01 12:16:21

标签: android sqlite

我想在Android中获取自动增量行的值

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)));

我的主要问题是我无法获得具有自动增量的列的值

2 个答案:

答案 0 :(得分:3)

值为null,因为您没有在列中添加任何内容。

要使数据库自动插入键值,请将列数据类型从AUTO_INCREMENT(对sqlite没有特殊含义)更改为INTEGER PRIMARY KEY AUTOINCREMENT

答案 1 :(得分:0)

尝试:

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