android之SQLite数据库insert操作
2013-08-15 18:52
411 查看
原型:
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方法,这里我们粘贴出该方法的部分实现
这里我们可以看到,当我们的ContentValues类型的数据initialValues为null,或者size<=0时,就会再sql语句中添加nullColumnHack的设置。我们可以想象一下,如果我们不添加nullColumnHack的话,那么我们的sql语句最终的结果将会类似insert into tableName()values();这显然是不允许的。而如果我们添加上nullColumnHack呢,sql将会变成这样,insert into tableName (nullColumnHack)values(null);这样很显然就是可以的。
下面附上插入操作的方法代码:
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数据库进行增删改[insert、update和delete] 操作
- [转]Android 如何对sqlite数据库进行增删改[insert、update和delete] 操作
- android之SQLite数据库insert操作
- android中SQLite数据库操作例程
- 安卓Android通过字段映射创建sqlite数据库,操作数据库,创建表,增、删、改查,通用方法,轻松编程(by寒冬)
- Android心得4.4--SQLite数据库--insert()、delete等方法
- android操作SQLite数据库
- Android LitePal 操作 SQLite数据库
- [置顶] Android多线程下操作sqlite数据库解决方案
- Android开发(10)使用SQLiteDatabase操作SQLite数据库实现增删改查(源码)
- Android中SQLite数据库操作(2)——使用SQLiteDatabase提供的方法操作数据库
- Android用SQLite数据库对数据存储及操作的总结
- Android 使用SQLiteDatabase操作SQLite数据库(二)
- 【转】Android使用SQLiteDatabase操作SQLite数据库【学习记录】
- android中SQLite数据库中用insert同时插入多条记录的方法和效率比较
- android典型代码系列(二)------sqlite数据库操作高级
- Android 存储学习之SQLite数据库的基本操作
- Android操作SQLite数据库
- Android精简小笔记(3):在adb shell中操作sqlite数据库的方法