在SQLite包装器中执行查询(Android持久性库)

时间:2015-03-11 18:17:24

标签: android database sqlite android-library

我使用Android persistence library作为SQLite包装器。但是我无法理解如何在那里执行查询。假设我想在查询下面执行

"SELECT * FROM " + TABLE_VIDEO +" where " + KEY_TYPE + "='popular'"+ " order by " + KEY_NOSEEN + " DESC "

首先使用这个库,我无法知道表的列名。其次,如何使用这个库来判断where子句。假设对象类的变量是列名,我试图执行以下查询 -

Constraint constraint = new Constraint().orderBy("noOfSeen");
List cached = adapter.findAll(database,constraint);

但我得到以下异常

android.database.sqlite.SQLiteException: no such column: noOfSeen (code 1): , while compiling: SELECT * FROM videos ORDER BY noOfSeen

在我的应用程序类中,我添加了 -

 DatabaseSpec database = PersistenceConfig.registerSpec(/**db version**/1);
        database.match(Video.class);

在适配器中,我正在添加以下代码以将数据添加到数据库

SqlAdapter adapterpopular = Persistence.getAdapter(mContext);
adapterpopular.store(video);

在使用以前没有任何库的数据库时,我使用了以下代码

String CREATE_VIDEO_TABLE = "create table "
                + TABLE_VIDEO + "(" + KEY_ID + " text not null unique, "
                + KEY_CAPTION + " text not null, "
                + KEY_LOCATION + " text not null, "
                + KEY_NOSHARES + " text not null, "
                + KEY_NOBEENTHERE + " text not null, "
                + KEY_NOMUSTGO + " text not null, "
                + KEY_NOSEEN + " text not null," + KEY_NOCOMMENTS + " text not null, " + KEY_USERNAME + " text not null," + KEY_USERID + " text not null," + KEY_TIMESTAMP + " INTEGER," + KEY_USERHANDLE + " text not null," + KEY_DTS + " text not null," + KEY_TYPE + " text not null," +  KEY_CACHE_TIMESTAMP + " text not null,"+KEY_ISMUSTGO + " text not null," +KEY_ISBEENTHERE + " text not null);";

    public void addVideo(Video videoSql) {
            SQLiteDatabase db = this.getWritableDatabase();

            ContentValues values = new ContentValues();
            values.put(KEY_ID, videoSql.getId());
            values.put(KEY_CAPTION, videoSql.getCaption());
            values.put(KEY_LOCATION, videoSql.getLocation());
            values.put(KEY_NOSHARES, videoSql.getNoOfShares());
            values.put(KEY_NOBEENTHERE, videoSql.getNoOfBeenTheres());
            values.put(KEY_NOMUSTGO, videoSql.getNoOfMustGos());
            values.put(KEY_NOCOMMENTS, videoSql.getNoOfComments());
            values.put(KEY_NOSEEN, videoSql.getNoOfSeen());
            values.put(KEY_USERNAME, videoSql.getUserName());
            values.put(KEY_USERID, videoSql.getUserId());
            values.put(KEY_TIMESTAMP, videoSql.getTimestamp());
            values.put(KEY_USERHANDLE, videoSql.getUserHandle());
            values.put(KEY_DTS, videoSql.getDts());
            values.put(KEY_TYPE, videoSql.getType());
            values.put(KEY_CACHE_TIMESTAMP,System.currentTimeMillis());
            values.put(KEY_ISMUSTGO,videoSql.getIsmustGo());
            values.put(KEY_ISBEENTHERE,videoSql.getIsbeenThere());

            try {
                db.replace(TABLE_VIDEO, null, values);
            }catch(Exception e){}

            db.close();
        }

对象类

@JsonIgnoreProperties(ignoreUnknown = true)

        public class Video {


            private String no_of_seen;



            public String getNo_of_seen() {
                return no_of_seen;
            }

            @JsonProperty(value = "nSeen")
            public void setNo_of_seen(String no_of_seen) {
                this.no_of_seen = no_of_seen;
            }


        }

0 个答案:

没有答案