Advertisement
Guest User

Untitled

a guest
Oct 10th, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.13 KB | None | 0 0
  1. import android.annotation.SuppressLint;
  2. import android.content.Context;
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.database.sqlite.SQLiteOpenHelper;
  6. import android.text.format.Time;
  7. import android.util.Log;
  8. import android.database.SQLException;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.io.OutputStream;
  13.  
  14. /**
  15. * Created by mikesaurio on 19/09/15.
  16. */
  17. public class DBHelper extends SQLiteOpenHelper {
  18. /** Path donde se encontrara alojada la BD en el tel�fono **/
  19. private static String DB_PATH = "";
  20. /** Nombre de la base de datos **/
  21. private final static String DB_NAME = "infraccion";
  22.  
  23. private SQLiteDatabase myDataBase;
  24. private final Context myContext;
  25.  
  26. /** Constructor **/
  27. @SuppressLint("SdCardPath")
  28. public DBHelper(Context context) {
  29. super(context, DB_NAME, null, 1);
  30. this.myContext = context;
  31.  
  32. DB_PATH = "/data/data/" + myContext.getPackageName() + "/databases/";
  33. }
  34.  
  35.  
  36. /**
  37. * Comprueba si ya existe nuestra base de datos
  38. *
  39. * @return true si ya existe, false si no
  40. **/
  41. private boolean checkDataBase() {
  42. SQLiteDatabase checkDB = null;
  43.  
  44. try {
  45. String myPath = DB_PATH + DB_NAME;
  46. checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
  47.  
  48.  
  49. } catch (Exception e) {
  50. Log.i("Base de datos", "falla en checkDataBAse");
  51. }
  52. if (checkDB != null) {
  53. checkDB.close();
  54. }
  55. return (checkDB!= null ? true : false);
  56. }
  57.  
  58. /**
  59. * Crea una base de datos vac�a y escribe en ella nuestra propia Base de
  60. * Datos
  61. **/
  62. public void createDataBase(Context contexto) throws IOException {
  63.  
  64. /** Comprueba si ya existe la base de datos **/
  65. boolean dbExist = checkDataBase();
  66.  
  67. if (dbExist) {
  68. /** Si existe la base de datos no hace nada **/
  69. } else {
  70. /**
  71. * Si no existe se llama a este metodo que crea una nueva base de
  72. * datos en la ruta por defecto
  73. **/
  74. this.getReadableDatabase();
  75. try {
  76. /**
  77. * Copia nuestra database.sqlite en la nueva base de datos
  78. * creada
  79. **/
  80. copyDataBase();
  81. } catch (IOException e) {
  82. throw new Error("Error copiando la Base de Datos");
  83. }
  84. }
  85. }
  86.  
  87. /**
  88. * Copia nuestra base de datos sqlite de la carpeta assets a nuestra nueva
  89. * Base de Datos
  90. **/
  91. private void copyDataBase() throws IOException {
  92.  
  93. /** Abre nuestra base de datos del fichero **/
  94. InputStream myInput = myContext.getAssets().open(DB_NAME);
  95.  
  96. /** La direcci�n de nuestra nueva Base de Datos **/
  97. String outFileName = DB_PATH + DB_NAME;
  98.  
  99. /** Abre la nueva Base de Datos **/
  100. OutputStream myOutput = new FileOutputStream(outFileName);
  101.  
  102. /** Transfiere bytes desde nuestro archivo a la nueva base de datos **/
  103. byte[] buffer = new byte[1024];
  104. int length;
  105. while ((length = myInput.read(buffer)) > 0) {
  106. myOutput.write(buffer, 0, length);
  107. }
  108.  
  109. /** Cierra los stream **/
  110. myOutput.flush();
  111. myOutput.close();
  112. myInput.close();
  113. }
  114.  
  115. /**
  116. * Abre la base de datos
  117. *
  118. * @throws SQLException
  119. **/
  120. public void openDataBase() throws SQLException {
  121. String myPath = DB_PATH + DB_NAME;
  122. myDataBase = SQLiteDatabase.openDatabase(myPath, null,
  123. SQLiteDatabase.OPEN_READWRITE);
  124. }
  125.  
  126. @Override
  127. public synchronized void close() {
  128. if (myDataBase != null)
  129. myDataBase.close();
  130. super.close();
  131. }
  132.  
  133. @Override
  134. public void onCreate(SQLiteDatabase db) {}
  135.  
  136. @Override
  137. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
  138.  
  139.  
  140. public SQLiteDatabase loadDataBase(Context context, DBHelper helper) throws IOException {
  141. helper.createDataBase(context);
  142. SQLiteDatabase db = helper.getWritableDatabase();
  143. return db;
  144. }
  145.  
  146. /**
  147. * inserta en la BDLite la informacion de un viaje
  148. * @param bd
  149. * @param placa
  150. * @return
  151. */
  152. public boolean setCops(SQLiteDatabase bd,String placa){
  153. Time now = new Time();
  154. now.setToNow();
  155. String date = Long.toString(now.toMillis(false));
  156. try{
  157. if(deleteCops(bd)){
  158. bd.execSQL("insert into cops (cops_json,cops_date) values ('" + placa + "','" + date + "');");
  159. Log.d("***********","Insertando Cops en base de datos");
  160. return true;
  161. }
  162. Log.d("*****FALLA******","No se insertó Cops en base de datos");
  163. return false;
  164. }catch(Exception e){
  165. Log.d("******FALLA****", "Insertando Cops en base de datos");
  166. e.printStackTrace();
  167. return false;
  168. }
  169. }
  170.  
  171.  
  172. public boolean deleteCops(SQLiteDatabase bd){
  173. try{
  174. bd.execSQL("delete from cops");
  175. return true;
  176. }catch(Exception e){
  177. return false;
  178. }
  179. }
  180.  
  181. /**
  182. * Regresa el Json de policias
  183. * @param bd
  184. * @return
  185. */
  186. public String getPolicias(SQLiteDatabase bd){
  187. Cursor c = null;
  188. String json = null;
  189. c = bd.rawQuery("select * from cops", null);
  190. if(c!=null && c.getCount()>0){
  191. c.moveToFirst();
  192. json = c.getString(c.getColumnIndex("cops_json"));
  193. }
  194. c.close();
  195. return json;
  196. }
  197.  
  198.  
  199.  
  200. /**
  201. * inserta en la BDLite la informacion de un viaje
  202. * @param bd
  203. * @param placa
  204. * @return
  205. */
  206. public boolean setInfractions(SQLiteDatabase bd,String placa){
  207. Time now = new Time();
  208. now.setToNow();
  209. String date = Long.toString(now.toMillis(false));
  210. try{
  211. if(deleteInfractions(bd)){
  212. bd.execSQL("insert into infractions (infractions_json,infractions_date) values ('" + placa + "','" + date + "');");
  213. Log.d("***********","Insertando infractions en base de datos");
  214. return true;
  215. }
  216. Log.d("*****FALLA******","No se insertó infractions en base de datos");
  217. return false;
  218. }catch(Exception e){
  219. Log.d("******FALLA****", "Insertando infractions en base de datos");
  220. e.printStackTrace();
  221. return false;
  222. }
  223. }
  224.  
  225.  
  226. public boolean deleteInfractions(SQLiteDatabase bd){
  227. try{
  228. bd.execSQL("delete from infractions");
  229. return true;
  230. }catch(Exception e){
  231. return false;
  232. }
  233. }
  234.  
  235. /**
  236. * Regresa el Json de policias
  237. * @param bd
  238. * @return
  239. */
  240. public String getInfractions(SQLiteDatabase bd){
  241. Cursor c = null;
  242. String json = null;
  243. c = bd.rawQuery("select * from infractions", null);
  244. if(c!=null && c.getCount()>0){
  245. c.moveToFirst();
  246. json = c.getString(c.getColumnIndex("infractions_json"));
  247. }
  248. c.close();
  249. return json;
  250. }
  251.  
  252.  
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement