SHARE
TWEET

Untitled

a guest Jul 19th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.taller_final;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.SQLException;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.database.sqlite.SQLiteOpenHelper;
  9.  
  10. public class DB {
  11.     //TABLA USUARIOS
  12. public static final String KEY_ROWID = "_id";
  13. public static final String KEY_NOMBRE = "_nombreusuario";
  14. public static final String KEY_PASS = "_pass";
  15.  
  16. //TABLA RECETAS
  17. public static final String KEY_RECETA = "_idR";
  18. public static final String KEY_NOMBRER = "_nombreR";
  19. public static String DATABASE_TABLE_R;
  20.  
  21. public final String DATABASE_TABLE_I="Ingredientes";
  22.  
  23. private final String DATABASE_NAME="BD";
  24. private final String DATABASE_TABLE="usuarios";
  25. private final int DATABASE_VERSION = 1;
  26.  
  27.  
  28. public String[] estructura(){
  29.     String[] recetas = new String[6];
  30.     recetas[0] = "CREATE TABLE Recetas(id INTEGER, NOMBRE TEXT)";
  31.     recetas[1] = "INSERT INTO Recetas VALUES(1, 'PANTRUCAS')";
  32.     recetas[2] = "INSERT INTO Recetas VALUES(2, 'TALLARINES CON CARNE
  33. MOLIDA')";
  34.     recetas[3] = "INSERT INTO Recetas VALUES(3, 'CAZUELA')";
  35.     return recetas;
  36. }
  37.  
  38.  
  39. private Context ourContext;
  40. private DBHelper ourHelper;
  41. private SQLiteDatabase ourDatabase;
  42.  
  43. public DB(Context ourContext) {
  44.  
  45.     this.ourContext = ourContext;
  46. }
  47.  
  48.  
  49. public DB open() throws SQLException {
  50.     ourHelper = new DBHelper(ourContext);
  51.     ourDatabase = ourHelper.getWritableDatabase();
  52.  
  53.     return  this;
  54. }
  55.  
  56. public void close(){
  57.     ourHelper.close();
  58. }
  59.  
  60.  
  61.  
  62. public String getUsuarios(){
  63.     String [] columns = new String[] {KEY_ROWID, KEY_NOMBRE, KEY_PASS};
  64.     Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null,
  65.             null, null, null, null);
  66.     String result = "";
  67.  
  68.     int iRowID = c.getColumnIndex(KEY_ROWID);
  69.     int iName = c.getColumnIndex(KEY_NOMBRE);
  70.     int iPass = c.getColumnIndex(KEY_PASS);
  71.  
  72.     for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
  73.         String id = c.getString(iRowID);
  74.         String name = c.getString(iName);
  75.         String pass = c.getString(iPass);
  76.  
  77.         result = result + id + ": " + name + " " + pass + "n";
  78.     }
  79.     c.close();
  80.     return result;
  81. }
  82.  
  83. public long guardarUsuarios(String nombreUsuario, String pass){
  84.     ContentValues cv = new ContentValues();
  85.     cv.put(KEY_NOMBRE, nombreUsuario);
  86.     cv.put(KEY_PASS, pass);
  87.  
  88.     return ourDatabase.insert(DATABASE_TABLE, null, cv);
  89.  
  90. }
  91.  
  92.  
  93. public static class DBHelper extends SQLiteOpenHelper {
  94.  
  95.     private static final String DATABASE_NAME = "DB";
  96.     private static final int DATABASE_VERSION = 1;
  97.     private static Cursor rawQuery;
  98.  
  99.     private String DATABASE_TABLE = "usuarios";
  100.     private String DATABASE_RECETAS = "recetas";
  101.     private String DATABASE_INGREDIENTES = "ingredientes";
  102.  
  103.     //USUARIO
  104.     public static final String KEY_ROWID = "_id";
  105.     public static final String KEY_NOMBRE = "_nombreusuario";
  106.     public static final String KEY_PASS = "_pass";
  107.  
  108.     //RECETAS
  109.     public String[] estructura(){
  110.         String[] recetas = new String[6];
  111.         recetas[0] = "CREATE TABLE Recetas(id_recetas INTEGER, NOMBRE_RECETA
  112.     TEXT)";
  113.         recetas[1] = "INSERT INTO Recetas VALUES(1, 'PANTRUCAS')";
  114.         recetas[2] = "INSERT INTO Recetas VALUES(2, 'TALLARINES CON
  115.     SALSA')";
  116.         recetas[3] = "INSERT INTO Recetas VALUES(3, 'CAZUELA')";
  117.         return recetas;
  118.     }
  119.  
  120.     //INGREDIENTES
  121.     public String[] estructura1(){
  122.         String[] ingredientes = new String[6];
  123.         ingredientes[0] = "CREATE TABLE Ingredientes(id_ingrediente INTEGER,
  124.     NOMBRE_INGREDIENTES TEXT)";
  125.         ingredientes[1] = "INSERT INTO Ingredientes VALUES(1, 'TOMATE')";
  126.         ingredientes[2] = "INSERT INTO Ingredientes VALUES(2, 'HARINA')";
  127.         ingredientes[3] = "INSERT INTO Ingredientes VALUES(3, 'CARNE')";
  128.         return ingredientes;
  129.     }
  130.  
  131.     //R/I
  132.     public String[] estructura2(){
  133.         String[] RI = new String[6];
  134.         RI[0] = "CREATE TABLE RI(id_recetas INTEGER FOREIGN KEY REFERENCES
  135.     Recetas, id_ingredientes INTEGER FOREIGN KEY REFERENCES Ingredientes)";
  136.         RI[1] = "INSERT INTO RI VALUES(1, 2)";
  137.         RI[2] = "INSERT INTO RI VALUES(2, 1)";
  138.         RI[3] = "INSERT INTO RI VALUES(3, 3)";
  139.         return RI;
  140.     }
  141.  
  142.  
  143.     private final Context ourContext;
  144.     private DBHelper ourHelper;
  145.     private SQLiteDatabase ourDatabase;
  146.  
  147.  
  148.  
  149.     public DBHelper(Context context){
  150.         super(context, DATABASE_NAME, null, DATABASE_VERSION);
  151.         ourContext = context;
  152.     }
  153.  
  154.     public DBHelper open() throws SQLException{
  155.         ourHelper = new DBHelper(ourContext);
  156.         ourDatabase = ourHelper.getWritableDatabase();
  157.         return this;
  158.     }
  159.  
  160.  
  161.     @Override
  162.     public void onCreate(SQLiteDatabase db) {
  163.  
  164.         //CREAR TABLA USUARIOS
  165.         String sqlCode = "CREATE TABLE " + DATABASE_TABLE + " (" +
  166.                 KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
  167.                 KEY_NOMBRE + " TEXT NOT NULL, " +
  168.                 KEY_PASS + " TEXT NOT NULL);";
  169.         db.execSQL(sqlCode);
  170.  
  171.         //INTENTO DE CREAR TABLA RECETA
  172.         String sqlCode1 = "CREATE TABLE " + DATABASE_TABLE_R + " (" +
  173.                 KEY_RECETA + " INTEGER PRIMERY KEY AUTOINCREMENT, " +
  174.                 KEY_NOMBRER + " TEXT NOT NULL);";
  175.         db.execSQL(sqlCode1);
  176.  
  177.  
  178.  
  179.     }
  180.  
  181.     @Override
  182.     public void onUpgrade(SQLiteDatabase db, int i, int i1) {
  183.         db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
  184.         onCreate(db);
  185.  
  186.         //DROP RECETA
  187.         db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_R);
  188.         onCreate(db);
  189.  
  190.     }
  191.  
  192.     public Cursor validarUsuario(String usu, String pas){
  193.         Cursor mcursor = null;
  194.         mcursor=this.getReadableDatabase().query("usuarios", new String[]
  195.     {"_id", "_nombreusuario", "_pass"},
  196.                 "_nombreusuario like '" + usu + "' " + "and _pass like'" +
  197.     pas + "'",
  198.                 null, null, null, null);
  199.  
  200.         return mcursor;
  201.     }
  202.  
  203.     public Cursor recuperar(int idReceta, String nombreReceta){
  204.         Cursor cursor = null;
  205.         cursor = this.getReadableDatabase().query("recetas", new String[]
  206.     {"id_recetas", "nombre_receta"},
  207.                  "id_recetas like '" + idReceta + "' " + "and nombre_receta
  208.     `introducir el código aquí`like'" + nombreReceta + "'",
  209.                   null, null, null, null);
  210.         return cursor;
  211.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top