1
I’m trying to develop a test app that requires two tables (aluno
and disciplina
) in class DataHelper
. I create two tables, but when testing, only the table Aluno
works. In summary: the application has some functionalities (insert student, insert discipline, delete student, among others) and all methods work for the student table, but no method works for the table of the discipline. What can it be?
Below, the class in which tables are created (DataHelper
) followed by the classes that manipulate each table, are these tables aluno
(AlunoDao
) and discipline (DisciplinaDao
) respectively and a class that opens and closes the database (Dao
):
public class DataHelper extends SQLiteOpenHelper {
public DataHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
try{
StringBuilder sbAluno = new StringBuilder();
StringBuilder sbDisciplina = new StringBuilder();
sbAluno.append( "CREATE TABLE IF NOT EXISTS [disciplina](\n" +
" [iddisciplina] INT PRIMARY KEY NOT NULL, \n" +
" [nomedisc] TEXT NOT NULL, \n" +
" [tutor] TEXT NOT NULL);");
db.execSQL(String.valueOf(sbAluno));
sbDisciplina.append("CREATE TABLE IF NOT EXISTS [aluno](\n" +
" [idaluno] INT PRIMARY KEY NOT NULL, \n" +
" [nome] TEXT NOT NULL, \n" +
" [curso] TEXT NOT NULL);" );
db.execSQL(String.valueOf(sbDisciplina));
}catch (Exception e){
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try{
StringBuilder sb= new StringBuilder();
sb.append("DROP TABLE IF EXISTS [aluno];" + "DROP TABLE IF EXISTS [disciplina];" );
String [] comando = sb.toString().split(";");
for (int i = 0; i < comando.length ; i++) {
db.execSQL(comando[i].toLowerCase()); //toLowerCase torna todas as letras minusculas
}
}catch (Exception e){
}
onCreate(db);
}
public class DisciplinaDao extends Dao {
private static final String TABELA = "disciplina";
private static final String IDDISCIPLINA = "iddisciplina";
private static final String NOMEDISC = "nomedisc";
private static final String TUTOR = "tutor";
public DisciplinaDao(Context context) {
super(context);
}
public void inserirDisciplina(Disciplina disciplina){
AbrirBanco();
//
ContentValues cv = new ContentValues();
cv.put(IDDISCIPLINA, disciplina.getIddisciplina());
cv.put(NOMEDISC, disciplina.getNomedisc());
cv.put(TUTOR, disciplina.getTutor());
//
db.insert(TABELA, null, cv);
//
FecharBanco();
}
public void atualizarDisciplina(Disciplina disciplina){
AbrirBanco();
//
ContentValues cv = new ContentValues();
//
String Filtro = "iddisciplina = ?";
String [] argumentos = { String.valueOf(disciplina.getIddisciplina()) };
//
cv.put(NOMEDISC, disciplina.getNomedisc());
cv.put(TUTOR, disciplina.getTutor());
//
db.update(TABELA, cv, Filtro, argumentos);
//
FecharBanco();
}
public void apagarDisciplina(int iddisciplina){
AbrirBanco();
//
String Filtro = "iddisciplina = ?";
String [] argumentos = { String.valueOf(iddisciplina) };
//
db.delete(TABELA, Filtro, argumentos);
//
FecharBanco();
}
public Disciplina obterContatoByID(int iddisciplina){
Disciplina cAux = null;
//
AbrirBanco();
//
Cursor cursor = null;
//
try{
String [] argumentos = { String.valueOf(iddisciplina) };
StringBuilder comando = new StringBuilder();
comando.append(" select * from disciplina where iddisciplina = ? ");
cursor = db.rawQuery(comando.toString(), argumentos);
//avançar os dados se nao conseguir passar pro proximo ele sai do while
while (cursor.moveToNext()){
cAux = new Disciplina();
cAux.setIddisciplina(cursor.getInt(cursor.getColumnIndex(IDDISCIPLINA)));
cAux.setNomedisc(cursor.getString(cursor.getColumnIndex(NOMEDISC)));
cAux.setTutor(cursor.getString(cursor.getColumnIndex(TUTOR)));
}
}catch (Exception e){
}finally {
if (cursor != null){
cursor.close();
cursor = null;
}
}
//
FecharBanco();
//
return cAux;
}
public ArrayList<HMAux> obterListaDisciplina(){
ArrayList<HMAux> dados = new ArrayList<>();
//
AbrirBanco();
//
Cursor cursor = null;
//
try{
StringBuilder comando = new StringBuilder();
comando.append(" select iddisciplina, nomedisc from disciplina order by nomedisc ");
cursor = db.rawQuery(comando.toString(), null);
//avançar os dados se nao conseguir passar pro proximo ele sai do while
while (cursor.moveToNext()){
HMAux hmAux = new HMAux();
hmAux.put(HMAux.id_disciplina, String.valueOf(cursor.getLong(cursor.getColumnIndex(IDDISCIPLINA))));
hmAux.put(HMAux.TEXTO_02, cursor.getString(cursor.getColumnIndex(NOMEDISC)));
dados.add(hmAux);
}
}catch (Exception e){
}finally {
if (cursor != null){
cursor.close();
cursor = null;
}
}
//
FecharBanco();
//
return dados;
}
public int proximoID(){
int proId = 0;
//
AbrirBanco();
//
Cursor cursor = null;
//
try{
StringBuilder comando = new StringBuilder();
comando.append("select max(iddisciplina)+1 as id from disciplina");
cursor = db.rawQuery(comando.toString(), null);
int x = 10;
//avançar os dados se nao conseguir passar pro proximo ele sai do while
while (cursor.moveToNext()){
proId = cursor.getInt(cursor.getColumnIndex("id"));
}
if(proId == 0){
proId = 1 ;
}
}catch (Exception e){
}finally {
if (cursor != null){
cursor.close();
cursor = null;
}
}
//
FecharBanco();
//
return proId;
}
}
public class AlunoDao extends Dao {
private static final String TABELA = "aluno";
private static final String IDALUNO = "idaluno";
private static final String NOME = "nome";
private static final String CURSO = "curso";
public AlunoDao(Context context) {
super(context);
}
public void inserirAluno(Aluno aluno){
AbrirBanco();
//
ContentValues cv = new ContentValues();
cv.put(IDALUNO, aluno.getIdaluno());
cv.put(NOME, aluno.getNome());
cv.put(CURSO, aluno.getCurso());
//
db.insert(TABELA, null, cv);
//
FecharBanco();
}
public void atualizarAluno(Aluno aluno){
AbrirBanco();
//
ContentValues cv = new ContentValues();
//
String Filtro = "idaluno = ?";
String [] argumentos = { String.valueOf(aluno.getIdaluno()) };
//
//cv.put(IDCONTATO, contato.getIdcontato());
cv.put(NOME, aluno.getNome());
cv.put(CURSO, aluno.getCurso());
//
db.update(TABELA, cv, Filtro, argumentos);
//
FecharBanco();
}
public void apagarAluno(int idaluno){
AbrirBanco();
//
String Filtro = "idaluno = ?";
String [] argumentos = { String.valueOf(idaluno) };
//
db.delete(TABELA, Filtro, argumentos);
//
FecharBanco();
}
public Aluno obterAlunoByID(long idaluno){
Aluno cAux = null;
//
AbrirBanco();
//
Cursor cursor = null;
//
try{
String [] argumentos = { String.valueOf(idaluno) };
StringBuilder comando = new StringBuilder();
comando.append(" select * from aluno where idaluno = ? ");
cursor = db.rawQuery(comando.toString(), argumentos);
//avançar os dados se nao conseguir passar pro proximo ele sai do while
while (cursor.moveToNext()){
cAux = new Aluno();
cAux.setIdaluno(cursor.getInt(cursor.getColumnIndex(IDALUNO)));
cAux.setNome(cursor.getString(cursor.getColumnIndex(NOME)));
cAux.setCurso(cursor.getString(cursor.getColumnIndex(CURSO)));
}
}catch (Exception e){
}finally {
if (cursor != null){
cursor.close();
cursor = null;
}
}
//
FecharBanco();
//
return cAux;
}
public ArrayList<HMAux> obterListaAluno(){
ArrayList<HMAux> dados = new ArrayList<>();
//
AbrirBanco();
//
Cursor cursor = null;
//
try{
StringBuilder comando = new StringBuilder();
comando.append(" select idaluno, nome from aluno order by nome ");
cursor = db.rawQuery(comando.toString(), null);
//avançar os dados se nao conseguir passar pro proximo ele sai do while
while (cursor.moveToNext()){
HMAux hmAux = new HMAux();
hmAux.put(HMAux.id, String.valueOf(cursor.getLong(cursor.getColumnIndex(IDALUNO))));
hmAux.put(HMAux.TEXTO_01, cursor.getString(cursor.getColumnIndex(NOME)));
dados.add(hmAux);
}
}catch (Exception e){
}finally {
if (cursor != null){
cursor.close();
cursor = null;
}
}
//
FecharBanco();
//
return dados;
}
public int proximoID(){
int proId = 0;
//
AbrirBanco();
//
Cursor cursor = null;
//
try{
StringBuilder comando = new StringBuilder();
comando.append(" select max(idaluno)+1 as id from aluno ");
cursor = db.rawQuery(comando.toString(), null);
//avançar os dados se nao conseguir passar pro proximo ele sai do while
while (cursor.moveToNext()){
proId = cursor.getInt(cursor.getColumnIndex("id"));
}
if(proId == 0){
proId = 1 ;
}
}catch (Exception e){
}finally {
if (cursor != null){
cursor.close();
cursor = null;
}
}
//
FecharBanco();
//
return proId;
}
}
public class Dao {
private Context context;
protected SQLiteDatabase db;
public Dao(Context context) {
this.context = context;
}
public void AbrirBanco(){
DataHelper dataHelper = new DataHelper(
context,
Constantes.BANCO,
null,
Constantes.VERSAO
);
this.db = dataHelper.getWritableDatabase();
}
public void FecharBanco(){
if (db != null){
db.close();
}
}
}
When using class methods AlunoDao
everything is normal. However, when using class methods DisciplinaDao
, do not work, but also no error. I created the two in the same way and with the same methods. I do not know what can be done. I thank you already.
Does it give any error? Put the code of one of the methods that do not work.
– ramaral
tries to do so oh, adicone this line in both tables:
db.execSQL("commit");
that might be this– Armando Marques Sobrinho
You created both tables at the same time or added the "discipline" table after?
– ramaral
I don’t understand your question. Are the two being created or just the students? If the two tables are being created the problem is not with their Datahelper class, but with the classes that manipulate their data.
– Thiago Queiroz
The two are created but only one works. I edited the question and put all the details of my project, thank you
– V. Godoy
Don’t put [solved] in the title. Instead, check how you accept the answer that solved your problem. If none of the answers have solved your problem and you have found the solution yourself, post the answer yourself.
– Victor Stafusa
Here’s your comment you posted on the question: "By researching the problem is in the version of the database, the solution I found was to uninstall and install the apk, it solved my problem. Link from where the solution was found:_ < http://stackoverflow.com/questions/21069532/android-database-sqlite-sqliteexception-no-such-table-admin-while-compiling-i >"
– Victor Stafusa