2014-02-17 177 views
5

我在爲我的android應用程序創建數據庫的最後階段,但是,我似乎無法讓我的主鍵增加。這裏是我的代碼,我將它設置,Android SQLite - 主鍵 - 插入表

public class DatabaseHandler extends SQLiteOpenHelper { 

    // All Static variables 
    // Database Version 
    private static final int DATABASE_VERSION = 17; 

    // Database Name 
    private static final String DATABASE_NAME = "journeyManager"; 

    // Contacts table name 
    public static final String TABLE_JOURNEY = "journey"; 

    // Contacts Table Columns names 
    private static final String KEY_P = "key"; 
    private static final String KEY_ID = "id"; 
    private static final String KEY_DIST = "distance"; 
    private static final String KEY_MPG = "mpg"; 
    private static final String KEY_COST = "cost"; 

    public DatabaseHandler(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    // Creating Tables 
    @Override 
    public void onCreate(SQLiteDatabase db) { 
     String CREATE_JOURNEY_TABLE = "CREATE TABLE " + TABLE_JOURNEY + "(" 
       + KEY_P + " INTEGER PRIMARY KEY," + KEY_ID + " TEXT," + KEY_DIST + " TEXT," 
       + KEY_MPG + " TEXT," + KEY_COST + " TEXT)"; 
     db.execSQL(CREATE_JOURNEY_TABLE); 
    } 

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

     // Create tables again 
     onCreate(db); 
    } 

    /** 
    * All CRUD(Create, Read, Update, Delete) Operations 
    */ 

    // Adding new contact 
    void addJourneyData(Journey journey) { 
     SQLiteDatabase db = this.getWritableDatabase(); 

     ContentValues values = new ContentValues(); 
     values.put(KEY_P, journey.getpKey()); 
     values.put(KEY_ID, journey.getId()); 
     values.put(KEY_DIST, journey.getDistance()); // Contact Name 
     values.put(KEY_MPG, journey.getMpg()); // Contact Phone 
     values.put(KEY_COST, journey.getCost()); // Contact Phone 

     // Inserting Row 
     db.insert(TABLE_JOURNEY, null, values); 
     db.close(); // Closing database connection 
    } 

    // Getting single contact 
    Journey getJourney(int id) { 
     SQLiteDatabase db = this.getReadableDatabase(); 

     Cursor cursor = db.query(TABLE_JOURNEY, new String[] { KEY_P + KEY_ID + 
       KEY_DIST, KEY_MPG, KEY_COST }, KEY_P + "=?", 
       new String[] { String.valueOf(id) }, null, null, null, null); 
     if (cursor != null) 
      cursor.moveToFirst(); 

     Journey journey = new Journey(); 
     journey.setPkey(Integer.parseInt(cursor.getString(0))); 
     journey.setId(String.valueOf(cursor.getString(1))); 
     journey.setMpg(String.valueOf(cursor.getString(2))); 
     journey.setDistance(String.valueOf(cursor.getString(3))); 
     journey.setCost(String.valueOf(cursor.getString(4))); 
     // return contact 
     return journey; 
    } 

    // Getting All Contacts 
    public List<Journey> getAllJourneys() { 
     List<Journey> journeyList = new ArrayList<Journey>(); 
     // Select All Query 
     String selectQuery = "SELECT * FROM " + TABLE_JOURNEY; 

     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 

     // looping through all rows and adding to list 
     if (cursor.moveToFirst()) { 
      do { 
       Journey journey = new Journey(); 
       journey.setPkey(Integer.parseInt(cursor.getString(0))); 
       journey.setId(String.valueOf(cursor.getString(1))); 
       journey.setMpg(String.valueOf(cursor.getString(2))); 
       journey.setDistance(String.valueOf(cursor.getString(3))); 
       journey.setCost(String.valueOf(cursor.getString(4))); 
       // Adding contact to list 
       journeyList.add(journey); 
      } while (cursor.moveToNext()); 
     } 

     // return contact list 
     return journeyList; 
    } 
} 

這裏是我添加細節到從另一個活動的按鈕數據庫,

db.addJourneyData(new Journey(1,timeStamp, distanceLabel, mpgAnswer, pplAnswer)); 

我切入正題,在那裏將添加第一個,但從那以後它會說主鍵不是唯一的 - 因此它不更新數據庫。

另外,我希望數據按降序排列,爲此,我使用DESC,但我應該在哪裏放置它?

任何幫助,將不勝感激,

非常感謝,

回答

8

爲了使數據庫自動爲您生成主鍵,只是不自己指定。從插入代碼刪除此行:

values.put(KEY_P, journey.getpKey()); 

您可以從insert()的返回值捕捉生成的ID。

另外,我希望數據按降序排列,爲此,我使用DESC,但我應該在哪裏放置它?

假設適用於getAllJourneys(),你做一個rawQuery(),只需添加ORDER BY直接在SQL:

String selectQuery = "SELECT * FROM " + TABLE_JOURNEY + " ORDER BY " + KEY_P + " DESC"; 
0

無需values.put(KEY_P, journey.getpKey());

的SQL會爲你做的。 PRIMARY KEY函數將自動爲每個記錄添加一個唯一的ID。

您可以在SQL中使用ORDER BY來指定順序。看here欲瞭解更多信息。

您還應明確聲明您的方法是private

3

經過以下幾個步驟

使用

INTEGER PRIMARY KEY AUTOINCREMENT DEFAULT 1 

你可以從1

開始增量修改這樣

// Creating Tables 
@Override 
public void onCreate(SQLiteDatabase db) { 
    String CREATE_JOURNEY_TABLE = "CREATE TABLE " + TABLE_JOURNEY + "(" 
      + KEY_P + " INTEGER PRIMARY KEY AUTOINCREMENT DEFAULT 1 ," + KEY_ID + " TEXT," + KEY_DIST + " TEXT," 
      + KEY_MPG + " TEXT," + KEY_COST + " TEXT)"; 
    db.execSQL(CREATE_JOURNEY_TABLE); 
} 

您創建表的代碼

然後刪除以下代碼

values.put(KEY_P, journey.getpKey());