SQLite数据库_id整数问题

时间:2015-10-14 22:05:28

标签: android sqlite

我正在DatabaseHelper类中创建一个简单的SQLite数据库,它扩展了SQLiteOpenHelper。该数据库包含两列,NAME和DESCRIPTION。这是在DatabaseHelper类

中的onCreate方法中运行的方法
     @Override
     public void onCreate(SQLiteDatabase db) {
         updateMyDatabase(db, 0, DATABASE_VERSION);
      }

    private void updateMyDatabase(SQLiteDatabase db, int olderVersion, int newerVersion){
    if (olderVersion<1){
        db.execSQL("CREATE TABLE MARK (_id INTEGER PRIMARY KEY AUTOINCREMENT,"
                + "NAME TEXT, "
                + "DESCRIPTION TEXT);");
        insertDrink(db,"latte","Espresso and Steamed Milk");
        insertDrink(db, "cappuccino", "This a Cappuccino");
    }
}


     private static void insertDrink(SQLiteDatabase database, String name, String description){
         ContentValues drinkValues = new ContentValues();
         drinkValues.put("NAME", name);
         drinkValues.put("DESCRIPTION",description);
         database.insert("MARK",null,drinkValues);
}

我的具体问题是关于创建数据库时使用的_id INTEGER PRIMARY KEY。这个数字是从1还是0开始?

这就是我想知道的原因。我有一个简单的ListView有两个项目。我单击第一个项目并将ListView的位置(即0)传递给另一个活动。

 listView.setOnItemClickListener(new ListView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch(position){
                case 0:{
                    Intent i = new Intent(MainActivity.this, Details.class);
                    i.putExtra(Details.EXTRA_DRINKNUM,(int)id);
                    startActivity(i);
                    break;
                }
                case 1:{
                    Intent i = new Intent(MainActivity.this, Details.class);
                    i.putExtra(Details.EXTRA_DRINKNUM,(int)id);
                    startActivity(i);
                    break;
                }
            }
        }
    });

然后我在该Activity中检索此数字并将其放在名为clickNum的变量中。

然后我使用clickNum查询数据库:

        SQLiteOpenHelper database = new DatabaseHelper(this);
        SQLiteDatabase db = database.getReadableDatabase();
        Cursor cursor = db.query("MARK",
                new String[]{"NAME","DESCRIPTION"},
                "_id = ?",
                new String[]{Integer.toString(clickNum)},
                null,null,null);

如果我喝drinkNum + 1,我只能拿出包含数据的第一行(Latte ....)。为什么会这样?不应该第一行_id是0 ???

以下是查询数据库的类(详细信息)的详细信息:

public class Details extends AppCompatActivity {
private TextView name, description;
public static final String EXTRA_DRINKNUM = "DrinkNum";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details);

    name = (TextView)findViewById(R.id.detailsCoffeeName);
    description = (TextView)findViewById(R.id.detailsCoffeeDescription);

    int clickNum = (Integer)getIntent().getExtras().get(EXTRA_DRINKNUM);

    ContentValues values = new ContentValues();
    values.put("DESCRIPTION", "Lateeeee");

    try{
        SQLiteOpenHelper database = new DatabaseHelper(this);
        SQLiteDatabase db = database.getReadableDatabase();
        Cursor cursor = db.query("MARK",
                new String[]{"NAME","DESCRIPTION"},
                "_id = ?",
                new String[]{Integer.toString(clickNum)},
                null,null,null);

            if (cursor.moveToFirst()){
            String nameText = cursor.getString(0);
            String descriptionText = cursor.getString(1);

            name.setText(nameText);
            description.setText(descriptionText);
        }else{
            Log.v("Details", "Nothing Found");
        }
        cursor.close();
        db.close();

    }catch(SQLiteException e){
        e.printStackTrace();
        Toast.makeText(this, "Database Issue", Toast.LENGTH_LONG).show();

    }

1 个答案:

答案 0 :(得分:1)

数据库的自动增量从1. :)

开始