SQLite的查询停止递增10 [英] SQLite Query Stops Incrementing at 10

查看:156
本文介绍了SQLite的查询停止递增10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android SQLite数据库插入查询。我需要保持这种自动递增为每个新记录的键ID字段。因此,作为安装到我的INSERT命令的一部分,我觉得在这方面的最大值,然后加一。事实证明,我的code停在10递增一切是罚款的第9插入,但我没有得到比10更高的我在做什么错了?谢谢!

在这里,code:

 公共布尔createSubcategory(字符串subcategoryName){    接下来INT;
    ContentValues​​ newValues​​ =新ContentValues​​();
    SQLiteDatabase分贝= myDBOpenHelper.getWritableDatabase();    / *获得在子类表中的最大值 - 这是递增数
     *和指定为subcategory_id为我们的新的子类别记录* /
    光标= db.query(SUBCATEGORIES_TABLE,新的String [] {MAX(+ SUBCATEGORY_ID_COLUMN +)作为COL},NULL,NULL,NULL,NULL,NULL);    / *插入一行到小类表* /
    如果(cursor.getCount()大于0){
        cursor.moveToFirst();
        接下来= cursor.getInt(cursor.getColumnIndex(COL))+ 1;
    }其他{下一= 1;}    newValues​​.put(SUBCATEGORY_ID_COLUMN,旁边);
    newValues​​.put(SUBCATEGORY_COLUMN,subcategoryName);    db.insert(SUBCATEGORIES_TABLE,空,newValues​​);
    cursor.close();    通信System.err.println(新子记录创建:ID:+ +旁边名称:+ subcategoryName +类别ID:+的categoryID);    返回true;
}

下面是我如何把这个从MainActivity:

 私人无效createSubcategories(MYDB DB){
    db.createSubcategory(爵士);
    db.createSubcategory(乐团);
    db.createSubcategory(岩石);
    db.createSubcategory(品酒);
    db.createSubcategory(讲座);
    db.createSubcategory(新书签售);
    db.createSubcategory(篮球);
    db.createSubcategory(棒球);
    db.createSubcategory(足球);
    db.createSubcategory(曲棍球);
    db.createSubcategory(其他);
    db.createSubcategory(向上计数器测试);
}

这是我得到的输出。见subcategory_id如何停止在10?


  

新的子类别记录创建:ID:1名称:爵士


  
  

新的子类别记录创建:ID:2名:乐团


  
  

新的子类别记录创建:ID:3名称:Rock


  
  

新的子类别记录创建:ID:4名:品酒


  
  

新的子类别记录创建:ID:5名:讲座


  
  

新的子类别记录创建:ID:6名称:新书签售


  
  

新的子类别记录创建:ID:7名:篮球


  
  

新的子类别记录创建:ID:8名:棒球


  
  

新的子类别记录创建:ID:9产品名称:足球


  
  

新的子类别记录创建:ID:10姓名:曲棍球


  
  

新的子类别记录创建:ID:10姓名:其他


  
  

新的子类别记录创建:ID:10姓名:向上计数器测试



解决方案

这是非常有可能的是搜索使用字典序做,自9,在ASCII-betical订单10后到来的时候,您的查询始终返回9和明年将永远是10。

是您的ID字符串?

I have an insert query in an Android SQLite database. I need to keep a key id field that autoincrements for each new record. So as part of the setup to my insert command, I find the max value in this field and then increment it. It turns out that my code stops incrementing at 10. Everything is fine for the first 9 inserts, but I don't get higher than 10. What am I doing wrong??? Thanks!

Here the code:

public boolean createSubcategory(String subcategoryName) {

    int next;
    ContentValues newValues = new ContentValues();
    SQLiteDatabase db = myDBOpenHelper.getWritableDatabase();

    /* get the maximum value in the sub category table - this is the number to increment
     * and assign as the subcategory_id for our new subcategory record */
    cursor = db.query(SUBCATEGORIES_TABLE, new String [] {"MAX("+SUBCATEGORY_ID_COLUMN+") AS COL"}, null, null, null, null, null);

    /* Insert a row into the Subcategories table */
    if ( cursor.getCount() > 0 ) {
        cursor.moveToFirst();
        next = cursor.getInt(cursor.getColumnIndex("COL")) + 1;
    } else {next = 1;}

    newValues.put(SUBCATEGORY_ID_COLUMN, next);
    newValues.put(SUBCATEGORY_COLUMN, subcategoryName);

    db.insert(SUBCATEGORIES_TABLE, null, newValues);
    cursor.close();     

    System.err.println("New subcategory record created: ID: " + next + " Name: " + subcategoryName + "  Category ID: " + categoryID);

    return true;
}   

Here is how I call this from MainActivity:

private void createSubcategories(myDB db) {
    db.createSubcategory("Jazz");
    db.createSubcategory("Orchestra");
    db.createSubcategory("Rock");
    db.createSubcategory("Wine Tasting");
    db.createSubcategory("Lectures");
    db.createSubcategory("Book Signings");
    db.createSubcategory("Basketball");
    db.createSubcategory("Baseball");
    db.createSubcategory("Football");
    db.createSubcategory("Hockey");
    db.createSubcategory("Other");
    db.createSubcategory("Incrementer Test");
}

And this is the output that I get. See how the subcategory_id stops at 10?

New subcategory record created: ID: 1 Name: Jazz

New subcategory record created: ID: 2 Name: Orchestra

New subcategory record created: ID: 3 Name: Rock

New subcategory record created: ID: 4 Name: Wine Tasting

New subcategory record created: ID: 5 Name: Lectures

New subcategory record created: ID: 6 Name: Book Signings

New subcategory record created: ID: 7 Name: Basketball

New subcategory record created: ID: 8 Name: Baseball

New subcategory record created: ID: 9 Name: Football

New subcategory record created: ID: 10 Name: Hockey

New subcategory record created: ID: 10 Name: Other

New subcategory record created: ID: 10 Name: Incrementer test

解决方案

It's very likely that the search is made using lexicographical order, and since "9" comes after "10" in "ASCII-betical" order, your query always return 9 and next will always be 10.

Is your ID a string?

这篇关于SQLite的查询停止递增10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