android之SQLite数据库insert操作
原型:
long Android.database.sqlite.SQLiteDatabase.insert(String table, String nullColumnHack, ContentValues values)
参数介绍:
table: 要插入数据的表的名称
nullColumnHack:当values参数为空或者里面没有内容的时候,我们insert是会失败的(底层数据库不允许插入一个空行),为了防止这种情况,我们要在这里指定一个列名,到时候如果发现将要插入的行为空行时,就会将你指定的这个列名的值设为null,然后再向数据库中插入。
values:一个ContentValues对象,类似一个map.通过键值对的形式存储值。
这里很多人会迷惑,nullColumnHack到底干什么用的,为什么会出现呢。当我们不设定一列的时候,不都是数据库给设为默认值吗?很多字段设置默认值也是null,这里显示的设置也是null,有什么区别吗,怎么会显示设置了之后就允许插入了呢?
其实在底层,各种insert方法最后都回去调用insertWithOnConflict方法,这里我们粘贴出该方法的部分实现
- [java] view plaincopy/**
- * General method for inserting a row into the database.
- *
- * @param table the table to insert the row into
- * @param nullColumnHack SQL doesn't allow inserting a completely empty row,
- * so if initialValues is empty this column will explicitly be
- * assigned a NULL value
- * @param initialValues this map contains the initial column values for the
- * row. The keys should be the column names and the values the
- * column values
- * @param conflictAlgorithm for insert conflict resolver
- * @return the row ID of the newly inserted row
- * OR the primary key of the existing row if the input param 'conflictAlgorithm' =
- * {@link #CONFLICT_IGNORE}
- * OR -1 if any error
- */
- public long insertWithOnConflict(String table, String nullColumnHack,
- ContentValues initialValues, int conflictAlgorithm) {
- if (!isOpen()) {
- throw new IllegalStateException("database not open");
- }
- // Measurements show most sql lengths <= 152
- StringBuilder sql = new StringBuilder(152);
- sql.append("INSERT");
- sql.append(CONFLICT_VALUES[conflictAlgorithm]);
- sql.append(" INTO ");
- sql.append(table);
- // Measurements show most values lengths < 40
- StringBuilder values = new StringBuilder(40);
- Set<Map.Entry<String, Object>> entrySet = null;
- if (initialValues != null && initialValues.size() > 0) {
- entrySet = initialValues.valueSet();
- Iterator<Map.Entry<String, Object>> entriesIter = entrySet.iterator();
- sql.append('(');
- boolean needSeparator = false;
- while (entriesIter.hasNext()) {
- if (needSeparator) {
- sql.append(", ");
- values.append(", ");
- }
- needSeparator = true;
- Map.Entry<String, Object> entry = entriesIter.next();
- sql.append(entry.getKey());
- values.append('?');
- }
- sql.append(')');
- } else {
- sql.append("(" + nullColumnHack + ") ");
- values.append("NULL");
- }
这里我们可以看到,当我们的ContentValues类型的数据initialValues为null,或者size<=0时,就会再sql语句中添加nullColumnHack的设置。我们可以想象一下,如果我们不添加nullColumnHack的话,那么我们的sql语句最终的结果将会类似insert into tableName()values();这显然是不允许的。而如果我们添加上nullColumnHack呢,sql将会变成这样,insert into tableName (nullColumnHack)values(null);这样很显然就是可以的。
下面附上插入操作的方法代码:
- public void insert(String name, String address, String type, String notes) {
- ContentValues cv=new ContentValues();
- cv.put("name", name);
- cv.put("address", address);
- cv.put("type", type);
- cv.put("notes", notes);
- getWritableDatabase().insert("restaurants", "name", cv);
- }
android之SQLite数据库insert操作的更多相关文章
- android 对sqlite数据库的增删改查等各种操作
转载:http://blog.csdn.net/vrix/article/details/6717090 package com.sqlite.main; import java.io.File; i ...
- 我的Android六章:Android中SQLite数据库操作
今天学习的内容是Android中的SQLite数据库操作,在讲解这个内容之前小编在前面有一篇博客也是讲解了SQLite数据库的操作,而那篇博客的讲解是讲述了 如何在Window中通过DOM来操作数据库 ...
- Android中SQLite数据库操作(1)——使用SQL语句操作SQLite数据库
下面是最原始的方法,用SQL语句操作数据库.后面的"Android中SQLite数据库操作(2)--SQLiteOpenHelper类"将介绍一种常用的android封装操作SQL ...
- android 一个SQLite数据库多个数据表的基本使用框架 (带demo)
android 一个SQLite数据库多个数据表(带demo) 前言 demo演示 一.搭建 二.建立实体类 三.建立数据库操作类 ...
- 从C#到Objective-C,循序渐进学习苹果开发(7)--使用FMDB对Sqlite数据库进行操作
本随笔系列主要介绍从一个Windows平台从事C#开发到Mac平台苹果开发的一系列感想和体验历程,本系列文章是在起步阶段逐步积累的,希望带给大家更好,更真实的转换历程体验.本篇主要开始介绍基于XCod ...
- Android实现SQLite数据库联系人列表
Android实现SQLite数据库联系人列表 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 工程内容 实现一个通讯录查看程序: 要求使用SQLite ...
- IOS开发-UI学习-sqlite数据库的操作
IOS开发-UI学习-sqlite数据库的操作 sqlite是一个轻量级的数据库,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了,而且它的处理速度比Mysql.PostgreSQL这 ...
- 使用iOS原生sqlite3框架对sqlite数据库进行操作
摘要: iOS中sqlite3框架可以很好的对sqlite数据库进行支持,通过面向对象的封装,可以更易于开发者使用. 使用iOS原生sqlite3框架对sqlite数据库进行操作 一.引言 sqlit ...
- android中sqlite数据库的基本使用和添加多张表
看了很多关于android使用sqlite数据库的文章,很多都是介绍了数据库的建立和表的建立,而表通常都是只建立一张,而实际情况我们用到的表可能不止一张,那这种情况下我们又该怎么办呢,好了,下面我教大 ...
随机推荐
- bundle中vim相关快捷键的使用
http://www.cnblogs.com/respawn/archive/2012/08/21/2649483.html http://blog.163.com/liao_ya/blog/stat ...
- LDA基本介绍以及LDA源码分析(BLEI)
基本介绍: topic model,主题模型介绍:http://www.cnblogs.com/lixiaolun/p/4455764.html 以及 (http://blog.csdn.net/h ...
- docker sshd image problem, session required pam_loginuid.so, cann't login
在使用sshd docker 镜像时, 发现一个比较诡异的问题, 有些启动的容器可以连接, 有些不能. 例如 : 启动2个容器(这两个容器都有问题) : [root@localhost ~]# d ...
- HDU 5389 Zero Escape(dp啊 多校)
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5389 Problem Description Zero Escape, is a visual no ...
- CI:关于计算智能
该书认为,当前的计算智能起源于人工智能和生物智能.人工智能的起源大约可以追溯到50年以前,而计算智能这个术语则仅仅出现于10年以前.计算智能由几个部分组成,即人工智能.模糊集和模糊逻辑.神经网络(有时 ...
- spring Log4j关于No appenders could be found for logger的警告
(spring环境下)配置Log4j时候,当启动WEB程序时,提示了如标题的警告,具体如下:log4j:WARN No appenders could be found for logger (org ...
- mysql优化一
1.show global status 可以列出MySQL服务器运行各种状态值 2.show variables 查询MySQL服务器配置信息 一.慢查询 mysql ...
- Eclipse保存验证JS缓慢
EclipseSave保存js文件的时候验证JS的时间很长( 使用extjs).Eclipse – Validation – JavaScript Validator 的 Manual和Build都没 ...
- jquery 鼠标拖动排序Li或Table
1.前端页面 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="拖动排序Li或Ta ...
- 通过XmlDocument读写Xml文档参考地址
/// <summary> /// 获取一个报表的参数 http://blog.csdn.net/hdhai9451/article/details/12170069 /// </s ...