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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "fiche";
public static final String CONT_TABLE_NAME = "tablename";
public static final String CONT_COLUMN_ID = "id";
public static final String NAME = "name";
public static final String FIRST_NAME = "first";
public static final String AGE = "age";
public static final String ADRESS = "address";
private static DBHelper sInstance;
public static final String CREATE_DB = "CREATE TABLE " + DATABASE_NAME + " (" +
CONT_COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
NAME + " TEXT, " + FIRST_NAME + " TEXT, " +
AGE + " REAL, " + ADRESS + " TEXT);";
public static String RESET_TO_0 = "UPDATE SQLITE_SEQUENCE SET seq = 0 WHERE name = \"fiche\"";
private HashMap hp;
private SQLiteDatabase db;
public DBHelper(Context context)
{
super(context, DATABASE_NAME , null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_DB);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db = this.getWritableDatabase();
db.execSQL("DROP TABLE IF EXISTS tablename");
onCreate(db);
}
public void reset() {
SQLiteDatabase db = this.getReadableDatabase();
db.execSQL(RESET_TO_0);
}
public boolean insertContact (String name, String first, String address, int age)
{
SQLiteDatabase db = this.getWritableDatabase(); //ouverture lecture/ecriture de la db
ContentValues contentValues = new ContentValues();// pour récup une sorte d'array de valeurs
contentValues.put(NAME, name);
contentValues.put(FIRST_NAME, first);
contentValues.put(ADRESS, address);
contentValues.put(AGE, age);
db.insert("fiche", null, contentValues);
return true;
}
public Cursor getData(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from fiche where id="+ id +"", null );
return res;
}
public int numberOfRows(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, CONT_TABLE_NAME);
return numRows;
}
public boolean updateContact (Integer id, String name, String first_name, String address, Integer age)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("first", first_name);
contentValues.put("address", address);
contentValues.put("age", age);
db.update("fiche", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
return true;
}
public Integer deleteContact (Integer id)
{
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("fiche",
"id = ? ",
new String[] { Integer.toString(id) });
}
public Cursor getFICHE() {
db = this.getWritableDatabase();
return db.rawQuery("SELECT * FROM " + "fiche", null);
}
public DBHelper rm_all(DBHelper db) {
Cursor strt = db.getFICHE();
if (strt.moveToFirst())
{
int i = strt.getInt(strt.getColumnIndex(DBHelper.CONT_COLUMN_ID));
while (strt.moveToNext()) {
db.deleteContact(i);
i++;
}
db.deleteContact(i);
}
return db;
} |
Partager