Android: Problem searching last id inserted with Sqlitedatabase (ORM Lite)

Asked

Viewed 336 times

1

I am having a problem executing a query (with Ormlite) that adds the return to 'ultimaConfiguration' in which you should get the last 'id' inserted in the Configuration table.

the method that searches for the last configuration is as follows:

public Integer buscaIdUltimaAtualizacao() throws SQLException {
        GenericRawResults<Integer> raw = this.queryRaw("SELECT MAX (id) FROM configuracao", new RawRowMapper<Integer>() {
            Configuracao c = new Configuracao();
            @Override
            public Integer mapRow(String[] columns, String[] results) throws SQLException {
                if(results[0] != null)
                    return Integer.parseInt(results[0]);
                else
                    return 0;
            }
        });
        return raw.getFirstResult();
    }

Class using Sqlite:

@DatabaseTable(tableName = "configuracao", daoClass = ConfiguracaoDao.class)
public class Configuracao {

    public Configuracao() {
        /*deixar vazio*/
    }

    @DatabaseField(generatedId = true)
    private int id;

    @DatabaseField(dataType = DataType.DATE, canBeNull = true)
    private Date ultima_atualizacao_artista;

    @DatabaseField(dataType = DataType.DATE, canBeNull = true)
    private Date ultima_atualizacao_diaEvento;

