Hi, my mistake is that when my app is developed with a single table it works perfectly but when trying to add a second one, when using it it closes...
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);
}
}
ProfessoresF ( fragment
which is generated in Navigation Drawer
and invokes the table teachers {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 ;
}
}
error this is the error that throws meandroid monitor
12-08 08:46:13.198 7388-7388/company.viral.organizadorjec E/SQLiteLog: (1) no such table: profesores
12-08 08:46:13.206 7388-7388/company.viral.organizadorjec E/AndroidRuntime: FATAL EXCEPTION: main
Process: company.viral.organizadorjec, PID: 7388
android.database.sqlite.SQLiteException: no such table: profesores (code 1): , while compiling: select nombreprofesor from profesores
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:893)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:504)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1339)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1278)
at company.viral.organizadorjec.FragmentMenu.ProfesoresF.onCreateView(ProfesoresF.java:29)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:536)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
the error seems to me to be that you have an old database on your device, that's why it doesn't find the table even when you create it. In the method
onUpgrade
you create the table again and that is fine since in future updates to keep the data that is where it is kept.If you are testing in an emulator I recommend that you check the DDMS if your database file is really changing its structure.
Uninstall the application from your phone if this is the case and check again.
I recommend that for every change you make to the database, you drop the database and do test inserts on your
Activity
main. Since in a production phase that would not be necessary