Android:如何增加SQLite INTEGER PRIMARY KEY AUTOINCREMENT列?

时间:2016-05-01 03:42:26

标签: android sqlite android-edittext

在我的活动中,我有一个Save按钮,点击后会将一堆用户输入数据保存到SQLite表中。在同一次单击,我想在另一个表中增加一个整数计数器。计数器设置为“cardnum”。

我在这里缺少什么?

活动文件:

...
public void onClickSave(View v) {

    helper = new DatabaseHelper(CardViewActivity.this);
    helper.insertIntoTableTOTALCOUNT();
    ...
    startActivity(new Intent(CardViewActivity.this,ListActivity.class));
}     

DBContract.java

public final class DBContract {
    public DBContract() {}

    public static abstract class DBEntry implements BaseColumns {

        // Strings for USERINPUTS TABLE
        public static final String TABLE_NAME_USERINPUTS = "userinputs";
        public static final String COLUMN_NAME_ID = "_id";
        public static final String COLUMN_NAME_TODO = "todo";
        public static final String COLUMN_NAME_NOTE1 = "note1";
        public static final String COLUMN_NAME_NOTE2 = "note2";
        public static final String COLUMN_NAME_DUEDATE = "duedate";
        public static final String COLUMN_NAME_DUETIME = "duetime";
        public static final String COLUMN_NAME_TIMESTAMP = "timestamp";

        // Strings for COUNT TABLE
        public static final String TABLE_NAME_TOTALCOUNT = "totalcount";
        public static final String COLUMN_NAME_COUNTID = "count_id";
        public static final String COLUMN_NAME_CARDNUM = "cardnum";
    }
}

DBHelper文件:

...
@Override
public void onCreate(SQLiteDatabase db) {
    // Set up the Column headings in USERINPUTS TABLE
    db.execSQL(SQL_CREATE_ENTRIES);
    // Set up the Column headings in TOTALCOUNT TABLE
    db.execSQL(SQL_CREATE_COUNTER);
    // Put a default zero value in the CARDNUM counter to get started.
    db.execSQL("INSERT INTO " + DBContract.DBEntry.TABLE_NAME_TOTALCOUNT +
          "(count_id," + DBContract.DBEntry.COLUMN_NAME_CARDNUM+") VALUES (1,0)");
}

private static final String SQL_CREATE_ENTRIES =
        "CREATE TABLE IF NOT EXISTS " + DBContract.DBEntry.TABLE_NAME_USERINPUTS +
            "(" + DBContract.DBEntry.COLUMN_NAME_ID +
                  " INTEGER PRIMARY KEY,"
                + DBContract.DBEntry.COLUMN_NAME_TODO +
                  " TEXT,"
                + DBContract.DBEntry.COLUMN_NAME_NOTE1 +
                  " TEXT,"
                + DBContract.DBEntry.COLUMN_NAME_NOTE2 +
                  " TEXT,"
                + DBContract.DBEntry.COLUMN_NAME_DUEDATE +
                  " TEXT,"
                + DBContract.DBEntry.COLUMN_NAME_DUETIME +
                  " TEXT,"
                + DBContract.DBEntry.COLUMN_NAME_TIMESTAMP +
                  " INTEGER" + ")";

private static final String SQL_CREATE_COUNTER =
        "CREATE TABLE IF NOT EXISTS " + DBContract.DBEntry.TABLE_NAME_TOTALCOUNT +
            "(" + DBContract.DBEntry.COLUMN_NAME_COUNTID+
                  " INTEGER UNIQUE, "
                + DBContract.DBEntry.COLUMN_NAME_CARDNUM +
                  " INTEGER" + ")";

    public void insertIntoTableTOTALCOUNT() {

    SQLiteDatabase db = this.getWritableDatabase();

    db.beginTransaction();

    db.execSQL(INCREMENT_COUNT1);
    db.execSQL(INCREMENT_COUNT2);

    db.setTransactionSuccessful();
    db.endTransaction();
    // Close the DB
    if(db.isOpen())
    db.close();
}

public static final String INCREMENT_COUNT1 = "INSERT OR IGNORE INTO "+ DBContract.DBEntry.TABLE_NAME_TOTALCOUNT+
        " (count_id,"+DBContract.DBEntry.COLUMN_NAME_CARDNUM+") VALUES (1,0)";
public static final String INCREMENT_COUNT2 = "UPDATE " + DBContract.DBEntry.TABLE_NAME_TOTALCOUNT+" SET "+ DBContract.DBEntry.COLUMN_NAME_CARDNUM + "="+ DBContract.DBEntry.COLUMN_NAME_CARDNUM+"+1";

2 个答案:

答案 0 :(得分:1)

虽然这件事对我来说似乎很奇怪,因为你想要增加的值等于你的另一个表中可以通过一个简单的"COUNT"查询得到的记录数,但是在这里你可以实现这个:

首先将您的计数表描述更改为:

    "CREATE TABLE IF NOT EXISTS " + DBContract.DBEntry.TABLE_NAME_COUNT +
        "(_id INTEGER UNIQUE, " + DBContract.DBEntry.COLUMN_NAME_CARDNUM +
              " INTEGER" + ")"

在SqliteOpenHelper的onCreate中将计数器初始化为0:

db.execSQL("INSERT INTO "+ DBContract.DBEntry.TABLE_NAME_COUNT + 
           "(_id," + DBContract.DBEntry.COLUMN_NAME_CARDNUM+") VALUES (1,0)");

为您的班级添加最终字符串:

public static final String INCREMENT_COUNT1 = "INSERT OR IGNORE INTO "+ DBContract.DBEntry.TABLE_NAME_COUNT+
" (_id,"+DBContract.DBEntry.COLUMN_NAME_CARDNUM+") VALUES (1,0)";
public static final String INCREMENT_COUNT2 = "UPDATE " + DBContract.DBEntry.TABLE_NAME_COUNT+" SET "+ DBContract.DBEntry.COLUMN_NAME_CARDNUM + "=" + DBContract.DBEntry.COLUMN_NAME_CARDNUM+"+1";

更改以下功能:

public void insertIntoTableCount() {

    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL(INCREMENT_COUNT1);
    db.execSQL(INCREMENT_COUNT2);
    db.close();
}

onClickSave中执行以下操作:

helper = new DatabaseHelper(CardViewActivity.this);

//for extra caution it is better to add beginTransaction here
helper.insertIntoTableUserInputs(todo,note1,note2,duedate,duetime,timestamp);
helper.insertIntoTableCount();
//add setTransactionSuccessfull() and endTransaction here

答案 1 :(得分:0)

我认为没有意义,一个包含单列的表并定义为主键。

如果您有一个包含主键的表:例如,表名为Student,列为studentid,studenName。 studentid声明为主键和自动增量,那么你只需要在studentName列中插入记录,如下所示:

print("hello world")

此内部查询将是

SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values2 = new ContentValues();
    values2.put("studentName", studentName);

    db.insert("Student", null, values2);

主键和自动递增的值作为null传递,以便sqlite引擎识别它是一个自动增量列,行值自动递增1.