使用整数子句查询数据库?

时间:2016-06-11 20:21:42

标签: android mysql sql sqlite

我怎么能用整数子句查询数据库。我想要使用整数子句删除查询。(我想要这样: - “删除table_name where name = myname和id = integer value”)我对数据库查询知之甚少请帮忙我

这是我的数据库:

public class ContactDatabase extends SQLiteOpenHelper {
    SQLiteDatabase db;
    public static final String DATABASE_NAME="totalContact.db";
    public static final  String TABLE_NAME="mecontact";
    public static final  String NAME="name";
    public static final  String PHONE="phone";


    public ContactDatabase(Context context) {
        super(context, DATABASE_NAME, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            db.execSQL("create table mecontact" +
                    "(_id integer primary key autoincrement, name text, phone text)");
        }catch(android.database.SQLException e){
                System.out.println("table create nhi ho rha");
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS mecontact");
        onCreate(db);
    }

    public void insertContact(String nam,String mob){

        db=this.getWritableDatabase();
        ContentValues contentValues=new ContentValues();

        contentValues.put(NAME,nam);
        contentValues.put(PHONE,mob);

        db.insert(TABLE_NAME, null, contentValues);
        db.close();
    }

    public void deleteContact(String d,int pos){
        db=this.getWritableDatabase();
        db.delete(TABLE_NAME,"name='"+d+"' AND /*what is for _id column*/);
    }
}

1 个答案:

答案 0 :(得分:0)

对于整数值,只需将它们放入SQL字符串即可。 (对于字符串,您应该使用参数。)

db.delete(TABLE_NAME, "name = ? AND _id = " + pos,
          new String[]{ d });