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
| package com.example.kro.easydati.config;
/**
* Created by KRO on 03/12/2014.
*/
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.example.kro.easydati.MainActivity;
import java.util.ArrayList;
import java.util.List;
public class MyDBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "magnetaDB.db";
private static final String TABLE_APPAREILS = "appareils";
public static final String COLUMN_ID_APPAREIL = "id_appareil";
public static final String COLUMN_NUMERO = "numero";
public static final String COLUMN_NOM = "nom";
public static final String COLUMN_ID_MODELE = "id_modele";
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_APPAREILS_TABLE = "CREATE TABLE " +
TABLE_APPAREILS + "(" +
COLUMN_ID_APPAREIL + " INTEGER PRIMARY KEY," +
COLUMN_NUMERO + " TEXT," +
COLUMN_NOM + " TEXT," +
COLUMN_ID_MODELE + " INTEGER"
+ ")";
db.execSQL(CREATE_APPAREILS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_APPAREILS);
onCreate(db);
}
public void addAppareil(Appareil appareil) {
ContentValues values = new ContentValues();
values.put(COLUMN_NUMERO, appareil.getNumero());
values.put(COLUMN_NOM, appareil.getNom());
values.put(COLUMN_ID_MODELE, appareil.getModele());
SQLiteDatabase db = this.getWritableDatabase();
db.insert(TABLE_APPAREILS, null, values);
db.close();
}
public Appareil findAppareil(String appareilnumero) {
String query = "Select * FROM " + TABLE_APPAREILS + " WHERE " + COLUMN_NUMERO + " = \"" + appareilnumero + "\"";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
Appareil appareil = new Appareil();
if (cursor.moveToFirst()) {
cursor.moveToFirst();
appareil.setID(Integer.parseInt(cursor.getString(0)));
appareil.setNumero(cursor.getString(1));
appareil.setNom(cursor.getString(2));
appareil.setModele(Integer.parseInt(cursor.getString(3)));
cursor.close();
} else {
appareil = null;
}
db.close();
return appareil;
}
public boolean deleteAppareil(String appareilnumero) {
boolean result = false;
String query = "Select * FROM " + TABLE_APPAREILS + " WHERE " + COLUMN_NUMERO + " = \"" + appareilnumero + "\"";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
Appareil product = new Appareil();
if (cursor.moveToFirst()) {
product.setID(Integer.parseInt(cursor.getString(0)));
db.delete(TABLE_APPAREILS, COLUMN_ID_APPAREIL + " = ?",
new String[] { String.valueOf(product.getID()) });
cursor.close();
result = true;
}
db.close();
return result;
}
public List<Appareil> getAllAppareil() {
List<Appareil> appareilList = new ArrayList<Appareil>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_APPAREILS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Appareil appareil = new Appareil();
appareil.setID(Integer.parseInt(cursor.getString(0)));
appareil.setNumero(cursor.getString(1));
appareil.setNom(cursor.getString(2));
appareil.setModele(Integer.parseInt(cursor.getString(3)));
String name = cursor.getString(1) +"\n"+ cursor.getString(2);
// MainActivity.ArrayofName.add(name);
// Adding contact to list
appareilList.add(appareil);
} while (cursor.moveToNext());
}
// return contact list
return appareilList;
}
public List<Appareil> getAppareilNumber() {
List<Appareil> numeroList = new ArrayList<Appareil>();
// Select All Query
String selectQuery = "SELECT " + COLUMN_NUMERO + " FROM " + TABLE_APPAREILS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Appareil appareil = new Appareil();
appareil.setNumero(cursor.getString(0));
numeroList.add(appareil);
} while (cursor.moveToNext());
}
// return contact list
return numeroList;
}
} |
Partager