android之存储篇_SQLite数据库_让你彻底学会SQLite的使用
Posted xinruzhishui_11
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android之存储篇_SQLite数据库_让你彻底学会SQLite的使用相关的知识,希望对你有一定的参考价值。
<div id="article_content" class="article_content csdn-tracking-statistics" data-mod="popu_307" data-dsm="post" style="overflow: hidden;"> <p>SQLite最大的特点是你可以把各种类型的数据保存到任何字段中,而不用关心字段声明的数据类型是什么。</p><p>例如:可以在Integer类型的字段中存放字符串,或者在布尔型字段中存放浮点数,或者在字符型字段中存放日期型值。</p><p> 但有一种情况例外:定义为INTEGER PRIMARY KEY的字段只能存储64位整数, 当向这种字段保存除整数以外的数据时,将会产生错误。 </p><p>另外, SQLite 在解析CREATE TABLE 语句时,会忽略 CREATE TABLE 语句中跟在字段名后面的数据类型信息,如下面语句会忽略 name字段的类型信息:</p><div class="dp-highlighter bg_sql"><div class="bar"><div class="tools"><b>[sql]</b> <a href="#" class="ViewSource" title="view plain" οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 288px; top: 386px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_1" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_1" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&width=16&height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" οnclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-sql"><li class="alt"><span><span class="keyword">CREATE</span><span> </span><span class="keyword">TABLE</span><span> person (personid </span><span class="keyword">integer</span><span> </span><span class="keyword">primary</span><span> </span><span class="keyword">key</span><span> autoincrement, </span><span class="keyword">name</span><span> </span><span class="keyword">varchar</span><span>(20)) </span></span></li></ol><div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><pre code_snippet_id="1922727" snippet_file_name="blog_20170215_1_2948333" class="sql" name="code" style="display: none;">CREATE TABLE person (personid integer primary key autoincrement, name varchar(20)) </pre><p>SQLite可以解析大部分标准SQL语句,如:</p><p>查询语句:select * from 表名 where 条件子句 group by 分组字句 having ... order by 排序子句 (顺序一定不能错)</p><p>如:</p><div class="dp-highlighter bg_sql"><div class="bar"><div class="tools"><b>[sql]</b> <a href="#" class="ViewSource" title="view plain" οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 288px; top: 542px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_2" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&width=16&height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" οnclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-sql"><li class="alt"><span><span class="keyword">select</span><span> * </span><span class="keyword">from</span><span> person </span></span></li><li class=""><span> </span></li><li class="alt"><span><span class="keyword">select</span><span> * </span><span class="keyword">from</span><span> person </span><span class="keyword">order</span><span> </span><span class="keyword">by</span><span> id </span><span class="keyword">desc</span><span> </span></span></li><li class=""><span> </span></li><li class="alt"><span><span class="keyword">select</span><span> </span><span class="keyword">name</span><span> </span><span class="keyword">from</span><span> person </span><span class="keyword">group</span><span> </span><span class="keyword">by</span><span> </span><span class="keyword">name</span><span> </span><span class="keyword">having</span><span> </span><span class="func">count</span><span>(*)>1 </span></span></li></ol><div class="save_code tracking-ad" data-mod="popu_249" style="display: none;"><a href="javascript:;"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><pre code_snippet_id="1922727" snippet_file_name="blog_20170215_2_3090820" class="sql" name="code" style="display: none;"> select * from personselect * from person order by id desc
select name from person group by name having count(*)>1
</pre><p>分页SQL与mysql类似,下面SQL语句获取5条记录,跳过前面3条记录</p><div class="dp-highlighter bg_sql"><div class="bar"><div class="tools"><b>[sql]</b> <a href="#" class="ViewSource" title="view plain" οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 288px; top: 724px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_3" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_3" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&width=16&height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" οnclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-sql"><li class="alt"><span><span class="keyword">select</span><span> * </span><span class="keyword">from</span><span> Account limit 5 offset 3 </span></span></li></ol><div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><pre code_snippet_id="1922727" snippet_file_name="blog_20170215_3_3407247" class="sql" name="code" style="display: none;">select * from Account limit 5 offset 3 </pre><p>或者</p><div class="dp-highlighter bg_sql"><div class="bar"><div class="tools"><b>[sql]</b> <a href="#" class="ViewSource" title="view plain" οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 288px; top: 834px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_4" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_4" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=4&width=16&height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" οnclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-sql"><li class="alt"><span><span class="keyword">select</span><span> * </span><span class="keyword">from</span><span> Account limit 3,5 </span></span></li></ol><div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><pre code_snippet_id="1922727" snippet_file_name="blog_20170215_4_7452645" class="sql" name="code" style="display: none;">select * from Account limit 3,5 </pre><p>插入语句:</p><p>insert into 表名(字段列表) values(值列表)。如:</p><div class="dp-highlighter bg_sql"><div class="bar"><div class="tools"><b>[sql]</b> <a href="#" class="ViewSource" title="view plain" οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 288px; top: 967px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_5" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_5" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=5&width=16&height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" οnclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-sql"><li class="alt"><span><span class="keyword">insert</span><span> </span><span class="keyword">into</span><span> person(</span><span class="keyword">name</span><span>, age) </span><span class="keyword">values</span><span>(‘小明’,3) </span></span></li></ol><div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><pre code_snippet_id="1922727" snippet_file_name="blog_20170215_5_5850245" class="sql" name="code" style="display: none;">insert into person(name, age) values(‘小明’,3)</pre><p>更新语句:</p><p>update 表名 set 字段名=值 where 条件子句。如:</p><div class="dp-highlighter bg_sql"><div class="bar"><div class="tools"><b>[sql]</b> <a href="#" class="ViewSource" title="view plain" οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 288px; top: 1100px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_6" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_6" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=6&width=16&height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" οnclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-sql"><li class="alt"><span><span class="keyword">update</span><span> person </span><span class="keyword">set</span><span> </span><span class="keyword">name</span><span>=‘小明‘ </span><span class="keyword">where</span><span> id=10 </span></span></li></ol><div class="save_code tracking-ad" data-mod="popu_249" style="display: none;"><a href="javascript:;"><img src="http://static.blog.csdn.net/images/save_snippets_01.png"></a></div></div><pre code_snippet_id="1922727" snippet_file_name="blog_20170215_6_6515337" class="sql" name="code" style="display: none;">update person set name=‘小明‘ where id=10</pre><p>删除语句:</p><p>delete from 表名 where 条件子句。如:</p><div class="dp-highlighter bg_sql"><div class="bar"><div class="tools"><b>[sql]</b> <a href="#" class="ViewSource" title="view plain" οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 288px; top: 1234px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_7" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_7" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=7&width=16&height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" οnclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-sql"><li class="alt"><span><span class="keyword">delete</span><span> </span><span class="keyword">from</span><span> person </span><span class="keyword">where</span><span> id=10 </span></span></li></ol><div class="save_code tracking-ad" data-mod="popu_249" style="display: none;"><a href="javascript:;"><img src="http://static.blog.csdn.net/images/save_snippets_01.png"></a></div></div><pre code_snippet_id="1922727" snippet_file_name="blog_20161011_7_7132045" class="sql" name="code" style="display: none;">delete from person where id=10 </pre><p> </p><p>2、使用<span style="font-family:Times New Roman">SQLiteOpenHelper</span><span style="font-family:宋体">对数据库进行版本管理</span></p><p>我们在编写数据库应用软件时,需要考虑这样的问题:因为我们开发的软件可能会安装在很多用户的手机上,如果应用使用到了SQLite数据库,我们必须在用户初次使用软件时创建出应用使用到的数据库表结构及添加一些初始化记录,另外在软件升级的时候,也需要对数据表结构进行更新。那么,我们如何才能实现在用户初次使用或升级软件时自动在用户的手机上创建出应用需要的数据库表呢?总不能让我们在每个需要安装此软件的手机上通过手工方式创建数据库表吧?因为这种需求是每个数据库应用都要面临的,所以在android系统,为我们提供了一个名为SQLiteOpenHelper的抽象类,必须继承它才能使用,它是通过对数据库版本进行管理来实现前面提出的需求。 </p><p>为了实现对数据库版本进行管理,SQLiteOpenHelper类提供了两个重要的方法,分别是onCreate(SQLiteDatabase db)和onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion),前者用于初次使用软件时生成数据库表,后者用于升级软件时更新数据库表结构。当调用SQLiteOpenHelper的getWritableDatabase()或者getReadableDatabase()方法获取用于操作数据库的SQLiteDatabase实例的时候,如果数据库不存在,Android系统会自动生成一个数据库,接着调用onCreate()方法,onCreate()方法在初次生成数据库时才会被调用,在onCreate()方法里可以生成数据库表结构及添加一些应用使用到的初始化数据。onUpgrade()方法在数据库的版本发生变化时会被调用,一般在软件升级时才需改变版本号,而数据库的版本是由程序员控制的,假设数据库现在的版本是1,由于业务的变更,修改了数据库表结构,这时候就需要升级软件,升级软件时希望更新用户手机里的数据库表结构,为了实现这一目的,可以把原来的数据库版本设置为2(有同学问设置为3行不行?当然可以,如果你愿意,设置为100也行),并且在onUpgrade()方法里面实现表结构的更新。当软件的版本升级次数比较多,这时在onUpgrade()方法里面可以根据原版号和目标版本号进行判断,然后作出相应的表结构及数据更新。</p><p>getWritableDatabase()和getReadableDatabase()方法都可以获取一个用于操作数据库的SQLiteDatabase实例。但getWritableDatabase() 方法以读写方式打开数据库,一旦数据库的磁盘空间满了,数据库就只能读而不能写,倘若使用getWritableDatabase()打开数据库就会出错。getReadableDatabase()方法先以读写方式打开数据库,如果数据库的磁盘空间满了,就会打开失败,当打开失败后会继续尝试以只读方式打开数据库。</p><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 295px; top: 1872px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_8" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_8" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=8&width=16&height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" οnclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-j"><li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> DatabaseHelper </span><span class="keyword">extends</span><span> SQLiteOpenHelper </span></span></li><li class=""><span> </span></li><li class="alt"><span> <span class="comment">//类没有实例化,是不能用作父类构造器的参数,必须声明为静态</span><span> </span></span></li><li class=""><span> </span></li><li class="alt"><span> <span class="keyword">private</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">final</span><span> String name = </span><span class="string">"count"</span><span>; </span><span class="comment">//数据库名称</span><span> </span></span></li><li class=""><span> </span></li><li class="alt"><span> <span class="keyword">private</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">final</span><span> </span><span class="keyword">int</span><span> version = </span><span class="number">1</span><span>; </span><span class="comment">//数据库版本</span><span> </span></span></li><li class=""><span> </span></li><li class="alt"><span> <span class="keyword">public</span><span> DatabaseHelper(Context context) </span></span></li><li class=""><span> </span></li><li class="alt"><span> <span class="comment">//第三个参数CursorFactory指定在执行查询时获得一个游标实例的工厂类,设置为null,代表使用系统默认的工厂类</span><span> </span></span></li><li class=""><span> </span></li><li class="alt"><span> <span class="keyword">super</span><span>(context, name, </span><span class="keyword">null</span><span>, version); </span></span></li><li class=""><span> </span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="annotation">@Override</span><span> </span></span></li><li class=""><span> <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> onCreate(SQLiteDatabase db) </span></span></li><li class="alt"><span> </span></li><li class=""><span> db.execSQL(<span class="string">"CREATE TABLE IF NOT EXISTS person (personid integer primary key autoincrement, name varchar(20), age INTEGER)"</span><span>); </span></span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span> </span></li><li class=""><span> <span class="annotation">@Override</span><span> </span></span></li><li class="alt"><span> <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> onUpgrade(SQLiteDatabase db, </span><span class="keyword">int</span><span> oldVersion, </span><span class="keyword">int</span><span> newVersion) </span></span></li><li class=""><span> </span></li><li class="alt"><span> db.execSQL(<span class="string">"ALTER TABLE person ADD phone VARCHAR(12)"</span><span>); </span><span class="comment">//往表中增加一列</span><span> </span></span></li><li class=""><span> </span></li><li class="alt"><span> </span></li><li class=""><span> </span></li></ol><div class="save_code tracking-ad" data-mod="popu_249" style="display: none;"><a href="javascript:;"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><pre code_snippet_id="1922727" snippet_file_name="blog_20161011_8_1145208" class="java" name="code" style="display: none;">public class DatabaseHelper extends SQLiteOpenHelper
//类没有实例化,是不能用作父类构造器的参数,必须声明为静态
private static final String name = "count"; //数据库名称
private static final int version = 1; //数据库版本
public DatabaseHelper(Context context)
//第三个参数CursorFactory指定在执行查询时获得一个游标实例的工厂类,设置为null,代表使用系统默认的工厂类
super(context, name, null, version);
@Override public void onCreate(SQLiteDatabase db)
db.execSQL("CREATE TABLE IF NOT EXISTS person (personid integer primary key autoincrement, name varchar(20), age INTEGER)");
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
db.execSQL("ALTER TABLE person ADD phone VARCHAR(12)"); //往表中增加一列
</pre><p>在实际项目开发中,当数据库表结构发生更新时,应该避免用户存放于数据库中的数据丢失。</p><p> </p><p>3、使用<span style="font-family:Times New Roman">SQLiteDatabase</span><span style="font-family:宋体">操作</span><span style="font-family:Times New Roman">SQLite</span><span style="font-family:宋体">数据库</span></p><p>Android提供了一个名为SQLiteDatabase的类,该类封装了一些操作数据库的API,使用该类可以完成对数据进行添加(Create)、查询(Retrieve)、更新(Update)和删除(Delete)操作(这些操作简称为CRUD)。对SQLiteDatabase的学习,我们应该重点掌握execSQL()和rawQuery()方法。 execSQL()方法可以执行insert、delete、update和CREATE TABLE之类有更改行为的SQL语句; rawQuery()方法用于执行select语句。</p><p>execSQL()方法的使用例子:</p><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 295px; top: 2679px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_9" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_9" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=9&width=16&height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" οnclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-j"><li class="alt"><span><span>SQLiteDatabase db = ....; </span></span></li><li class=""><span> </span></li><li class="alt"><span>db.execSQL(<span class="string">"insert into person(name, age) values('炸死特', 4)"</span><span>); </span></span></li><li class=""><span> </span></li><li class="alt"><span>db.close(); </span></li></ol><div class="save_code tracking-ad" data-mod="popu_249" style="display: none;"><a href="javascript:;"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><pre code_snippet_id="1922727" snippet_file_name="blog_20161011_9_3621225" class="java" name="code" style="display: none;">SQLiteDatabase db = ....;
db.execSQL("insert into person(name, age) values('炸死特', 4)");
db.close();
</pre><p>执行上面SQL语句会往person表中添加进一条记录,在实际应用中, 语句中的“炸死特”这些参数值会由用户输入界面提供,如果把用户输入的内容原样组拼到上面的insert语句, 当用户输入的内容含有单引号时,组拼出来的SQL语句就会存在语法错误。要解决这个问题需要对单引号进行转义,也就是把单引号转换成两个单引号。有些时候用户往往还会输入像“ & ”这些特殊SQL符号,为保证组拼好的SQL语句语法正确,必须对SQL语句中的这些特殊SQL符号都进行转义,显然,对每条SQL语句都做这样的处理工作是比较烦琐的。 SQLiteDatabase类提供了一个重载后的execSQL(String sql, Object[] bindArgs)方法,使用这个方法可以解决前面提到的问题,因为这个方法支持使用占位符参数(?)。</p><p>例子如下:</p><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 295px; top: 3026px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_10" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_10" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=10&width=16&height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" οnclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-j"><li class="alt"><span><span>SQLiteDatabase db = ....; </span></span></li><li class=""><span> </span></li><li class="alt"><span>db.execSQL(<span class="string">"insert into person(name, age) values(?,?)"</span><span>, </span><span class="keyword">new</span><span> Object[]</span><span class="string">"炸死特"</span><span>, </span><span class="number">4</span><span>); </span></span></li><li class=""><span> </span></li><li class="alt"><span>db.close(); </span></li></ol><div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><pre code_snippet_id="1922727" snippet_file_name="blog_20161011_10_8223031" class="java" name="code" style="display: none;">SQLiteDatabase db = ....;
db.execSQL("insert into person(name, age) values(?,?)", new Object[]"炸死特", 4);
db.close();</pre><p>execSQL(String sql, Object[] bindArgs)方法的第一个参数为SQL语句,第二个参数为SQL语句中占位符参数的值,参数值在数组中的顺序要和占位符的位置对应。</p><p>SQLiteDatabase的rawQuery() 用于执行select语句,使用例子如下:<br></p><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 295px; top: 3254px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_11" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_11" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=11&width=16&height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" οnclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-j"><li class="alt"><span><span>SQLiteDatabase db = ....; </span></span></li><li class=""><span> </span></li><li class="alt"><span>Cursor cursor = db.rawQuery(“select * from person”, <span class="keyword">null</span><span>); </span></span></li><li class=""><span> </span></li><li class="alt"><span><span class="keyword">while</span><span> (cursor.moveToNext()) </span></span></li><li class=""><span> </span></li><li class="alt"><span> <span class="keyword">int</span><span> personid = cursor.getInt(</span><span class="number">0</span><span>); </span><span class="comment">//获取第一列的值,第一列的索引从0开始</span><span> </span></span></li><li class=""><span> </span></li><li class="alt"><span> String name = cursor.getString(<span class="number">1</span><span>);</span><span class="comment">//获取第二列的值</span><span> </span></span></li><li class=""><span> </span></li><li class="alt"><span> <span class="keyword">int</span><span> age = cursor.getInt(</span><span class="number">2</span><span>);</span><span class="comment">//获取第三列的值</span><span> </span></span></li><li class=""><span> </span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span>cursor.close(); </span></li><li class=""><span> </span></li><li class="alt"><span>db.close(); </span></li></ol><div class="save_code tracking-ad" data-mod="popu_249" style="display: none;"><a href="javascript:;"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><pre code_snippet_id="1922727" snippet_file_name="blog_20161011_11_5028639" class="java" name="code" style="display: none;">SQLiteDatabase db = ....;
Cursor cursor = db.rawQuery(“select * from person”, null);
while (cursor.moveToNext())
int personid = cursor.getInt(0); //获取第一列的值,第一列的索引从0开始
String name = cursor.getString(1);//获取第二列的值
int age = cursor.getInt(2);//获取第三列的值
cursor.close();
db.close(); </pre><p>rawQuery()方法的第一个参数为select语句;第二个参数为select语句中占位符参数的值,如果select语句没有使用占位符,该参数可以设置为null。带占位符参数的select语句使用例子如下:</p><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 295px; top: 3671px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_12" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_12" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=12&width=16&height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" οnclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-j"><li class="alt"><span><span>Cursor cursor = db.rawQuery(</span><span class="string">"select * from person where name like ? and age=?"</span><span>, </span><span class="keyword">new</span><span> String[]</span><span class="string">"%炸死特%"</span><span>, </span><span class="string">"4"</span><span>); </span></span></li></ol><div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><pre code_snippet_id="1922727" snippet_file_name="blog_20161011_12_1834246" class="java" name="code" style="display: none;">Cursor cursor = db.rawQuery("select * from person where name like ? and age=?", new String[]"%炸死特%", "4"); </pre><p>Cursor是结果集游标,用于对结果集进行随机访问,如果大家熟悉jdbc, 其实Cursor与JDBC中的ResultSet作用很相似。使用moveToNext()方法可以将游标从当前行移动到下一行,如果已经移过了结果集的最后一行,返回结果为false,否则为true。另外Cursor 还有常用的moveToPrevious()方法(用于将游标从当前行移动到上一行,如果已经移过了结果集的第一行,返回值为false,否则为true )、moveToFirst()方法(用于将游标移动到结果集的第一行,如果结果集为空,返回值为false,否则为true )和moveToLast()方法(用于将游标移动到结果集的最后一行,如果结果集为空,返回值为false,否则为true ) 。</p><p>除了前面给大家介绍的execSQL()和rawQuery()方法, SQLiteDatabase还专门提供了对应于添加、删除、更新、查询的操作方法: insert()、delete()、update()和query() 。这些方法实际上是给那些不太了解SQL语法的菜鸟使用的,对于熟悉SQL语法的程序员而言,直接使用execSQL()和rawQuery()方法执行SQL语句就能完成数据的添加、删除、更新、查询操作。</p><p>Insert()方法用于添加数据,各个字段的数据使用ContentValues进行存放。 ContentValues类似于MAP,相对于MAP,它提供了存取数据对应的put(String key, Xxx value)和getAsXxx(String key)方法, key为字段名称,value为字段值,Xxx指的是各种常用的数据类型,如:String、Integer等。</p><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 295px; top: 4069px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_13" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_13" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=13&width=16&height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" οnclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-j"><li class="alt"><span><span>SQLiteDatabase db = databaseHelper.getWritableDatabase(); </span></span></li><li class=""><span> </span></li><li class="alt"><span>ContentValues values = <span class="keyword">new</span><span> ContentValues(); </span></span></li><li class=""><span> </span></li><li class="alt"><span>values.put(<span class="string">"name"</span><span>, </span><span class="string">"炸死特"</span><span>); </span></span></li><li class=""><span> </span></li><li class="alt"><span>values.put(<span class="string">"age"</span><span>, </span><span class="number">4</span><span>); </span></span></li><li class=""><span> </span></li><li class="alt"><span><span class="keyword">long</span><span> rowid = db.insert(“person”, </span><span class="keyword">null</span><span>, values);</span><span class="comment">//返回新添记录的行号,与主键id无关</span><span> </span></span></li></ol><div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><pre code_snippet_id="1922727" snippet_file_name="blog_20161011_13_6688005" class="java" name="code" style="display: none;">SQLiteDatabase db = databaseHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", "炸死特");
values.put("age", 4);
long rowid = db.insert(“person”, null, values);//返回新添记录的行号,与主键id无关</pre><p>不管第三个参数是否包含数据,执行Insert()方法必然会添加一条记录,如果第三个参数为空,会添加一条除主键之外其他字段值为Null的记录。Insert()方法内部实际上通过构造insert SQL语句完成数据的添加,Insert()方法的第二个参数用于指定空值字段的名称,相信大家对该参数会感到疑惑,该参数的作用是什么?</p><p>是这样的:如果
以上是关于android之存储篇_SQLite数据库_让你彻底学会SQLite的使用的主要内容,如果未能解决你的问题,请参考以下文章
android之存储篇_SQLite数据库_让你彻底学会SQLite的使用