Android SQLite – 主键 – 插入表格

前端之家收集整理的这篇文章主要介绍了Android SQLite – 主键 – 插入表格前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在为我的 Android应用程序创建数据库的最后阶段,但是,我似乎无法让我的主键增加.这是我设置的代码,
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,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);
        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,但我应该在哪里放置它?

任何帮助,将不胜感激,

非常感谢,

解决方法

要使数据库自动为您生成主键,请不要自己指定.从插入代码删除此行:
values.put(KEY_P,journey.getpKey());

您可以从返回值insert()捕获生成的id.

Also,I want the data to be in descending order,to do this,I use DESC,but where shall I place this?

假设这适用于执行rawQuery()的getAllJourneys(),只需在sql中直接添加ORDER BY:

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

猜你在找的Android相关文章