Android:如何获得最后一个id ?

时间:2022-09-25 15:34:55

I am having a problem in executing a query (with ORMLite) that adds a return to 'ultimaConfiguracao' in which to get the last 'id' inserted in the configuration table.

我在执行查询时遇到了一个问题(使用了ORMLite),这增加了一个返回到“最后的子句”,以便在配置表中插入最后一个“id”。

the search method 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:

使用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 in which I perform 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:

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 个解决方案

#1


0  

You should put tide(`) character before and after of your column_name and table_name.

您应该在column_name和table_name之前和之后放置tide(')字符。

I also faced with this problem. Here is the solution.

我也面临着这个问题。这是解决方案。

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

Before I put the tide character, it showed error like that "Could not perform raw query for SELECT MAX (id) FROM configuracao"

在放入tide字符之前,它显示了这样的错误:“不能执行从configuracao中选择MAX (id)的原始查询”

reference : http://ormlite.com/docs/raw-queries

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

#1


0  

You should put tide(`) character before and after of your column_name and table_name.

您应该在column_name和table_name之前和之后放置tide(')字符。

I also faced with this problem. Here is the solution.

我也面临着这个问题。这是解决方案。

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

Before I put the tide character, it showed error like that "Could not perform raw query for SELECT MAX (id) FROM configuracao"

在放入tide字符之前,它显示了这样的错误:“不能执行从configuracao中选择MAX (id)的原始查询”

reference : http://ormlite.com/docs/raw-queries

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