    @DatabaseField(dataType = DataType.DATE, canBeNull = true)
    private Date ultima_atualizacao_evento;
...

context where I perform the operations:

protected Boolean doInBackground(Void... paths){
        try {
            this.ultimaConfiguracao = this.confDao.queryForId(confDao.buscaIdUltimaAtualizacao().toString());

            if(ultimaConfiguracao != null){
                //baixa somente o que tem atualização
            }
            else{
                //region MIDIA
                this.query = new ParseQuery("Midia");
                this.query.whereEqualTo("ativo", true);
                query.findInBackground(new FindCallback() {
                    @Override
                    public void done(List list, com.parse.ParseException e) {
                        if(!list.isEmpty()){
                            for(ParseObject pObject : (List<ParseObject>) list) {
                                publishProgress(((Activity) listener).getString(R.string.m_progress_textView_verificandoAtualizacoes));
                                Midia md = new Midia();

Logcat:

02-23 22:36:24.097    1446-1453/com.universo91.towersrock W/art﹕ Suspending all threads took: 313.333ms
02-23 22:36:24.936    1446-1453/com.universo91.towersrock W/art﹕ Suspending all threads took: 651.381ms
02-23 22:36:25.009    1446-1491/com.universo91.towersrock W/System.err﹕ java.sql.SQLException: Could not perform raw query for SELECT MAX (id) FROM configuracao
02-23 22:36:25.011    1446-1491/com.universo91.towersrock W/System.err﹕ at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
02-23 22:36:25.011    1446-1491/com.universo91.towersrock W/System.err﹕ at com.j256.ormlite.dao.BaseDaoImpl.queryRaw(BaseDaoImpl.java:552)
02-23 22:36:25.011    1446-1491/com.universo91.towersrock W/System.err﹕ at com.universo91.towersrock.DAO.ConfiguracaoDao.buscaIdUltimaAtualizacao(ConfiguracaoDao.java:78)
02-23 22:36:25.011    1446-1491/com.universo91.towersrock W/System.err﹕ at com.universo91.towersrock.BL.PSTowersRock.doInBackground(PSTowersRock.java:72)
02-23 22:36:25.011    1446-1491/com.universo91.towersrock W/System.err﹕ at com.universo91.towersrock.BL.PSTowersRock.doInBackground(PSTowersRock.java:39)
02-23 22:36:25.011    1446-1491/com.universo91.towersrock W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
02-23 22:36:25.012    1446-1491/com.universo91.towersrock W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-23 22:36:25.012    1446-1491/com.universo91.towersrock W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
02-23 22:36:25.012    1446-1491/com.universo91.towersrock W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-23 22:36:25.012    1446-1491/com.universo91.towersrock W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-23 22:36:25.012    1446-1491/com.universo91.towersrock W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
02-23 22:36:25.012    1446-1491/com.universo91.towersrock W/System.err﹕ Caused by: java.sql.SQLException: Getting a writable database from helper DatabaseHelper@28ef97dc failed
02-23 22:36:25.013    1446-1491/com.universo91.towersrock W/System.err﹕ at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
02-23 22:36:25.013    1446-1491/com.universo91.towersrock W/System.err﹕ at com.j256.ormlite.android.AndroidConnectionSource.getReadWriteConnection(AndroidConnectionSource.java:68)
02-23 22:36:25.013    1446-1491/com.universo91.towersrock W/System.err﹕ at com.j256.ormlite.android.AndroidConnectionSource.getReadOnlyConnection(AndroidConnectionSource.java:54)
02-23 22:36:25.014    1446-1491/com.universo91.towersrock W/System.err﹕ at com.j256.ormlite.stmt.StatementExecutor.queryRaw(StatementExecutor.java:306)
02-23 22:36:25.014    1446-1491/com.universo91.towersrock W/System.err﹕ at com.j256.ormlite.dao.BaseDaoImpl.queryRaw(BaseDaoImpl.java:550)
02-23 22:36:25.014    1446-1491/com.universo91.towersrock W/System.err﹕ ... 9 more
02-23 22:36:25.014    1446-1491/com.universo91.towersrock W/System.err﹕ Caused by: android.database.sqlite.SQLiteException: Can't downgrade database from version 4 to 1
02-23 22:36:25.015    1446-1491/com.universo91.towersrock W/System.err﹕ at android.database.sqlite.SQLiteOpenHelper.onDowngrade(SQLiteOpenHelper.java:360)
02-23 22:36:25.015    1446-1491/com.universo91.towersrock W/System.err﹕ at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:254)
02-23 22:36:25.066    1446-1491/com.universo91.towersrock W/System.err﹕ at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
02-23 22:36:25.066    1446-1491/com.universo91.towersrock W/System.err﹕ at com.j256.ormlite.android.AndroidConnectionSource.getReadWriteConnection(AndroidConnectionSource.java:66)
02-23 22:36:25.067    1446-1491/com.universo91.towersrock W/System.err﹕ ... 12 more

Databasehelper:

package com.universo91.towersrock.DAO;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.universo91.towersrock.Models.*;
import java.sql.SQLException;


public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

    private static final String databaseName = "towersrock.db";
    private static final int databaseVersion = 4;

    public DatabaseHelper(Context context) {
        super(context, databaseName, null, databaseVersion);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource cs) {
        try {
            TableUtils.createTable(cs, Midia.class);
            TableUtils.createTable(cs, Artista.class);
            TableUtils.createTable(cs, Configuracao.class);
            TableUtils.createTable(cs, DiaEvento.class);
            TableUtils.createTable(cs, Evento.class);
            TableUtils.createTable(cs, Excursao.class);
            TableUtils.createTable(cs, Ingresso.class);
            TableUtils.createTable(cs, Noticia.class);
            TableUtils.createTable(cs, Passageiro.class);
            TableUtils.createTable(cs, PontoDeVenda.class);
            TableUtils.createTable(cs, User.class);
            TableUtils.createTable(cs, Veiculo.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource cs, int i, int i2) {
        try {
            TableUtils.dropTable(cs, Midia.class, true);
            TableUtils.dropTable(cs, Artista.class, true);
            TableUtils.dropTable(cs, Configuracao.class, true);
            TableUtils.dropTable(cs, DiaEvento.class, true);
            TableUtils.dropTable(cs, Evento.class, true);
            TableUtils.dropTable(cs, Excursao.class, true);
            TableUtils.dropTable(cs, Ingresso.class, true);
            TableUtils.dropTable(cs, Noticia.class, true);
            TableUtils.dropTable(cs, Passageiro.class, true);
            TableUtils.dropTable(cs, PontoDeVenda.class, true);
            TableUtils.dropTable(cs, User.class, true);
            TableUtils.dropTable(cs, Veiculo.class, true);
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

    @Override
    public void close() {
        super.close();
    }
}
  • 1

    For the reason given for the error: Can't downgrade database from version 4 to 1, database version changed from 4 to 1.

  • @great ramaral, but when I change to the 4 or 5 again, gives the same problem.

  • Gives the same error? Show how you implemented SQLiteOpenHelper.onUpgrade

  • @ramaral this class is implemented in the ORM, it was not I who implemented it. I would have to look at the documentation.

  • I didn’t realize you were using Ormlite.

2 answers

1

Try it if it works.

long max = this.queryRawValue("SELECT MAX (CAMPO_ID) FROM configuracao");

Or you can try , adapt second example of the documentation Here: http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_2.html#Raw-Statements

// find max id
GenericRawResults<String[]> rawResults =
orderDao.queryRaw("SELECT MAX (CAMPO_ID) FROM configuracao");

// there should be 1 result
List<String[]> results = rawResults.getResults();

// the results array should have 1 value
String[] resultArray = results.get(0);

// this should print the number  
System.out.println("Show Result MAX ID: " + resultArray[0]  );
  • E/Sqlitelog (1) in such table: configuration.... I just edited the question with the helper class.

0

As it says in logcat, the problem is that the configuration table was not created, I just uninstalled the app and installed it again to access the onCreate() of Databasehelper and thus create my new tables.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.