【Android】ContentValues的用法
2015-06-10 18:29
363 查看
ContentValues 和HashTable类似都是一种存储的机制 但是两者最大的区别就在于,contenvalues只能存储基本类型的数据,像string,int之类的,不能存储对象这种东西,而HashTable却可以存储对象。
在忘数据库中插入数据的时候,首先应该有一个ContentValues的对象所以:
ContentValues initialValues = new ContentValues();
initialValues.put(key,values);
SQLiteDataBase sdb ;
sdb.insert(database_name,null,initialValues);
插入成功就返回记录的id否则返回-1;
在忘数据库中插入数据的时候,首先应该有一个ContentValues的对象所以:
ContentValues initialValues = new ContentValues();
initialValues.put(key,values);
SQLiteDataBase sdb ;
sdb.insert(database_name,null,initialValues);
插入成功就返回记录的id否则返回-1;
public Uri insert(Uri uri, ContentValues initialValues) { if (uriMatcher.match(uri) != CONTACTS) { throw new IllegalArgumentException("unknow uri " + uri); } ContentValues values; if (initialValues != null) { values = new ContentValues(initialValues); System.out.println("contentValues插入成功,initailValues不是空的"); } else { values = new ContentValues(); } Long now = Long.valueOf(System.currentTimeMillis()); // 设置默认值 if (values.containsKey(ContactColumn.CREATED) == false) { values.put(ContactColumn.CREATED, now); } if (values.containsKey(ContactColumn.NAME) == false) { values.put(ContactColumn.NAME, now); } if (values.containsKey(ContactColumn.EMAIL) == false) { values.put(ContactColumn.EMAIL, now); } if (values.containsKey(ContactColumn.MOBILE) == false) { values.put(ContactColumn.MOBILE, now); } if (values.containsKey(ContactColumn.MODIFIED) == false) { values.put(ContactColumn.MODIFIED, now); } System.out.println("应该插入成功了吧"); long RowId = contactsDB.insert(CONTACTS_TABLE, null, values); if (RowId > 0) { Uri noteUri = ContentUris.withAppendedId(CONTENT_URI, RowId); getContext().getContentResolver().notifyChange(noteUri, null); System.out.println("到这里也是没问题的!"); return noteUri; } throw new IllegalArgumentException("unknow uri " + uri); }
相关文章推荐
- 使用DjangoUeditor将Ueditor移植到Django(BAE环境下)
- Inverse = true,cascade=all
- 【Win 10 应用开发】打印UI元素
- 将UIImage转化成NSData在转化成NSString(将UIImage转化成Base64的字节流)
- Building and Testing with Gradle
- 异步模块定义AMD
- UIView与CALayer的区别,很详细
- oralce10g 安装dbvault出错OUI-25031
- 【MySql】Column count doesn't match value count at row 1 解决方法
- UVA11995:I Can Guess the Data Structure
- 得到UIImage的方法
- iOS UITableView的Section Footer添加按钮
- iOS UITableView的Section Footer添加按钮
- Android Bluetooth开发
- 详细解析BluetoothAdapter的详细api
- GRE的issue写作要求
- 在storyboard中的静态UITableView中拖入 UISearchBar and Search Display Controller出现的奇怪问题
- request response 编码设置
- IOS开发—UITableViewCell的四种类型
- rust map的使用:获取某个key对应的value,如果不存在就插入一个