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 112 113 114 115 116 117 118 119 120 121 122 123 124
|
package com.panameInc.statstt;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
public class db {
public static final String KEY_ID = "ID";
public static final String KEY_NAME = "Nom";
public static final String KEY_VALUE = "Valeur";
private static final String DATABASE_NAME = "Db_Stat";
private static final String DATABASE_TABLE = "table_point";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " +DATABASE_TABLE+ " (" +
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_VALUE + " INTEGER);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public db(Context c){
ourContext = c;
}
public db open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long insert(Point p){
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, p.getNom());
cv.put(KEY_VALUE, p.getValeur());
return ourDatabase.insert(db.DATABASE_TABLE, null, cv);
}
public void update(int ID, String nom, int valeur) throws SQLException, NullPointerException{
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, nom);
cv.put(KEY_VALUE, valeur);
ourDatabase.update(db.DATABASE_TABLE, cv, KEY_ID + " = " +ID, null);
}
public void delete(int id) throws SQLException, NullPointerException{
ourDatabase.delete(db.DATABASE_TABLE, KEY_ID +" = " +id, null);
}
public void deleteAll() throws SQLException , NullPointerException{
int id = 0;
ourDatabase = ourHelper.getWritableDatabase();
for(id = 0; id <= 55; id++)
ourDatabase.delete(db.DATABASE_TABLE, KEY_ID +" = " +id, null);
}
public Point getData(String nom){
String[] columns = new String[]{KEY_ID, KEY_NAME, KEY_VALUE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_NAME + " LIKE \"" + nom +"\"", null, null, null, null);
int iRow = c.getColumnIndex(KEY_ID);
int iName = c.getColumnIndex(KEY_NAME);
int iValue = c.getColumnIndex(KEY_VALUE);
if (c.getCount() == 0)
return null;
//Sinon on se place sur le premier élément
c.moveToFirst();
//On créé un livre
Point p = new Point();
//on lui affecte toutes les infos grâce aux infos contenues dans le Cursor
p.setId(c.getInt(iRow));
p.setNom(c.getString(iName));
p.setValeur(c.getInt(iValue));
//On ferme le cursor
c.close();
//On retourne le livre
return p;
}
} |
Partager