原型:

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方法,这里我们粘贴出该方法的部分实现

  1. [java] view plaincopy/**
  2. * General method for inserting a row into the database.
  3. *
  4. * @param table the table to insert the row into
  5. * @param nullColumnHack SQL doesn't allow inserting a completely empty row,
  6. *            so if initialValues is empty this column will explicitly be
  7. *            assigned a NULL value
  8. * @param initialValues this map contains the initial column values for the
  9. *            row. The keys should be the column names and the values the
  10. *            column values
  11. * @param conflictAlgorithm for insert conflict resolver
  12. * @return the row ID of the newly inserted row
  13. * OR the primary key of the existing row if the input param 'conflictAlgorithm' =
  14. * {@link #CONFLICT_IGNORE}
  15. * OR -1 if any error
  16. */
  17. public long insertWithOnConflict(String table, String nullColumnHack,
  18. ContentValues initialValues, int conflictAlgorithm) {
  19. if (!isOpen()) {
  20. throw new IllegalStateException("database not open");
  21. }
  22. // Measurements show most sql lengths <= 152
  23. StringBuilder sql = new StringBuilder(152);
  24. sql.append("INSERT");
  25. sql.append(CONFLICT_VALUES[conflictAlgorithm]);
  26. sql.append(" INTO ");
  27. sql.append(table);
  28. // Measurements show most values lengths < 40
  29. StringBuilder values = new StringBuilder(40);
  30. Set<Map.Entry<String, Object>> entrySet = null;
  31. if (initialValues != null && initialValues.size() > 0) {
  32. entrySet = initialValues.valueSet();
  33. Iterator<Map.Entry<String, Object>> entriesIter = entrySet.iterator();
  34. sql.append('(');
  35. boolean needSeparator = false;
  36. while (entriesIter.hasNext()) {
  37. if (needSeparator) {
  38. sql.append(", ");
  39. values.append(", ");
  40. }
  41. needSeparator = true;
  42. Map.Entry<String, Object> entry = entriesIter.next();
  43. sql.append(entry.getKey());
  44. values.append('?');
  45. }
  46. sql.append(')');
  47. } else {
  48. sql.append("(" + nullColumnHack + ") ");
  49. values.append("NULL");
  50. }

这里我们可以看到,当我们的ContentValues类型的数据initialValues为null,或者size<=0时,就会再sql语句中添加nullColumnHack的设置。我们可以想象一下,如果我们不添加nullColumnHack的话,那么我们的sql语句最终的结果将会类似insert into tableName()values();这显然是不允许的。而如果我们添加上nullColumnHack呢,sql将会变成这样,insert into tableName (nullColumnHack)values(null);这样很显然就是可以的。

下面附上插入操作的方法代码:

    1. public void insert(String name, String address, String type, String notes) {
    2. ContentValues cv=new ContentValues();
    3. cv.put("name", name);
    4. cv.put("address", address);
    5. cv.put("type", type);
    6. cv.put("notes", notes);
    7. getWritableDatabase().insert("restaurants", "name", cv);
    8. }

android之SQLite数据库insert操作的更多相关文章

  1. android 对sqlite数据库的增删改查等各种操作

    转载:http://blog.csdn.net/vrix/article/details/6717090 package com.sqlite.main; import java.io.File; i ...

  2. 我的Android六章:Android中SQLite数据库操作

    今天学习的内容是Android中的SQLite数据库操作,在讲解这个内容之前小编在前面有一篇博客也是讲解了SQLite数据库的操作,而那篇博客的讲解是讲述了 如何在Window中通过DOM来操作数据库 ...

  3. Android中SQLite数据库操作(1)——使用SQL语句操作SQLite数据库

    下面是最原始的方法,用SQL语句操作数据库.后面的"Android中SQLite数据库操作(2)--SQLiteOpenHelper类"将介绍一种常用的android封装操作SQL ...

  4. android 一个SQLite数据库多个数据表的基本使用框架 (带demo)

    android 一个SQLite数据库多个数据表(带demo) 前言        demo演示        一.搭建        二.建立实体类        三.建立数据库操作类        ...

  5. 从C#到Objective-C,循序渐进学习苹果开发(7)--使用FMDB对Sqlite数据库进行操作

    本随笔系列主要介绍从一个Windows平台从事C#开发到Mac平台苹果开发的一系列感想和体验历程,本系列文章是在起步阶段逐步积累的,希望带给大家更好,更真实的转换历程体验.本篇主要开始介绍基于XCod ...

  6. Android实现SQLite数据库联系人列表

    Android实现SQLite数据库联系人列表 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 工程内容 实现一个通讯录查看程序: 要求使用SQLite ...

  7. IOS开发-UI学习-sqlite数据库的操作

    IOS开发-UI学习-sqlite数据库的操作 sqlite是一个轻量级的数据库,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了,而且它的处理速度比Mysql.PostgreSQL这 ...

  8. 使用iOS原生sqlite3框架对sqlite数据库进行操作

    摘要: iOS中sqlite3框架可以很好的对sqlite数据库进行支持,通过面向对象的封装,可以更易于开发者使用. 使用iOS原生sqlite3框架对sqlite数据库进行操作 一.引言 sqlit ...

  9. android中sqlite数据库的基本使用和添加多张表

    看了很多关于android使用sqlite数据库的文章,很多都是介绍了数据库的建立和表的建立,而表通常都是只建立一张,而实际情况我们用到的表可能不止一张,那这种情况下我们又该怎么办呢,好了,下面我教大 ...

随机推荐

  1. Cocos2d-x -- 如何让背景从上到下滚动

    1. 首先,声明一个2个大小的sprite数组 class GameScreen : public cocos2d::Layer { public: ... cocos2d::Sprite *back ...

  2. KineticJS教程(5)

    KineticJS教程(5) 作者: ysm  5.事件响应 5.1.图形的事件响应 图形对象对事件的响应处理可以使用 on() 方法绑定事件类型和相应方法. On() 方法需要一个事件类型参数和相应 ...

  3. Makefile自动编写工具实例

    准备源文件如下: /*test.c*/ #include <stdio.h>#include "phello.h"#include "pword.h" ...

  4. Unity时钟定时器插件——Vision Timer源码分析之二

      Unity时钟定时器插件——Vision Timer源码分析之二 By D.S.Qiu 尊重他人的劳动,支持原创,转载请注明出处:http.dsqiu.iteye.com 前面的已经介绍了vp_T ...

  5. VB 获取默认打印机的状态

    如何获取默认打印机的状态,包括缺纸.卡纸.无连接等状态,还有将某文件打印后,如何得知打印成功? Option ExplicitDeclare Function MapPhysToLin Lib &qu ...

  6. js 替换json对象中的键名

      js 替换json对象中的键名 CreateTime--2018年3月30日15:38:50 Author:Marydon 情景描述: 有个json数组,现在需要将json对象中的key替换掉,值 ...

  7. 〖C++〗string2int把字符串转换成int的函数

    #include <stdio.h> #include <stdlib.h> #include <string.h> int string2int(char *ar ...

  8. jQuery name选择器 带正则 写法示例

    $("div[id]") 选择所有含有id属性的div元素 $("input[name='target']") 选择所有的name属性等于'target'的in ...

  9. Flume入门:安装、部署

    一.什么是Flume? flume 作为 cloudera 开发的实时日志收集系统,受到了业界的认可与广泛应用.Flume 初始的发行版本目前被统称为 Flume OG(original genera ...

  10. Linux内核开发之异步通知与异步I/O(一)

    “小王,听说过锦上添花吧..”我拍拍下王的头说. “还锦上添花你,为你上次提的几个东东,我是头上长包..”小王气愤地瞪着我. “啊,为啥这样呢,本来还特意拒绝了MM的月份,抽出时间打算给你说点高级的东 ...