/** * 查找单个的联系人,返回的是一个字符串 * * @param name 要查找用户的姓名 * @return */ public String find(String name) { SQLiteDatabase db = helper.getWritableDatabase(); // 得到数据库 // Cursor cursor = db.rawQuery("select *from person where name=?;", new String[]{name}); // //得到查询返回的结果集 Cursor cursor = db.query("mycard", null, "name=?", new String[] {name}, null, null, null); if (cursor.moveToNext()) { String name1 = cursor.getString(cursor.getColumnIndex("name")); String number1 = cursor.getString(cursor.getColumnIndex("number")); return name1 + ":" + number1; } else { return "该联系人不存在!"; } // boolean result = cursor.moveToNext(); //游标移向查找的数据,存在返回true,否则反之。 // // cursor.close(); // db.close(); // return result; }
/** 添加一条数据 */ public void add( String cardName, String name, String cellNumber, String phoneNumber, String email, String qq, String address, String company, String remark) { SQLiteDatabase db = helper.getWritableDatabase(); // 得到数据库 // db.execSQL("insert into person (name,number) values(?,?);", new Object[]{name,number}); ContentValues values = new ContentValues(); values.put("cardName", cardName); values.put("name", name); values.put("cellNumber", cellNumber); values.put("phoneNumber", phoneNumber); values.put("email", email); values.put("qq", qq); values.put("address", address); values.put("company", company); values.put("remark", remark); db.insert("mycard", null, values); db.close(); }
/** * 更新数据库中数据的属性,忽略联系人的name, * * @param cardName * @param cellNumber * @param phoneNumber * @param email * @param qq * @param address * @param company * @param remark * @return */ public int update( String cardName, String name, String cellNumber, String phoneNumber, String email, String qq, String address, String company, String remark) { SQLiteDatabase db = helper.getWritableDatabase(); // 得到数据库 // db.execSQL("update person set number=? where name=?;", new Object[]{number,name}); ContentValues values = new ContentValues(); values.put("cardName", cardName); values.put("cellNumber", cellNumber); values.put("phoneNumber", phoneNumber); values.put("email", email); values.put("qq", qq); values.put("address", address); values.put("company", company); values.put("remark", remark); int numberAffected = db.update("mycard", values, "name=?", new String[] {name}); // 得到修改后影响的行数 db.close(); return numberAffected; }
/** * 查询所有数据,返回一个List集合 * * @return */ @SuppressLint("NewApi") public ArrayList<ContactBean> findAll() { ArrayList<ContactBean> list = new ArrayList<ContactBean>(); ContactBean contact; SQLiteDatabase db = helper.getWritableDatabase(); // 得到数据库 // Cursor cursor = db.rawQuery("select *from person", null); Cursor cursor = db.query( "mycard", new String[] { "id", "cardName", "name", "cellNumber", "phoneNumber", "email", "qq", "address", "company", "remark" }, null, null, null, null, null, null); while (cursor.moveToNext()) { // int id = cursor.getInt(cursor.getColumnIndex("id")); String cardName = cursor.getString(cursor.getColumnIndex("cardName")); String name = cursor.getString(cursor.getColumnIndex("name")); String cellNumber = cursor.getString(cursor.getColumnIndex("cellNumber")); String phoneNumber = cursor.getString(cursor.getColumnIndex("phoneNumber")); String email = cursor.getString(cursor.getColumnIndex("email")); String qq = cursor.getString(cursor.getColumnIndex("qq")); String address = cursor.getString(cursor.getColumnIndex("address")); String company = cursor.getString(cursor.getColumnIndex("company")); String remark = cursor.getString(cursor.getColumnIndex("remark")); contact = new ContactBean( cardName, name, cellNumber, phoneNumber, email, qq, address, company, remark); list.add(contact); } cursor.close(); db.close(); return list; }
/** * 删除一条数据 * * @param name 要删除人的名字 */ public void delete(String name) { SQLiteDatabase db = helper.getWritableDatabase(); // 得到数据库 // db.execSQL("delete from person where name=?;", new Object[]{name}); db.delete("mycard", "name=?", new String[] {name}); db.close(); }