· 9 years ago · Oct 11, 2016, 05:10 PM
1package com.example.depmun;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import android.content.ContentValues;
7import android.content.Context;
8import android.database.Cursor;
9import android.database.sqlite.SQLiteDatabase;
10import android.database.sqlite.SQLiteOpenHelper;
11import android.database.sqlite.SQLiteDatabase.CursorFactory;
12
13public class DBHelper extends SQLiteOpenHelper {
14
15 // Sentencia SQL para crear la tabla de Usuarios
16 String sqlDep = "CREATE TABLE Departamentos ( id INTEGER PRIMARY KEY AUTOINCREMENT, nombre TEXT NOT NULL UNIQUE)";
17 String sqlMun = "CREATE TABLE Municipios ( id INTEGER PRIMARY KEY AUTOINCREMENT, nombre TEXT NOT NULL UNIQUE, id_dep INTEGER, FOREIGN KEY(id_dep) REFERENCES Departamentos(id) )";
18 //String sqlSeq = "CREATE TABLE sqlite_sequence(name,seq)";
19
20 public DBHelper(Context contexto) {
21 super(contexto, "DepMun", null, 1);
22 }
23
24 @Override
25 public void onCreate(SQLiteDatabase db) {
26 db.execSQL(sqlDep);
27 db.execSQL(sqlMun);
28 //db.execSQL(sqlSeq);
29 }
30
31 @Override
32 public void onUpgrade(SQLiteDatabase db, int versionAnterior, int versionNueva) {
33 db.execSQL("DROP TABLE IF EXISTS Departamentos");
34 db.execSQL(sqlDep);
35 db.execSQL("DROP TABLE IF EXISTS Municipios");
36 db.execSQL(sqlMun);
37 //db.execSQL("DROP TABLE IF EXISTS sqlite_sequence");
38 //db.execSQL(sqlSeq);
39 }
40
41 public void insertarValorDep(String valor){
42 SQLiteDatabase db = this.getWritableDatabase();
43
44 ContentValues valNom = new ContentValues();
45 valNom.put("nombre", valor);
46
47 db.insert("Departamentos", null, valNom);
48 db.close();
49 }
50
51 public void insertarValorMun(String valor, String Departamento){
52 SQLiteDatabase db = this.getWritableDatabase();
53
54 ContentValues valNom = new ContentValues();
55 valNom.put("nombre", valor);
56 valNom.put("id_dep", Departamento);
57
58 db.insert("Municipios", null, valNom);
59 db.close();
60 }
61
62 public void eliminarValorMun(String valor){
63 SQLiteDatabase db = this.getWritableDatabase();
64
65 db.delete("Municipios", "nombre=?", new String[] { valor });
66 db.close();
67 }
68
69 public List<String> getDep(){
70 List<String> valores = new ArrayList<String>();
71
72 String selectQuery = "SELECT * FROM Departamentos";
73
74 SQLiteDatabase db = this.getReadableDatabase();
75 Cursor cursor = db.rawQuery(selectQuery, null);
76
77 if (cursor.moveToFirst()) {
78 do {
79 valores.add(cursor.getString(1));
80 } while (cursor.moveToNext());
81 }
82
83 cursor.close();
84 db.close();
85
86 return valores;
87 }
88
89 public List<String> getMun(String Departamento){
90 List<String> valores = new ArrayList<String>();
91
92 String selectQuery = "SELECT * FROM Municipios WHERE id_dep = " + Departamento;
93
94 SQLiteDatabase db = this.getReadableDatabase();
95 Cursor cursor = db.rawQuery(selectQuery, null);
96
97 if (cursor.moveToFirst()) {
98 do {
99 valores.add(cursor.getString(1));
100 } while (cursor.moveToNext());
101 }
102
103 cursor.close();
104 db.close();
105
106 return valores;
107 }
108}