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>&nbsp;但有一种情况例外:定义为INTEGER&nbsp;PRIMARY&nbsp;KEY的字段只能存储64位整数,&nbsp;当向这种字段保存除整数以外的数据时,将会产生错误。&nbsp;</p><p>另外,&nbsp;SQLite&nbsp;在解析CREATE&nbsp;TABLE&nbsp;语句时,会忽略&nbsp;CREATE&nbsp;TABLE&nbsp;语句中跟在字段名后面的数据类型信息,如下面语句会忽略&nbsp;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&amp;width=16&amp;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>&nbsp;</span><span class="keyword">TABLE</span><span>&nbsp;person&nbsp;(personid&nbsp;</span><span class="keyword">integer</span><span>&nbsp;</span><span class="keyword">primary</span><span>&nbsp;</span><span class="keyword">key</span><span>&nbsp;autoincrement,&nbsp;</span><span class="keyword">name</span><span>&nbsp;</span><span class="keyword">varchar</span><span>(20))&nbsp;&nbsp;</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&nbsp;*&nbsp;from&nbsp;表名&nbsp;where&nbsp;条件子句&nbsp;group&nbsp;by&nbsp;分组字句&nbsp;having&nbsp;...&nbsp;order&nbsp;by&nbsp;排序子句 (顺序一定不能错)</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&amp;width=16&amp;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>&nbsp;*&nbsp;</span><span class="keyword">from</span><span>&nbsp;person&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span><span class="keyword">select</span><span>&nbsp;*&nbsp;</span><span class="keyword">from</span><span>&nbsp;person&nbsp;</span><span class="keyword">order</span><span>&nbsp;</span><span class="keyword">by</span><span>&nbsp;id&nbsp;</span><span class="keyword">desc</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span><span class="keyword">select</span><span>&nbsp;</span><span class="keyword">name</span><span>&nbsp;</span><span class="keyword">from</span><span>&nbsp;person&nbsp;</span><span class="keyword">group</span><span>&nbsp;</span><span class="keyword">by</span><span>&nbsp;</span><span class="keyword">name</span><span>&nbsp;</span><span class="keyword">having</span><span>&nbsp;</span><span class="func">count</span><span>(*)&gt;1&nbsp;&nbsp;</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 person

        select * from person order by id desc

        select name from person group by name having count(*)&gt;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&amp;width=16&amp;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>&nbsp;*&nbsp;</span><span class="keyword">from</span><span>&nbsp;Account&nbsp;limit&nbsp;5&nbsp;offset&nbsp;3&nbsp;&nbsp;&nbsp;</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&amp;width=16&amp;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>&nbsp;*&nbsp;</span><span class="keyword">from</span><span>&nbsp;Account&nbsp;limit&nbsp;3,5&nbsp;&nbsp;</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&nbsp;into&nbsp;表名(字段列表)&nbsp;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&amp;width=16&amp;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>&nbsp;</span><span class="keyword">into</span><span>&nbsp;person(</span><span class="keyword">name</span><span>,&nbsp;age)&nbsp;</span><span class="keyword">values</span><span>(‘小明’,3)&nbsp;&nbsp;</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&nbsp;表名&nbsp;set&nbsp;字段名=值&nbsp;where&nbsp;条件子句。如:</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&amp;width=16&amp;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>&nbsp;person&nbsp;</span><span class="keyword">set</span><span>&nbsp;</span><span class="keyword">name</span><span>=‘小明‘&nbsp;</span><span class="keyword">where</span><span>&nbsp;id=10&nbsp;&nbsp;</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&nbsp;from&nbsp;表名&nbsp;where&nbsp;条件子句。如:</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&amp;width=16&amp;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>&nbsp;</span><span class="keyword">from</span><span>&nbsp;person&nbsp;&nbsp;</span><span class="keyword">where</span><span>&nbsp;id=10&nbsp;&nbsp;</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>&nbsp;</p><p>2、使用<span style="font-family:Times New Roman">SQLiteOpenHelper</span><span style="font-family:宋体">对数据库进行版本管理</span></p><p>我们在编写数据库应用软件时,需要考虑这样的问题:因为我们开发的软件可能会安装在很多用户的手机上,如果应用使用到了SQLite数据库,我们必须在用户初次使用软件时创建出应用使用到的数据库表结构及添加一些初始化记录,另外在软件升级的时候,也需要对数据表结构进行更新。那么,我们如何才能实现在用户初次使用或升级软件时自动在用户的手机上创建出应用需要的数据库表呢?总不能让我们在每个需要安装此软件的手机上通过手工方式创建数据库表吧?因为这种需求是每个数据库应用都要面临的,所以在Android系统,为我们提供了一个名为SQLiteOpenHelper的抽象类,必须继承它才能使用,它是通过对数据库版本进行管理来实现前面提出的需求。&nbsp;</p><p>为了实现对数据库版本进行管理,SQLiteOpenHelper类提供了两个重要的方法,分别是onCreate(SQLiteDatabase&nbsp;db)和onUpgrade(SQLiteDatabase&nbsp;db,&nbsp;int&nbsp;oldVersion,&nbsp;int&nbsp;newVersion),前者用于初次使用软件时生成数据库表,后者用于升级软件时更新数据库表结构。当调用SQLiteOpenHelper的getWritableDatabase()或者getReadableDatabase()方法获取用于操作数据库的SQLiteDatabase实例的时候,如果数据库不存在,Android系统会自动生成一个数据库,接着调用onCreate()方法,onCreate()方法在初次生成数据库时才会被调用,在onCreate()方法里可以生成数据库表结构及添加一些应用使用到的初始化数据。onUpgrade()方法在数据库的版本发生变化时会被调用,一般在软件升级时才需改变版本号,而数据库的版本是由程序员控制的,假设数据库现在的版本是1,由于业务的变更,修改了数据库表结构,这时候就需要升级软件,升级软件时希望更新用户手机里的数据库表结构,为了实现这一目的,可以把原来的数据库版本设置为2(有同学问设置为3行不行?当然可以,如果你愿意,设置为100也行),并且在onUpgrade()方法里面实现表结构的更新。当软件的版本升级次数比较多,这时在onUpgrade()方法里面可以根据原版号和目标版本号进行判断,然后作出相应的表结构及数据更新。</p><p>getWritableDatabase()和getReadableDatabase()方法都可以获取一个用于操作数据库的SQLiteDatabase实例。但getWritableDatabase()&nbsp;方法以读写方式打开数据库,一旦数据库的磁盘空间满了,数据库就只能读而不能写,倘若使用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&amp;width=16&amp;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>&nbsp;</span><span class="keyword">class</span><span>&nbsp;DatabaseHelper&nbsp;</span><span class="keyword">extends</span><span>&nbsp;SQLiteOpenHelper&nbsp;{&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//类没有实例化,是不能用作父类构造器的参数,必须声明为静态</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">private</span><span>&nbsp;</span><span class="keyword">static</span><span>&nbsp;</span><span class="keyword">final</span><span>&nbsp;String&nbsp;name&nbsp;=&nbsp;</span><span class="string">"count"</span><span>;&nbsp;</span><span class="comment">//数据库名称</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">private</span><span>&nbsp;</span><span class="keyword">static</span><span>&nbsp;</span><span class="keyword">final</span><span>&nbsp;</span><span class="keyword">int</span><span>&nbsp;version&nbsp;=&nbsp;</span><span class="number">1</span><span>;&nbsp;</span><span class="comment">//数据库版本</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;DatabaseHelper(Context&nbsp;context)&nbsp;{&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//第三个参数CursorFactory指定在执行查询时获得一个游标实例的工厂类,设置为null,代表使用系统默认的工厂类</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">super</span><span>(context,&nbsp;name,&nbsp;</span><span class="keyword">null</span><span>,&nbsp;version);&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="annotation">@Override</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;onCreate(SQLiteDatabase&nbsp;db)&nbsp;{&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db.execSQL(<span class="string">"CREATE&nbsp;TABLE&nbsp;IF&nbsp;NOT&nbsp;EXISTS&nbsp;person&nbsp;(personid&nbsp;integer&nbsp;primary&nbsp;key&nbsp;autoincrement,&nbsp;name&nbsp;varchar(20),&nbsp;age&nbsp;INTEGER)"</span><span>);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="annotation">@Override</span><span>&nbsp;&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;onUpgrade(SQLiteDatabase&nbsp;db,&nbsp;</span><span class="keyword">int</span><span>&nbsp;oldVersion,&nbsp;</span><span class="keyword">int</span><span>&nbsp;newVersion)&nbsp;{&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db.execSQL(<span class="string">"ALTER&nbsp;TABLE&nbsp;person&nbsp;ADD&nbsp;phone&nbsp;VARCHAR(12)"</span><span>);&nbsp;</span><span class="comment">//往表中增加一列</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class=""><span>}&nbsp;&nbsp;</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>&nbsp;</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()方法。&nbsp;execSQL()方法可以执行insert、delete、update和CREATE&nbsp;TABLE之类有更改行为的SQL语句;&nbsp;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&amp;width=16&amp;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&nbsp;db&nbsp;=&nbsp;....;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>db.execSQL(<span class="string">"insert&nbsp;into&nbsp;person(name,&nbsp;age)&nbsp;values('炸死特',&nbsp;4)"</span><span>);&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>db.close();&nbsp;&nbsp;</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表中添加进一条记录,在实际应用中,&nbsp;语句中的“炸死特”这些参数值会由用户输入界面提供,如果把用户输入的内容原样组拼到上面的insert语句,&nbsp;当用户输入的内容含有单引号时,组拼出来的SQL语句就会存在语法错误。要解决这个问题需要对单引号进行转义,也就是把单引号转换成两个单引号。有些时候用户往往还会输入像“&nbsp;&amp;&nbsp;”这些特殊SQL符号,为保证组拼好的SQL语句语法正确,必须对SQL语句中的这些特殊SQL符号都进行转义,显然,对每条SQL语句都做这样的处理工作是比较烦琐的。&nbsp;SQLiteDatabase类提供了一个重载后的execSQL(String&nbsp;sql,&nbsp;Object[]&nbsp;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&amp;width=16&amp;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&nbsp;db&nbsp;=&nbsp;....;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>db.execSQL(<span class="string">"insert&nbsp;into&nbsp;person(name,&nbsp;age)&nbsp;values(?,?)"</span><span>,&nbsp;</span><span class="keyword">new</span><span>&nbsp;Object[]{</span><span class="string">"炸死特"</span><span>,&nbsp;</span><span class="number">4</span><span>});&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>db.close();&nbsp;&nbsp;</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&nbsp;sql,&nbsp;Object[]&nbsp;bindArgs)方法的第一个参数为SQL语句,第二个参数为SQL语句中占位符参数的值,参数值在数组中的顺序要和占位符的位置对应。</p><p>SQLiteDatabase的rawQuery()&nbsp;用于执行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&amp;width=16&amp;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&nbsp;db&nbsp;=&nbsp;....;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>Cursor&nbsp;cursor&nbsp;=&nbsp;db.rawQuery(“select&nbsp;*&nbsp;from&nbsp;person”,&nbsp;<span class="keyword">null</span><span>);&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span><span class="keyword">while</span><span>&nbsp;(cursor.moveToNext())&nbsp;{&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;<span class="keyword">int</span><span>&nbsp;personid&nbsp;=&nbsp;cursor.getInt(</span><span class="number">0</span><span>);&nbsp;</span><span class="comment">//获取第一列的值,第一列的索引从0开始</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;String&nbsp;name&nbsp;=&nbsp;cursor.getString(<span class="number">1</span><span>);</span><span class="comment">//获取第二列的值</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;<span class="keyword">int</span><span>&nbsp;age&nbsp;=&nbsp;cursor.getInt(</span><span class="number">2</span><span>);</span><span class="comment">//获取第三列的值</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>}&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>cursor.close();&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>db.close();&nbsp;&nbsp;</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&amp;width=16&amp;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&nbsp;cursor&nbsp;=&nbsp;db.rawQuery(</span><span class="string">"select&nbsp;*&nbsp;from&nbsp;person&nbsp;where&nbsp;name&nbsp;like&nbsp;?&nbsp;and&nbsp;age=?"</span><span>,&nbsp;</span><span class="keyword">new</span><span>&nbsp;String[]{</span><span class="string">"%炸死特%"</span><span>,&nbsp;</span><span class="string">"4"</span><span>});&nbsp;&nbsp;</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,&nbsp;其实Cursor与JDBC中的ResultSet作用很相似。使用moveToNext()方法可以将游标从当前行移动到下一行,如果已经移过了结果集的最后一行,返回结果为false,否则为true。另外Cursor&nbsp;还有常用的moveToPrevious()方法(用于将游标从当前行移动到上一行,如果已经移过了结果集的第一行,返回值为false,否则为true&nbsp;)、moveToFirst()方法(用于将游标移动到结果集的第一行,如果结果集为空,返回值为false,否则为true&nbsp;)和moveToLast()方法(用于将游标移动到结果集的最后一行,如果结果集为空,返回值为false,否则为true&nbsp;)&nbsp;。</p><p>除了前面给大家介绍的execSQL()和rawQuery()方法,&nbsp;SQLiteDatabase还专门提供了对应于添加、删除、更新、查询的操作方法:&nbsp;insert()、delete()、update()和query()&nbsp;。这些方法实际上是给那些不太了解SQL语法的菜鸟使用的,对于熟悉SQL语法的程序员而言,直接使用execSQL()和rawQuery()方法执行SQL语句就能完成数据的添加、删除、更新、查询操作。</p><p>Insert()方法用于添加数据,各个字段的数据使用ContentValues进行存放。&nbsp;ContentValues类似于MAP,相对于MAP,它提供了存取数据对应的put(String&nbsp;key,&nbsp;Xxx&nbsp;value)和getAsXxx(String&nbsp;key)方法,&nbsp;&nbsp;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&amp;width=16&amp;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&nbsp;db&nbsp;=&nbsp;databaseHelper.getWritableDatabase();&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>ContentValues&nbsp;values&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;ContentValues();&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>values.put(<span class="string">"name"</span><span>,&nbsp;</span><span class="string">"炸死特"</span><span>);&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>values.put(<span class="string">"age"</span><span>,&nbsp;</span><span class="number">4</span><span>);&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span><span class="keyword">long</span><span>&nbsp;rowid&nbsp;=&nbsp;db.insert(“person”,&nbsp;</span><span class="keyword">null</span><span>,&nbsp;values);</span><span class="comment">//返回新添记录的行号,与主键id无关</span><span>&nbsp;&nbsp;</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&nbsp;SQL语句完成数据的添加,Insert()方法的第二个参数用于指定空值字段的名称,相信大家对该参数会感到疑惑,该参数的作用是什么?</p><p>是这样的:如果第三个参数values&nbsp;为Null或者元素个数为0,&nbsp;由于Insert()方法要求必须添加一条除了主键之外其它字段为Null值的记录,为了满足SQL语法的需要,&nbsp;insert语句必须给定一个字段名,如:insert&nbsp;into&nbsp;person(name)&nbsp;values(NULL),倘若不给定字段名&nbsp;,&nbsp;insert语句就成了这样:&nbsp;insert&nbsp;into&nbsp;person()&nbsp;values(),显然这不满足标准SQL的语法。对于字段名,建议使用主键之外的字段,如果使用了INTEGER类型的主键字段,执行类似insert&nbsp;into&nbsp;person(personid)&nbsp;values(NULL)的insert语句后,该主键字段值也不会为NULL。如果第三个参数values&nbsp;不为Null并且元素的个数大于0&nbsp;,可以把第二个参数设置为null。</p><p>关于此处insert的更加详细讲解在这里<a target="_blank" href="http://blog.csdn.net/jason0539/article/details/9977175">http://blog.csdn.net/jason0539/article/details/9977175</a></p><p>delete()方法的使用:</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: 4559px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_14" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_14" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=14&amp;width=16&amp;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&nbsp;db&nbsp;=&nbsp;databaseHelper.getWritableDatabase();&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>db.delete(<span class="string">"person"</span><span>,&nbsp;</span><span class="string">"personid&lt;?"</span><span>,&nbsp;</span><span class="keyword">new</span><span>&nbsp;String[]{</span><span class="string">"2"</span><span>});&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>db.close();&nbsp;&nbsp;</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_14_9905562" class="java" name="code" style="display: none;">SQLiteDatabase db = databaseHelper.getWritableDatabase();

db.delete("person", "personid&lt;?", new String[]{"2"});

db.close();
</pre><p>上面代码用于从person表中删除personid小于2的记录。</p><p>update()方法的使用:</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: 4763px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_15" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_15" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=15&amp;width=16&amp;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&nbsp;db&nbsp;=&nbsp;databaseHelper.getWritableDatabase();&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>ContentValues&nbsp;values&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;ContentValues();&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>values.put(“name”,&nbsp;“炸死特”);<span class="comment">//key为字段名,value为值</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>db.update(<span class="string">"person"</span><span>,&nbsp;values,&nbsp;</span><span class="string">"personid=?"</span><span>,&nbsp;</span><span class="keyword">new</span><span>&nbsp;String[]{</span><span class="string">"1"</span><span>});&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>db.close();&nbsp;&nbsp;</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_15_9535068" class="java" name="code" style="display: none;">SQLiteDatabase db = databaseHelper.getWritableDatabase();

ContentValues values = new ContentValues();

values.put(“name”, “炸死特”);//key为字段名,value为值

db.update("person", values, "personid=?", new String[]{"1"}); 

db.close();
</pre><p>上面代码用于把person表中personid等于1的记录的name字段的值改为“炸死特”。</p><p>query()方法</p><p>实际上是把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: 5062px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_16" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_16" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=16&amp;width=16&amp;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&nbsp;db&nbsp;=&nbsp;databaseHelper.getWritableDatabase();&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>Cursor&nbsp;cursor&nbsp;=&nbsp;db.query(<span class="string">"person"</span><span>,&nbsp;</span><span class="keyword">new</span><span>&nbsp;String[]{</span><span class="string">"personid,name,age"</span><span>},&nbsp;</span><span class="string">"name&nbsp;like&nbsp;?"</span><span>,&nbsp;</span><span class="keyword">new</span><span>&nbsp;String[]{</span><span class="string">"%炸死特%"</span><span>},&nbsp;</span><span class="keyword">null</span><span>,&nbsp;</span><span class="keyword">null</span><span>,&nbsp;</span><span class="string">"personid&nbsp;desc"</span><span>,&nbsp;</span><span class="string">"1,2"</span><span>);&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span><span class="keyword">while</span><span>&nbsp;(cursor.moveToNext())&nbsp;{&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">int</span><span>&nbsp;personid&nbsp;=&nbsp;cursor.getInt(</span><span class="number">0</span><span>);&nbsp;</span><span class="comment">//获取第一列的值,第一列的索引从0开始</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;name&nbsp;=&nbsp;cursor.getString(<span class="number">1</span><span>);</span><span class="comment">//获取第二列的值</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">int</span><span>&nbsp;age&nbsp;=&nbsp;cursor.getInt(</span><span class="number">2</span><span>);</span><span class="comment">//获取第三列的值</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>}&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>cursor.close();&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>db.close();&nbsp;&nbsp;</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_16_7976879" class="java" name="code" style="display: none;">SQLiteDatabase db = databaseHelper.getWritableDatabase();

Cursor cursor = db.query("person", new String[]{"personid,name,age"}, "name like ?", new String[]{"%炸死特%"}, null, null, "personid desc", "1,2");

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>上面代码用于从person表中查找name字段含有“炸死特”的记录,匹配的记录按personid降序排序,对排序后的结果略过第一条记录,只获取2条记录。</p><p>query(table,&nbsp;columns,&nbsp;selection,&nbsp;selectionArgs,&nbsp;groupBy,&nbsp;having,&nbsp;orderBy,&nbsp;limit)方法各参数的含义:</p><p>table:表名。相当于select语句from关键字后面的部分。如果是多表联合查询,可以用逗号将两个表名分开。</p><p>columns:要查询出来的列名。相当于select语句select关键字后面的部分。</p><p>selection:查询条件子句,相当于select语句where关键字后面的部分,在条件子句允许使用占位符“?”</p><p>selectionArgs:对应于selection语句中占位符的值,值在数组中的位置与占位符在语句中的位置必须一致,否则就会有异常。</p><p>groupBy:相当于select语句group&nbsp;by关键字后面的部分</p><p>having:相当于select语句having关键字后面的部分</p><p>orderBy:相当于select语句order&nbsp;by关键字后面的部分,如:personid&nbsp;desc,&nbsp;age&nbsp;asc;</p><p>limit:指定偏移量和获取的记录数,相当于select语句limit关键字后面的部分。</p><p>&nbsp;</p><p>数据库实例:</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: 5810px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_17" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_17" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=17&amp;width=16&amp;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">package</span><span>&nbsp;jason.demo;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span><span class="keyword">import</span><span>&nbsp;android.content.Context;&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">import</span><span>&nbsp;android.content.ContentValues;&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="keyword">import</span><span>&nbsp;android.database.Cursor;&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">import</span><span>&nbsp;android.database.SQLException;&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="keyword">import</span><span>&nbsp;android.database.sqlite.SQLiteOpenHelper;&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">import</span><span>&nbsp;android.database.sqlite.SQLiteDatabase;&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="keyword">import</span><span>&nbsp;android.database.sqlite.SQLiteQueryBuilder;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span><span class="keyword">class</span><span>&nbsp;RestaurantHelper&nbsp;</span><span class="keyword">extends</span><span>&nbsp;SQLiteOpenHelper&nbsp;{&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">private</span><span>&nbsp;</span><span class="keyword">static</span><span>&nbsp;</span><span class="keyword">final</span><span>&nbsp;String&nbsp;DATABASE_NAME=</span><span class="string">"lunchlist.db"</span><span>;</span><span class="comment">//数据库名称</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">private</span><span>&nbsp;</span><span class="keyword">static</span><span>&nbsp;</span><span class="keyword">final</span><span>&nbsp;</span><span class="keyword">int</span><span>&nbsp;SCHEMA_VERSION=</span><span class="number">2</span><span>;</span><span class="comment">//版本号,则是升级之后的,升级方法请看onUpgrade方法里面的判断</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;RestaurantHelper(Context&nbsp;context)&nbsp;{</span><span class="comment">//构造函数,接收上下文作为参数,直接调用的父类的构造函数</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">super</span><span>(context,&nbsp;DATABASE_NAME,&nbsp;</span><span class="keyword">null</span><span>,&nbsp;SCHEMA_VERSION);&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="annotation">@Override</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;onCreate(SQLiteDatabase&nbsp;db)&nbsp;{</span><span class="comment">//创建的是一个午餐订餐的列表,id,菜名,地址等等</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db.execSQL(<span class="string">"CREATE&nbsp;TABLE&nbsp;restaurants&nbsp;(_id&nbsp;INTEGER&nbsp;PRIMARY&nbsp;KEY&nbsp;AUTOINCREMENT,&nbsp;name&nbsp;TEXT,&nbsp;address&nbsp;TEXT,&nbsp;type&nbsp;TEXT,&nbsp;notes&nbsp;TEXT,&nbsp;phone&nbsp;TEXT);"</span><span>);&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="annotation">@Override</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;onUpgrade(SQLiteDatabase&nbsp;db,&nbsp;</span><span class="keyword">int</span><span>&nbsp;oldVersion,&nbsp;</span><span class="keyword">int</span><span>&nbsp;newVersion)&nbsp;{&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">if</span><span>&nbsp;(oldVersion==</span><span class="number">1</span><span>&nbsp;&amp;&amp;&nbsp;newVersion==</span><span class="number">2</span><span>)&nbsp;{</span><span class="comment">//升级判断,如果再升级就要再加两个判断,从1到3,从2到3</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db.execSQL(<span class="string">"ALTER&nbsp;TABLE&nbsp;restaurants&nbsp;ADD&nbsp;phone&nbsp;TEXT;"</span><span>);&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;Cursor&nbsp;getAll(String&nbsp;where,&nbsp;String&nbsp;orderBy)&nbsp;{</span><span class="comment">//返回表中的数据,where是调用时候传进来的搜索内容,orderby是设置中传进来的列表排序类型</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StringBuilder&nbsp;buf=<span class="keyword">new</span><span>&nbsp;StringBuilder(</span><span class="string">"SELECT&nbsp;_id,&nbsp;name,&nbsp;address,&nbsp;type,&nbsp;notes,&nbsp;phone&nbsp;FROM&nbsp;restaurants"</span><span>);&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">if</span><span>&nbsp;(where!=</span><span class="keyword">null</span><span>)&nbsp;{&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buf.append(<span class="string">"&nbsp;WHERE&nbsp;"</span><span>);&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buf.append(where);&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">if</span><span>&nbsp;(orderBy!=</span><span class="keyword">null</span><span>)&nbsp;{&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buf.append(<span class="string">"&nbsp;ORDER&nbsp;BY&nbsp;"</span><span>);&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buf.append(orderBy);&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">return</span><span>(getReadableDatabase().rawQuery(buf.toString(),&nbsp;</span><span class="keyword">null</span><span>));&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;Cursor&nbsp;getById(String&nbsp;id)&nbsp;{</span><span class="comment">//根据点击事件获取id,查询数据库</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String[]&nbsp;args={id};&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">return</span><span>(getReadableDatabase()&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.rawQuery(<span class="string">"SELECT&nbsp;_id,&nbsp;name,&nbsp;address,&nbsp;type,&nbsp;notes,&nbsp;phone&nbsp;FROM&nbsp;restaurants&nbsp;WHERE&nbsp;_ID=?"</span><span>,&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;args));&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;insert(String&nbsp;name,&nbsp;String&nbsp;address,&nbsp;String&nbsp;type,&nbsp;String&nbsp;notes,&nbsp;String&nbsp;phone)&nbsp;{&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ContentValues&nbsp;cv=<span class="keyword">new</span><span>&nbsp;ContentValues();&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cv.put(<span class="string">"name"</span><span>,&nbsp;name);&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cv.put(<span class="string">"address"</span><span>,&nbsp;address);&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cv.put(<span class="string">"type"</span><span>,&nbsp;type);&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cv.put(<span class="string">"notes"</span><span>,&nbsp;notes);&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cv.put(<span class="string">"phone"</span><span>,&nbsp;phone);&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getWritableDatabase().insert(<span class="string">"restaurants"</span><span>,&nbsp;</span><span class="string">"name"</span><span>,&nbsp;cv);&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;update(String&nbsp;id,&nbsp;String&nbsp;name,&nbsp;String&nbsp;address,&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;type,&nbsp;String&nbsp;notes,&nbsp;String&nbsp;phone)&nbsp;{&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ContentValues&nbsp;cv=<span class="keyword">new</span><span>&nbsp;ContentValues();&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String[]&nbsp;args={id};&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cv.put(<span class="string">"name"</span><span>,&nbsp;name);&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cv.put(<span class="string">"address"</span><span>,&nbsp;address);&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cv.put(<span class="string">"type"</span><span>,&nbsp;type);&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cv.put(<span class="string">"notes"</span><span>,&nbsp;notes);&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cv.put(<span class="string">"phone"</span><span>,&nbsp;phone);&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getWritableDatabase().update(<span class="string">"restaurants"</span><span>,&nbsp;cv,&nbsp;</span><span class="string">"_ID=?"</span><span>,&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;args);&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;String&nbsp;getName(Cursor&nbsp;c)&nbsp;{&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">return</span><span>(c.getString(</span><span class="number">1</span><span>));&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;String&nbsp;getAddress(Cursor&nbsp;c)&nbsp;{&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">return</span><span>(c.getString(</span><span class="number">2</span><span>));&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;String&nbsp;getType(Cursor&nbsp;c)&nbsp;{&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">return</span><span>(c.getString(</span><span class="number">3</span><span>));&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;String&nbsp;getNotes(Cursor&nbsp;c)&nbsp;{&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">return</span><span>(c.getString(</span><span class="number">4</span><span>));&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;String&nbsp;getPhone(Cursor&nbsp;c)&nbsp;{&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">return</span><span>(c.getString(</span><span class="number">5</span><span>));&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="alt"><span>}&nbsp;&nbsp;</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_17_1194436" class="java" name="code" style="display: none;">package jason.demo;

import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;

class RestaurantHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="lunchlist.db";//数据库名称
private static final int SCHEMA_VERSION=2;//版本号,则是升级之后的,升级方法请看onUpgrade方法里面的判断
public RestaurantHelper(Context context) {//构造函数,接收上下文作为参数,直接调用的父类的构造函数
super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {//创建的是一个午餐订餐的列表,id,菜名,地址等等
db.execSQL("CREATE TABLE restaurants (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, address TEXT, type TEXT, notes TEXT, phone TEXT);");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion==1 &amp;&amp; newVersion==2) {//升级判断,如果再升级就要再加两个判断,从1到3,从2到3
db.execSQL("ALTER TABLE restaurants ADD phone TEXT;");
}
}

public Cursor getAll(String where, String orderBy) {//返回表中的数据,where是调用时候传进来的搜索内容,orderby是设置中传进来的列表排序类型
StringBuilder buf=new StringBuilder("SELECT _id, name, address, type, notes, phone FROM restaurants");
if (where!=null) {
buf.append(" WHERE ");
buf.append(where);
}
if (orderBy!=null) {
buf.append(" ORDER BY ");
buf.append(orderBy);
}
return(getReadableDatabase().rawQuery(buf.toString(), null));
}
public Cursor getById(String id) {//根据点击事件获取id,查询数据库
String[] args={id};

return(getReadableDatabase()
.rawQuery("SELECT _id, name, address, type, notes, phone FROM restaurants WHERE _ID=?",
args));
}
public void insert(String name, String address, String type, String notes, String phone) {
ContentValues cv=new ContentValues();
cv.put("name", name);
cv.put("address", address);
cv.put("type", type);
cv.put("notes", notes);
cv.put("phone", phone);
getWritableDatabase().insert("restaurants", "name", cv);
}
public void update(String id, String name, String address,
String type, String notes, String phone) {
ContentValues cv=new ContentValues();
String[] args={id};
cv.put("name", name);
cv.put("address", address);
cv.put("type", type);
cv.put("notes", notes);
cv.put("phone", phone);
getWritableDatabase().update("restaurants", cv, "_ID=?",
args);
}
public String getName(Cursor c) {
return(c.getString(1));
}
public String getAddress(Cursor c) {
return(c.getString(2));
}
public String getType(Cursor c) {
return(c.getString(3));
}
public String getNotes(Cursor c) {
return(c.getString(4));
}
public String getPhone(Cursor c) {
return(c.getString(5));
}
}</pre><p>&nbsp;</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: 7763px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_18" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_18" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=18&amp;width=16&amp;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>&nbsp;</span><span class="keyword">class</span><span>&nbsp;LunchList&nbsp;</span><span class="keyword">extends</span><span>&nbsp;ListActivity&nbsp;{&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;Cursor&nbsp;model=<span class="keyword">null</span><span>;</span><span class="comment">//用来存储查询的数据</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;RestaurantAdapter&nbsp;adapter=<span class="keyword">null</span><span>;</span><span class="comment">//上面自定义的数据库帮助类</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;onCreate(Bundle&nbsp;savedInstanceState)&nbsp;{&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;<span class="keyword">super</span><span>.onCreate(savedInstanceState);&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;setContentView(R.layout.main);&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;helper=<span class="keyword">new</span><span>&nbsp;RestaurantHelper(</span><span class="keyword">this</span><span>);</span><span class="comment">//参数是上下文</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;initList();<span class="comment">//初始化listview列表,省略了很多,大体能明白使用数据库流程就行啦</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li class=""><span>}&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li class=""><span><span class="keyword">private</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;initList()&nbsp;{&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;<span class="keyword">if</span><span>&nbsp;(model!=</span><span class="keyword">null</span><span>)&nbsp;{&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;stopManagingCursor(model);&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;model.close();&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;String&nbsp;where=<span class="keyword">null</span><span>;&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;<span class="keyword">if</span><span>&nbsp;(Intent.ACTION_SEARCH.equals(getIntent().getAction()))&nbsp;{</span><span class="comment">//如果有查询指令则根据查询内容从数据库获取表中所有满足条件数据</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;where=<span class="string">"name&nbsp;LIKE&nbsp;\"%"</span><span>+getIntent().getStringExtra(SearchManager.QUERY)+</span><span class="string">"%\""</span><span>;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;model=helper.getAll(where,&nbsp;prefs.getString(<span class="string">"sort_order"</span><span>,&nbsp;</span><span class="string">"name"</span><span>));</span><span class="comment">//调用帮助类中的方法获取数据</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;startManagingCursor(model);&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;adapter=<span class="keyword">new</span><span>&nbsp;RestaurantAdapter(model);</span><span class="comment">//设置adapter</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;setListAdapter(adapter);<span class="comment">//在列表中显示出来</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;}&nbsp;&nbsp;</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_18_6048195" class="java" name="code" style="display: none;">public class LunchList extends ListActivity {
 Cursor model=null;//用来存储查询的数据
 RestaurantAdapter adapter=null;//上面自定义的数据库帮助类
 public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   helper=new RestaurantHelper(this);//参数是上下文

   initList();//初始化listview列表,省略了很多,大体能明白使用数据库流程就行啦

}

private void initList() {
  if (model!=null) {
   stopManagingCursor(model);
   model.close();
  }
  
  String where=null;

  if (Intent.ACTION_SEARCH.equals(getIntent().getAction())) {//如果有查询指令则根据查询内容从数据库获取表中所有满足条件数据
   where="name LIKE \"%"+getIntent().getStringExtra(SearchManager.QUERY)+"%\"";
  }
  model=helper.getAll(where, prefs.getString("sort_order", "name"));//调用帮助类中的方法获取数据
  startManagingCursor(model);
  adapter=new RestaurantAdapter(model);//设置adapter
  setListAdapter(adapter);//在列表中显示出来
 }
</pre><p>这里只举例了查询,增删改操作都类似,只是用helper调用一下需要的方法就行了,参数传对就没有问题.</p><p>作者:jason0539</p><p>微博:<a target="_blank" href="http://weibo.com/2553717707" style="font-family:'WenQuanYi Micro Hei Mono','WenQuanYi Micro Hei','Microsoft Yahei Mono','Microsoft Yahei',sans-serif!important; color:rgb(106,57,6); text-decoration:none"><span style="font-family:'WenQuanYi Micro Hei Mono','WenQuanYi Micro Hei','Microsoft Yahei Mono','Microsoft Yahei',sans-serif!important">http://weibo.com/2553717707</span></a></p><p>博客:<a target="_blank" href="http://blog.csdn.net/jason0539" style="font-family:'WenQuanYi Micro Hei Mono','WenQuanYi Micro Hei','Microsoft Yahei Mono','Microsoft Yahei',sans-serif!important; color:rgb(106,57,6); text-decoration:none"><span style="font-family:'WenQuanYi Micro Hei Mono','WenQuanYi Micro Hei','Microsoft Yahei Mono','Microsoft Yahei',sans-serif!important">http://blog.csdn.net/jason0539</span></a>(转载请说明出处)</p><p><br></p><iframe width="100%" height="400" class="share_self" frameborder="0" scrolling="no" src="http://widget.weibo.com/weiboshow/index.php?language=&amp;width=0&amp;height=550&amp;fansRow=2&amp;ptype=1&amp;speed=0&amp;skin=5&amp;isTitle=1&amp;noborder=1&amp;isWeibo=1&amp;isFans=1&amp;uid=2553717707&amp;verifier=3201fa5c&amp;dpc=1"></iframe><br><link rel="stylesheet" href="http://static.blog.csdn.net/public/res-min/markdown_views.css?v=2.0">
            </div>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值