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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
|
package com.example.testcovid;
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.SQLiteDatabaseLockedException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicInteger;
public class Database extends SQLiteOpenHelper {
private static final String DATABASE_NAMES = " wozniak ";
private static final int DATABASE_VERSION = 2;
public static final String TABLE_EVENTS = " events ";
public static final String TABLE_MEDECINS = " medecins ";
public static final String TABLE_PATIENTS = " patients ";
public static final String TABLE_QUIZ_PATIENTS = " quizs ";
public static final String TABLE_RESEAU_MEDECINS = " reseau_medecins ";
public static final String COLUMN_ID_EVENTS = " ID ";
public static final String COLUMN_EVENT_EVENTS = " event ";
public static final String COLUMN_DATE_EVENTS = " date ";
public static final String COLUMN_TIME_EVENTS = " time ";
public static final String COLUMN_MONTH_EVENTS = " month ";
public static final String COLUMN_YEAR_EVENTS = " year ";
public static final String COLUMN_ID_PATIENT_EVENTS = " id_patient ";
public static final String COLUMN_ID_MEDECIN_EVENTS = " id_medecin ";
public static final String COLUMN_ID_MEDECIN = " medecin_id ";
public static final String COLUMN_NOM_MEDECIN = " medecin_nom ";
public static final String COLUMN_PRENOM_MEDECIN = " medecin_prenom ";
public static final String COLUMN_MEDECIN_RPPS = " medecin_rpps ";
public static final String COLUMN_MEDECIN_MAIL = " medecin_mail ";
public static final String COLUMN_MEDECIN_PORTABLE = " medecin_portable ";
public static final String COLUMN_MEDECIN_VILLE = " medecin_ville ";
public static final String COLUMN_MEDECIN_CODE_POSTAL = " medecin_code_postal ";
public static final String COLUMN_MEDECIN_MOT_DE_PASSE = " medecin_mot_de_passe ";
public static final String COLUMN_MEDECIN_CONFIR_MOT_DE_PASSE = " medecin_confir_mot_de_passe ";
public static final String COLUMN_ID_PATIENT = " patient_id ";
public static final String COLUMN_NOM_PATIENT = " patient_nom ";
public static final String COLUMN_PRENOM_PATIENT = " patient_prenom ";
public static final String COLUMN_MAIL_PATIENT = " patient_mail ";
public static final String COLUMN_PORTABLE_PATIENT = " patient_portable ";
public static final String COLUMN_VILLE_PATIENT = " patient_ville ";
public static final String COLUMN_CODE_POSTAL_PATIENT = " patient_code_postal ";
public static final String COLUMN_ID_QUIZ_PATIENT = " id_quiz_patient ";
public static final String COLUMN_DATE_QUIZ_PATIENT = " date_quiz_patient ";
public static final String COLUMN_RESULT_QUIZ_PATIENT = " result_quiz_patient ";
public static final String COLUMN_RESEAU_MEDECINS_ID = " reseau_id ";
public static final String COLUMN_MEDECIN_USER_ID = " medecin_user_id ";
public static final String COLUMN_MEDECIN_ASSOCIATE = " medecin_associate_id ";
private static Database sInstance;
private AtomicInteger mOpenCounter = new AtomicInteger();
public Handler mHandler;
volatile SQLiteDatabase db1;
private static final String CREATE_TABLE_EVENTS =
"CREATE TABLE " + TABLE_EVENTS + "("
+ COLUMN_ID_EVENTS + "INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_EVENT_EVENTS + "TEXT NOT NULL, "
+ COLUMN_DATE_EVENTS + "TEXT NOT NULL, "
+ COLUMN_TIME_EVENTS + "TEXT NOT NULL, "
+ COLUMN_MONTH_EVENTS + "TEXT NOT NULL, "
+ COLUMN_YEAR_EVENTS + "TEXT NOT NULL " + ")";
private static final String CREATE_TABLE_MEDECINS =
"CREATE TABLE " + TABLE_MEDECINS + "("
+ COLUMN_ID_MEDECIN + "INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_NOM_MEDECIN + "TEXT, "
+ COLUMN_PRENOM_MEDECIN + "TEXT, "
+ COLUMN_MEDECIN_RPPS + "TEXT, "
+ COLUMN_MEDECIN_MAIL + "TEXT, "
+ COLUMN_MEDECIN_PORTABLE + "TEXT, "
+ COLUMN_MEDECIN_VILLE + "TEXT, "
+ COLUMN_MEDECIN_CODE_POSTAL + "TEXT, "
+ COLUMN_MEDECIN_MOT_DE_PASSE + "TEXT, "
+ COLUMN_MEDECIN_CONFIR_MOT_DE_PASSE + "TEXT " + ")";
private static final String CREATE_TABLE_PATIENTS =
"CREATE TABLE " + TABLE_PATIENTS + "("
+ COLUMN_ID_PATIENT + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_NOM_PATIENT + " TEXT, "
+ COLUMN_PRENOM_PATIENT + " TEXT, "
+ COLUMN_MAIL_PATIENT + "TEXT, "
+ COLUMN_PORTABLE_PATIENT + "TEXT, "
+ COLUMN_VILLE_PATIENT + "TEXT, "
+ COLUMN_CODE_POSTAL_PATIENT + "TEXT " + ")";
private static final String DROP_TABLE_EVENTS = "DROP TABLE IF EXISTS " + DBStructure.EVENT_TABLE_NAME;
public static synchronized Database getInstance(Context context) {
if (sInstance == null) {
sInstance = new Database(context.getApplicationContext());
}
return sInstance;
}
public Database(Context context) {
super(context, DATABASE_NAMES, null, DATABASE_VERSION);
db1 = this.getReadableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_EVENTS);
db.execSQL(CREATE_TABLE_MEDECINS);
db.execSQL(CREATE_TABLE_PATIENTS);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_EVENTS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEDECINS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PATIENTS);
onCreate(db);
}
public String insertMedecin(String nom, String prenom, String rpps, String mail, String portable, String ville, String code_postal, String mot_de_passe) {
Log.d("nom", nom);
Log.d("prenom", prenom);
Log.d("rpps", rpps);
Log.d("mail", mail);
Log.d("portable", portable);
Log.d("ville", ville);
Log.d("code_postal", code_postal);
Log.d("mot_de_passe", mot_de_passe);
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("medecin_nom", nom);
contentValues.put("medecin_prenom", prenom);
contentValues.put("medecin_rpps", rpps);
contentValues.put("medecin_mail", mail);
contentValues.put("medecin_portable", portable);
contentValues.put("medecin_ville", ville);
contentValues.put("medecin_code_postal", code_postal);
contentValues.put("medecin_mot_de_passe", mot_de_passe);
long result = db.insert(TABLE_MEDECINS, null, contentValues);
Log.d("result", String.valueOf(result));
if (result == -1) {
return "false";
} else {
return "true";
}
}
public Cursor getAllMedecins() {
Log.d("TAG-SQLITE", "MERDE DE MERDE");
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_MEDECINS + ";", null);
while(cursor.moveToNext()) {
Log.d("TAG-SQLITE", cursor.getString(1));
Log.d("TAG-SQLITE", cursor.getString(2));
Log.d("TAG-SQLITE", cursor.getString(3));
Log.d("TAG-SQLITE", cursor.getString(4));
Log.d("TAG-SQLITE", cursor.getString(5));
Log.d("TAG-SQLITE", cursor.getString(6));
Log.d("TAG-SQLITE", cursor.getString(7));
}
Log.d("TAG-SQLITE", "MERDE DE MERDE");
return cursor;
}
public ArrayList<Medecin> getAllPhysicians(Context context) {
ArrayList<Medecin> list = new ArrayList<Medecin>();
SQLiteDatabase db = this.getReadableDatabase();
db.beginTransaction();
String selectQuery = "SELECT * FROM " + TABLE_MEDECINS;
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.getCount() > 0) {
while(cursor.moveToNext()) {
Medecin m = new Medecin();
m.setIdMedecin(cursor.getInt(cursor.getColumnIndex("medecin_id")));
m.setNomMedecin(cursor.getString(cursor.getColumnIndex("medecin_nom")));
m.setPrenomMedecin(cursor.getString(cursor.getColumnIndex("medecin_prenom")));
m.setMailMedecin(cursor.getString(cursor.getColumnIndex("medecin_mail")));
m.setMailMedecin(cursor.getString(cursor.getColumnIndex("medecin_mot_de_passe")));
list.add(m);
}
}
return list;
}
public void SaveEvent(String event, String date, String time, String month, String year, SQLiteDatabase database) {
ContentValues contentValues = new ContentValues();
contentValues.put(DBStructure.EVENT, event);
contentValues.put(DBStructure.DATE, date);
contentValues.put(DBStructure.TIME, time);
contentValues.put(DBStructure.MONTH, month);
contentValues.put(DBStructure.YEAR, year);
database.insert(DBStructure.EVENT_TABLE_NAME, null, contentValues);
String query = "select * from events";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(query, null);
if(cursor.moveToFirst()){
do{
int id = cursor.getInt(0);
String eventE = cursor.getString(cursor.getColumnIndexOrThrow("event"));
String dateE = cursor.getString(cursor.getColumnIndexOrThrow("date"));
String timeE = cursor.getString(cursor.getColumnIndexOrThrow("time"));
String monthE = cursor.getString(cursor.getColumnIndexOrThrow("month"));
String yearE = cursor.getString(cursor.getColumnIndexOrThrow("year"));
}
while (cursor.moveToNext());
}
cursor.close();
}
public Cursor ReadEvents(String date, SQLiteDatabase database) {
String[] Projections = {COLUMN_EVENT_EVENTS, COLUMN_TIME_EVENTS, COLUMN_DATE_EVENTS, COLUMN_MONTH_EVENTS, COLUMN_YEAR_EVENTS};
String Selection = DBStructure.DATE + "=?";
String[] SelectionArgs = {date};
return database.query(DBStructure.EVENT_TABLE_NAME, Projections, Selection, SelectionArgs, null, null, null);
}
public Cursor ReadEventsPerMonth(String month, String year, SQLiteDatabase database) {
String[] Projections = {COLUMN_EVENT_EVENTS, COLUMN_TIME_EVENTS, COLUMN_DATE_EVENTS, COLUMN_MONTH_EVENTS, COLUMN_YEAR_EVENTS};
String Selection = COLUMN_MONTH_EVENTS + "=? and " + COLUMN_YEAR_EVENTS + "=?";
String[] SelectionArgs = {month, year};
return database.query(TABLE_EVENTS, Projections, Selection, SelectionArgs, null, null, null);
}
public void deleteEvent(String event, String date, String time, SQLiteDatabase database) {
String selection = COLUMN_EVENT_EVENTS + "=? and " + COLUMN_DATE_EVENTS + "=? and " + COLUMN_TIME_EVENTS + "=?";
String[] selectionArg = {event, date, time };
database.delete(TABLE_EVENTS, selection, selectionArg);
}
public void updateEvent(String date, String event, String time, SQLiteDatabase database) {
ContentValues contentValues = new ContentValues();
String selection = DBStructure.DATE + "=? and " + DBStructure.EVENT + "=? and " + DBStructure.TIME + "=?";
String[] selectionArg = {date, event, time};
database.update(TABLE_EVENTS, contentValues, selection, selectionArg);
}
public List<EventObjects> getAllFutureEvents() {
Date dateToday = new Date();
List<EventObjects> events = new ArrayList<>();
/*String query = "select * from " + TABLE_EVENTS + "";*/
new Thread(new Runnable() {
@Override
public void run() {
try {
String query1 = "SELECT * FROM " + TABLE_EVENTS;
db1.beginTransaction();
db1.execSQL(query1);
//Cursor cursor = db.rawQuery(query1, null);
//cursor.close();
db1.close();
}
catch(SQLiteDatabaseLockedException e) {
e.printStackTrace();
}
db1.close();
}
}).start();
Looper.prepare();
mHandler = new Handler() {
public void handleMessage(Message msg) {
Log.d("MESSAGE", "String.valueOf(events.get(0).getEvent())");
}
};
Looper.loop();
return events;
}
private Date convertStringToDate(String dateInString) {
DateFormat format = new SimpleDateFormat("MMMM yyyy", Locale.ENGLISH);
Date date = null;
try {
date = format.parse(dateInString);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
public boolean insertPatient(String nom, String prenom, String mail, String portable, String ville, String code_postal) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("patient_nom", nom);
contentValues.put("patient_prenom", prenom);
contentValues.put("patient_mail", mail);
contentValues.put("patient_portable", portable);
contentValues.put("patient_ville", ville);
contentValues.put("patient_code_postal", code_postal);
long result = db.insert(TABLE_PATIENTS, null, contentValues);
if(result == -1) {
return false;
}
else {
return true;
}
}
public ArrayList<Patient> getAllPatients(Context context) {
ArrayList<Patient> list = new ArrayList<Patient>();
SQLiteDatabase db = this.getReadableDatabase();
db.beginTransaction();
String selectQuery = "SELECT * FROM " + TABLE_PATIENTS;
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.getCount() > 0) {
while(cursor.moveToNext()) {
Patient p = new Patient();
p.setIdPatient(cursor.getInt(cursor.getColumnIndex("patient_id")));
p.setNomPatient(cursor.getString(cursor.getColumnIndex("patient_nom")));
p.setPrenomPatient(cursor.getString(cursor.getColumnIndex("patient_prenom")));
p.setMailPatient(cursor.getString(cursor.getColumnIndex("patient_mail")));
p.setPhonePatient(cursor.getString(cursor.getColumnIndex("patient_portable")));
list.add(p);
}
}
return list;
}
public boolean insertNewQuizPatient(String dateQuiz, Integer resultQuiz, Integer idPatient) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("quiz_dateQuiz", dateQuiz);
contentValues.put("quiz_resultQuiz", resultQuiz);
contentValues.put("quiz_idPatient", idPatient);
long result = db.insert(TABLE_QUIZ_PATIENTS, null, contentValues);
if(result == -1) {
return false;
}
else {
return true;
}
}
} |
Partager