加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Sqlite创建表一定要声明字段类型(ContentProvider查询的小问题

发布时间:2020-12-12 19:56:01 所属栏目:百科 来源:网络整理
导读:用ContentProvider查询,有时候不太灵。。就像下面的代码,数据库中存在该数据,但是查不出来。原因可能是该字段没有声明类型。。 如下: private final static String SQL_CREATE_TABLE_INSTALLED = "CREATE TABLE if not exists " + TABLE_INSTALLED + " (

用ContentProvider查询,有时候不太灵。。就像下面的代码,数据库中存在该数据,但是查不出来。原因可能是该字段没有声明类型。。

如下:

private final static String SQL_CREATE_TABLE_INSTALLED =
"CREATE TABLE if not exists " + TABLE_INSTALLED + " ( "
+ "id not null,"
+ "name text not null,"
+ "primary key(id)"
+")";

这样的id字段没有任何问题。使用下面的contentprovide进行查询会查不到数据。。。

ContentResolver resolver = context.getContentResolver();
Uri uri = getContentUri();
Cursor cursor = resolver.query(uri,DataColumns.ALL,DataColumns.ID
+ "=?",new String[] { Integer.toString(id) },
DEFAULT_ORDER);

这样的小问题还真是让人无解。

用下面的方法可以查得到。直接用参数查。

Cursor cursor = resolver.query(getContentUri(),
DataColumns.ID + " = " + id,null,DEFAULT_ORDER);

所以,写代码还是得细心,在创建表时候,注意各个字段类型。sqlite不会进行检查字段类型的。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读