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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
package com.example.tryy;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MaBaseTrain extends SQLiteOpenHelper {
// Logcat tag
private static final String LOG = "MaBaseTrain";
private static final int VERSION_BDD=1;
private static final String NOM_BDD = "train.db";
//les attributs de table utilisateur:
private static final String TABLE_UTILISATEUR = "table_utilisateur";
private static final String COL_IDUSER = "IDUser";
private static final String COL_EMAIL = "Email";
private static final String COL_MOTDEPASSE = "MotDePasse";
private static final String COL_CIN = "Cin";
//les attributs de table Voyage
private static final String TABLE_Voyage = "table_Voyage";
private static final String COL_IDVOYAGE = "IDVoyage";
private static final String COL_GAREDEPT = "GareDept";
private static final String COL_GAREARRIV = "GareArriv";
private static final String COL_DATE = "DateV";
private static final String COL_HeuREV="HeureV";
//creer la table utilisateur
private static final String CREATE_USER = "CREATE TABLE " + TABLE_UTILISATEUR + " ("
+ COL_IDUSER + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_EMAIL + " TEXT NOT NULL, "
+ COL_MOTDEPASSE + " TEXT NOT NULL, "+ COL_CIN + " INTEGER NOT NULL);";
//creer la table voyage
private static final String CREATE_VOY = "CREATE TABLE " + TABLE_Voyage + " ("
+ COL_IDVOYAGE + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_GAREDEPT + " TEXT NOT NULL, "
+ COL_GAREARRIV + " TEXT NOT NULL, "+ COL_DATE + " TEXT NOT NULL, "+ COL_HeuREV + " TEXT NOT NULL);";
public MaBaseTrain(Context context) {
super(context, NOM_BDD, null, VERSION_BDD);
}
@Override
public void onCreate(SQLiteDatabase db) {
//on créé la table à partir de la requête écrite dans la variable CREATE_BDD
db.execSQL(CREATE_USER);
//creer la table voyage
db.execSQL(CREATE_VOY);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//On peut fait ce qu'on veut ici moi j'ai décidé de supprimer la table et de la recréer
//comme ça lorsque je change la version les id repartent de 0
db.execSQL("DROP TABLE " + TABLE_UTILISATEUR + ";");
db.execSQL("DROP TABLE " + TABLE_Voyage + ";");
onCreate(db);
}
// ------------------------ "Utilisateur" table methods ----------------//
/*
* Creating a user
*/
public long createUser(Utilisateur utilisateur/*, long[] tag_ids */) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL_EMAIL, utilisateur.getEmail());
values.put(COL_MOTDEPASSE, utilisateur.getMotDePasse());
values.put(COL_CIN, utilisateur.getCin());
// insert row
long user_id = db.insert(TABLE_UTILISATEUR, null, values);
// insert tag_ids methode pour association entre 2 tables
/*for (long tag_id : tag_ids) {
createTodoTag(todo_id, tag_id);
}*/
return user_id;
}
/*
* get utilisateur by id
*/
public Utilisateur getUser(long user_id) {
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_UTILISATEUR + " WHERE "
+ COL_IDUSER + " = " + user_id;
Log.e(LOG, selectQuery);
Cursor c = db.rawQuery(selectQuery, null);
if (c != null)
c.moveToFirst();
Utilisateur u = new Utilisateur();
u.setIdUser(c.getInt(0));
u.setEmail(c.getString(1));
u.setMotDePasse(c.getString(2));
u.setCin(c.getInt(3));
return u;
}
/**
* getting all user
* */
public List<Utilisateur> getAllUser() {
List<Utilisateur> user = new ArrayList<Utilisateur>();
String selectQuery = "SELECT * FROM " + TABLE_UTILISATEUR;
Log.e(LOG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
Utilisateur u = new Utilisateur();
u.setIdUser(c.getInt(0));
u.setEmail(c.getString(1));
u.setMotDePasse(c.getString(2));
u.setCin(c.getInt(3));
// adding user to list
user.add(u);
} while (c.moveToNext());
}
return user;
}
// utliser pour l'association entres 2 tables
/**
* getting all todos under single tag
* */
/* public List<Todo> getAllToDosByTag(String tag_name) {
List<Todo> todos = new ArrayList<Todo>();
String selectQuery = "SELECT * FROM " + TABLE_TODO + " td, "
+ TABLE_TAG + " tg, " + TABLE_TODO_TAG + " tt WHERE tg."
+ KEY_TAG_NAME + " = '" + tag_name + "'" + " AND tg." + KEY_ID
+ " = " + "tt." + KEY_TAG_ID + " AND td." + KEY_ID + " = "
+ "tt." + KEY_TODO_ID;
Log.e(LOG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
Todo td = new Todo();
td.setId(c.getInt((c.getColumnIndex(KEY_ID))));
td.setNote((c.getString(c.getColumnIndex(KEY_TODO))));
td.setCreatedAt(c.getString(c.getColumnIndex(KEY_CREATED_AT)));
// adding to todo list
todos.add(td);
} while (c.moveToNext());
}
return todos;
}*/
/*
* getting User count
*/
public int getUserCount() {
String countQuery = "SELECT * FROM " + TABLE_UTILISATEUR;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int count = cursor.getCount();
cursor.close();
// return count
return count;
}
/*
* Updating a User
*/
public int updateUser(Utilisateur utilisateur) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL_EMAIL, utilisateur.getEmail());
values.put(COL_MOTDEPASSE, utilisateur.getMotDePasse());
values.put(COL_CIN, utilisateur.getCin());
// updating row
return db.update(TABLE_UTILISATEUR, values, COL_IDUSER + " = ?",
new String[] { String.valueOf(utilisateur.getIdUser()) });
}
/*
* Deleting a User
*/
public void deleteUser(long user_id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_UTILISATEUR, COL_IDUSER + " = ?",
new String[] { String.valueOf(user_id) });
}
// ------------------------ "Voyage" table methods ----------------//
/*
* Creating voyage
*/
public long createVoyage(VoyageBase voyage) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL_GAREDEPT, voyage.getGareDep());
values.put(COL_GAREARRIV, voyage.getGareArriv());
values.put(COL_DATE, voyage.getDateV());
values.put(COL_HeuREV, voyage.getHeureV());
// insert row
long voy_id = db.insert(TABLE_Voyage, null, values);
return voy_id;
}
/**
* getting all voyage
* */
public List<VoyageBase> getAllVoyage() {
List<VoyageBase> voyage = new ArrayList<VoyageBase>();
String selectQuery = "SELECT * FROM " + TABLE_Voyage;
Log.e(LOG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
VoyageBase v=new VoyageBase();
v.setIdVoyage(c.getInt(0));
v.setGareDep(c.getString(1));
v.setGareArriv(c.getString(2));
v.setDateV(c.getString(3));
v.setHeureV(c.getString(4));
// adding to voyage list
voyage.add(v);
} while (c.moveToNext());
}
return voyage;
}
/*
* Updating voyage
*/
public int updateVoyage(VoyageBase voyage) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL_GAREDEPT, voyage.getGareDep());
values.put(COL_GAREARRIV, voyage.getGareArriv());
values.put(COL_DATE, voyage.getDateV());
values.put(COL_HeuREV, voyage.getHeureV());
// updating row
return db.update(TABLE_Voyage, values, COL_IDVOYAGE + " = ?",
new String[] { String.valueOf(voyage.getIdVoyage()) });
}
/*
* Deleting Voyage
*/
public void deleteVoyage(long voy_id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_Voyage, COL_IDVOYAGE + " = ?",
new String[] { String.valueOf(voy_id) });
}
/*
* Deleting voyage avec association de 2 tables
*/
/*public void deleteTag(VoyageBase voyage , boolean should_delete_all_tag_todos) {
SQLiteDatabase db = this.getWritableDatabase();
// before deleting tag
// check if todos under this tag should also be deleted
if (should_delete_all_tag_todos) {
// get all todos under this tag
List<Todo> allTagToDos = getAllToDosByTag(tag.getTagName());
// delete all todos
for (Todo todo : allTagToDos) {
// delete todo
deleteToDo(todo.getId());
}
}
// now delete the tag
db.delete(TABLE_TAG, KEY_ID + " = ?",
new String[] { String.valueOf(tag.getId()) });
}*/
} |
Partager