0

my error is that my app when developing with a single table is working perfectly but when trying to add a second, at the time of use this is closed ...

SQLite (Class where I have the creation of my database ...)

package company.viral.organizadorjec.ActivitysPrincipales;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by erny on 27/10/2016.
 */

public class SQLite extends SQLiteOpenHelper {

    //constructor.......
    public SQLite(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    //aqui se crea la tabla...
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table usuarios (id_usuario integer primary key autoincrement,usuario text, clave text)");

        db.execSQL("create table profesores (id_profesor integer primary key autoincrement,nombreprofesor text, comentarioprofesor text)");    

        db.execSQL("insert into usuarios values('0','admin','admin')");
        db.execSQL("insert into profesores values('0','alfonso','pirata')");    
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("create table usuarios (id_usuario integer primary key autoincrement, " +
                "usuario text, clave text)");

        db.execSQL("create table profesores (id_profesor integer primary key autoincrement, " +
                "nombreprofesor text, comentarioprofesor text)");


        db.execSQL("insert into usuarios values('0','admin','admin')");
        db.execSQL("insert into profesores values('0','alfonso','autobus')");   

    }
}

Main Activity (Class that uses my first table for system access-which is a Navigation Drawer)

package company.viral.organizadorjec.ActivitysPrincipales;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import company.viral.organizadorjec.R;

//aqui empieza...
public class MainActivity extends AppCompatActivity {

    //creamos variables EditText para capturar los datos
    private EditText aetid,aetpass;
    private Cursor fila;    

    //en este metodo SIEMPRE se dibuja la app correspondiente
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    //antes de dibujar definimos las variables y a quienes pertecen en el layout

        aetid = (EditText) findViewById(R.id.etid);
        aetpass = (EditText) findViewById(R.id.etpass);    
    } 

    //creamos los metodos con los que reaccionan los btn (onClick)
    /*metodo para entrar y buscar (en construccion.... explorando metodos)*/
    public void onClickAceptar (View view) {

        String auxn = aetid.getText().toString();
        String auxp = aetpass.getText().toString();

        SQLite admin = new SQLite(this,"administracion", null, 1);
        SQLiteDatabase bd = admin.getWritableDatabase();

        fila=bd.rawQuery("select usuario, clave from usuarios where usuario='"+auxn+"'and clave='"+auxp+"'",null);   

        if(fila.moveToFirst()==true){

            //capturamos los valores del cursos y lo almacenamos en variable
            String usua=fila.getString(0);
            String pass=fila.getString(1);

            //preguntamos si los datos ingresados son iguales
            if (auxn.equals(usua)&&auxp.equals(pass)){

                //si son iguales entonces vamos a otra ventana
                //Menu es una nueva actividad empty
                Intent ven=new Intent(this,MenuCentral.class);

                startActivity(ven);

                //limpiamos las las cajas de texto
                aetid.setText("");
                aetpass.setText("");    
            }    
        }else {    
            Toast.makeText(getApplicationContext(), "Usuario o contraseña erroneo", Toast.LENGTH_LONG).show();
        }

        bd.close();
    }    

    //metodo para entrar a la actividad de registro

    public void onClickRegistro(View view){
        Intent i = new Intent(this,Registro.class);
        startActivity(i);
    }    
}

ProfesoresF (Fragment that is generated inNavigation Drawer and invokes the teachers table {table that gives me the problem)

package company.viral.organizadorjec.FragmentMenu;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import company.viral.organizadorjec.R;
import company.viral.organizadorjec.ActivitysPrincipales.SQLite;    

public class ProfesoresF extends Fragment {
    private Cursor buscador;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_profesores, container, false);

        SQLite admin = new SQLite(getContext(),"administracion",null,1);
        SQLiteDatabase bd = admin.getWritableDatabase();


        buscador=bd.rawQuery("select nombreprofesor from profesores ",null);

        String [] listamateria = new String[buscador.getCount()];

        int i=0;
        while (buscador.moveToNext()){
            String contenedor = buscador.getString(buscador.getColumnIndex("nombreprofesor"));
            listamateria[i]=contenedor;
            i++;
        }

        //adaptadores
        //adaptador dias

        ListView listaprofe = (ListView) view.findViewById(R.id.listprofef);

        ArrayAdapter<String> listavistaprofes = new ArrayAdapter<String>(
                getActivity(),
                android.R.layout.simple_list_item_1,listamateria);    

        listaprofe.setAdapter(listavistaprofes);    
        return view ;
    }    
}

Sorry my bad English, everything translated google, I really need help with this, thank you very much for your time

ArK
  • 20,698
  • 67
  • 109
  • 136
  • which method is giving error? Also please share logs at time of error – Amod Gokhale Dec 08 '16 at 13:25
  • Possible duplicate of [When is SQLiteOpenHelper onCreate() / onUpgrade() run?](http://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run) – Phantômaxx Dec 09 '16 at 08:06
  • Try Uninstalling the app and re-install again. When you change anything in Database side in SQLite you must uninstall and re-install again for the changes update. – Dev Tamil Dec 09 '16 at 09:22
  • @DevTamil Exactly as said in the link I posted as a duplicate. – Phantômaxx Dec 09 '16 at 12:54

0 Answers0