Android:如何将最后一个id插入ORMLite?

时间:2015-02-23 23:15:51

标签: java android ormlite

我在执行查询(使用ORMLite)时遇到问题,该查询添加了一个返回'ultimaConfiguracao',在其中将最后一个'id'插入配置表中。

搜索方法的最后一个配置如下:

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();
    }

使用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;
    ...

我执行操作的上下文:

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

1 个答案:

答案 0 :(得分:0)

您应该在column_name和table_name之前和之后加上tide(`)字符。

我也遇到过这个问题。这是解决方案。

QueryBuilder<Account, Integer> qb = accountDao.queryBuilder();
// select 2 aggregate functions as the return
qb.selectRaw("MAX(`column_name`)");
// the results will contain 2 string values for the min and max
results = accountDao.queryRaw(qb.prepareStatementString());
String[] values = results.getFirstResult();

在我放置潮汐字符之前,它显示错误,如#34;无法执行SELECT MAX(id)FROM configuracao&#34;

的原始查询

参考:http://ormlite.com/docs/raw-queries