1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
public class DataBaseContact extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "Contact.db";
private static final String CONTACT_NAME = "contact_table";
private static final String COL_1 = "PSEUDO";
private static final String COL_2 = "USER";
public DataBaseContact(@Nullable Context context){
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase dbC) {
dbC.execSQL("create table " + CONTACT_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, PSEUDO TEXT,USER INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + CONTACT_NAME);
onCreate(db);
}
boolean insertPseudo(String pseudo, Integer user) {
SQLiteDatabase dbC = this.getWritableDatabase();
ContentValues contentPseudo = new ContentValues();
contentPseudo.put(COL_1,pseudo);
contentPseudo.put(COL_2,user);
if (!checkPseudoContact(pseudo)) {
long result = dbC.insert(CONTACT_NAME, null, contentPseudo);
if (result == -1) return false;
else return true;
}
else return false;
}
public Cursor viewContact(Integer idUser) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM contact_table WHERE user = '"+idUser+"'", null);
return c;
}
private boolean checkPseudoContact(String pseudo) {
SQLiteDatabase db = this.getReadableDatabase();
String savePseudo = null;
Cursor c = db.rawQuery("SELECT * FROM contact_table WHERE pseudo = '"+pseudo+"'", null);
int pseudoIndex = c.getColumnIndex("PSEUDO");
c.moveToFirst();
if (c.moveToFirst()) {
savePseudo = c.getString(pseudoIndex);
return savePseudo.equals(pseudo);
}
else return false;
}
public Integer deleteData(String pseudo) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(CONTACT_NAME,"PSEUDO = ?",new String[] {pseudo});
}
} |
Partager