Android内置了
SQLite
数据库系统。
关于一些SQL基本命令的用法( CREATE,INSERT,UPDATE,DELETE 和 SELECT ),
请参阅SQLite文档( http://www.sqlite.org/lang.html<wbr style="line-height:25px">)<br style="line-height:25px"><span style="color:#003366; line-height:25px">与</span><span style="color:#993300; line-height:25px">SharedPreferences</span><span style="color:#003366; line-height:25px">不同,</span><span style="color:#ff6600; line-height:25px">不能</span><span style="color:#003366; line-height:25px">将数据库设为</span><span style="color:#ff6600; line-height:25px">WORLD_READABLE</span><span style="color:#003366; line-height:25px">,每个数据库都只能创建它的包访问,<br style="line-height:25px"> 这意味着只有由创建数据库的进程可访问它。如果需要在进程间传递数据,<br style="line-height:25px"> 则可以使用AIDL/Binder或创建一个</span><span style="color:#993300; line-height:25px">ContentProvider</span><span style="color:#003366; line-height:25px">,</span><span style="color:#000080; line-height:25px">但是不能跨越进程/包边界直接来使用数据库</span><span style="color:#003366; line-height:25px">。<br style="line-height:25px"> 数据库文件存放在</span><span style="color:#0000ff; line-height:25px">/data/data/[PACKAGE_NAME]/database/</span><span style="color:#003366; line-height:25px">下。这些文件是SQLite专有的。</span><br style="line-height:25px"> 可以通过ADBshell(sqlite3工具)来操作它。<br style="line-height:25px"> 详细命令可参考<br style="line-height:25px"><a target="_blank" rel="nofollow" href="http://www.sqlite.org/sqlite.html" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">http://www.sqlite.org/sqlite.html</a><wbr style="line-height:25px">。<br style="line-height:25px"><span style="line-height:25px">实例1</span>:<br style="line-height:25px"> DBHelper.java文件<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">packagecom.teleca;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.ArrayList;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.List;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.content.ContentValues;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.content.Context;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.database.Cursor;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.database.SQLException;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.database.sqlite.SQLiteDatabase;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.database.sqlite.SQLiteOpenHelper;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.util.Log;;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff00ff; line-height:25px">DBHelper</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicstatic</span><span style="color:#3366ff; line-height:25px">StringDB_NAME="</span><span style="color:#99cc00; line-height:25px">peopledb</span><span style="color:#3366ff; line-height:25px">";</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicstatic</span><span style="color:#3366ff; line-height:25px">String</span><span style="color:#0000ff; line-height:25px">DB_TABLE_NAME</span><span style="color:#3366ff; line-height:25px">="</span><span style="color:#99cc00; line-height:25px">people</span><span style="color:#3366ff; line-height:25px">";</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">SQLiteDatabasedb;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">final</span><span style="color:#3366ff; line-height:25px">DBOpenHelperdbOpenHelper;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">finalstatic</span><span style="color:#3366ff; line-height:25px">StringCOLS[]=newString[]</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{"_id","name","phone","age"};</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">final</span><span style="color:#3366ff; line-height:25px">Stringtag="robin";</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">DBHelper(Contextcontext)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">this.dbOpenHelper=newDBOpenHelper(context,DB_NAME,1);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">establishDb();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">privatevoid</span><span style="color:#3366ff; line-height:25px">establishDb()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(db==null)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px">db=dbOpenHelper.getWritableDatabase();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">cleanup()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(db!=null)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">db.close();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">db=null;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">insert(Peoplepeople)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ContentValuesvalues=newContentValues();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">values.put("name",people.name);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">values.put("phone",people.phone);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">values.put("age",people.age);</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">people.id=db.insert(DBHelper.DB_TABLE_NAME,null,values);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i(tag,"insert:"+people.id);</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">//selecttop1idfromtablenameorderbyid</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px"><wbr style="line-height:25px">Cursorc=db.query(DBHelper.DB_TABLE_NAME,newString[]{"_id"},null,null,null,null,"_idDESC","1");</wbr></span><br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#3366ff; line-height:25px">c.moveToFirst();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i("hubin","idMax:"+c.getInt(0));</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">c.close();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">update(Peoplepeople)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">ContentValuesvalues=newContentValues();<br style="line-height:25px"> values.put("name",people.name);<br style="line-height:25px"> values.put("phone",people.phone);<br style="line-height:25px"> values.put("age",people.age);<br style="line-height:25px"> db.update(DBHelper.DB_TABLE_NAME,values,"_id="+people.id,null);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">delete(longid)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px">db.delete(DB_TABLE_NAME,"_id="+id,null);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">deleteAll()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px">db.delete(DB_TABLE_NAME,null,null);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">Peopleget(longid)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Peoplepeople=newPeople();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Cursorc=null;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">try</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">c=db.query(DB_TABLE_NAME,COLS,"_id='"+id+"'",null,null,null,null);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i(tag,"count:"+c.getCount());</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(c.getCount()>0)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">c.moveToFirst();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people=newPeople();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people.id=c.getLong(0);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people.name=c.getString(1);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people.phone=c.getString(2);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people.age=c.getInt(3);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><span style="color:#993300; line-height:25px">catch</span><span style="color:#3366ff; line-height:25px">(SQLExceptione)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i(tag,"",e);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">finally</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(c!=null&&!c.isClosed())</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">c.close();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">people;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">List<People>getAll()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ArrayList<People>ret=newArrayList<People>();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Cursorc=null;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">try</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">c=db.query(DB_TABLE_NAME,COLS,null,null,null,null,null);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">intcount=c.getCount();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">c.moveToFirst();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Peoplepeople;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">for(inti=0;i<count;i++)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people=newPeople();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people.id=c.getLong(0);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people.name=c.getString(1);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people.phone=c.getString(2);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people.age=c.getInt(3);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ret.add(people);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">c.moveToNext();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><span style="color:#993300; line-height:25px">catch</span><span style="color:#3366ff; line-height:25px">(SQLExceptione)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i(tag,"",e);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">finally</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(c!=null&&!c.isClosed())</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">c.close();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">ret;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">class</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff00ff; line-height:25px">DBOpenHelper</span><span style="color:#3366ff; line-height:25px">extendsSQLiteOpenHelper</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">privatestaticfinal</span><span style="color:#3366ff; line-height:25px">StringDB_CREATE="CREATETABLE"</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">+DBHelper.DB_TABLE_NAME</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">+"(_idINTEGERPRIMARYKEY,nameTEXTUNIQUENOTNULL,"</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">+"phoneTEXT,ageINTEGER);";</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">finalstatic</span><span style="color:#3366ff; line-height:25px">Stringtag="robin";</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">DBOpenHelper(Contextcontext,StringdbName,intversion)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super(context,dbName,null,version);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">onCreate(SQLiteDatabasedb)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">try{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">db.execSQL(DB_CREATE);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">catch(SQLExceptione)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.e(tag,"",e);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">onOpen(SQLiteDatabasedb)<br style="line-height:25px"> {<br style="line-height:25px"> super.onOpen(db);<br style="line-height:25px"> }<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">onUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion)<br style="line-height:25px"> {<br style="line-height:25px"> db.execSQL("DROPTABLEIFEXISTS"+DBHelper.DB_TABLE_NAME);<br style="line-height:25px"> this.onCreate(db);<br style="line-height:25px"> }<br style="line-height:25px"> }</span><br style="line-height:25px"> Hello.java文件<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">packagecom.teleca;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.ArrayList;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.List;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.app.Activity;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.os.Bundle;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.os.Handler;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.util.Log;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.view.View;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.view.View.OnClickListener;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.widget.Button;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff00ff; line-height:25px">Hello</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">extends</span><span style="color:#3366ff; line-height:25px">Activity{</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/**Calledwhentheactivityisfirstcreated.*/</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">DBHelperdbHelper;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">finalstatic</span><span style="color:#3366ff; line-height:25px">Stringtag="hubin";</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">@Override</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#ff6600; line-height:25px">onCreate</span><span style="color:#3366ff; line-height:25px">(BundlesavedInstanceState){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super.onCreate(savedInstanceState);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">setContentView(R.layout.main);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Buttonbutton=(Button)findViewById(R.id.Button01);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">OnClickListenerlistener=newOnClickListener(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">@Override</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicvoidonClick(Viewv){</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">cmd=CMD_ADD;<br style="line-height:25px"> doAction();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">};</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">button.setOnClickListener(listener);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Buttonbutton2=(Button)findViewById(R.id.Button02);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">OnClickListenerlistener2=newOnClickListener(){</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">@Override</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicvoidonClick(Viewv){</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">cmd=CMD_UPDATE;<br style="line-height:25px"> doAction();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">};</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">button2.setOnClickListener(listener2);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Buttonbutton3=(Button)findViewById(R.id.Button03);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">OnClickListenerlistener3=newOnClickListener(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">@Override</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicvoidonClick(Viewv){</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">cmd=CMD_QUERY;<br style="line-height:25px"> doAction();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">};</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">button3.setOnClickListener(listener3);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Buttonbutton4=(Button)findViewById(R.id.Button04);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">OnClickListenerlistener4=newOnClickListener(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">@Override</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicvoidonClick(Viewv){</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">cmd=CMD_QUERY_ALL;<br style="line-height:25px"> doAction();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">};</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">button4.setOnClickListener(listener4);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Buttonbutton5=(Button)findViewById(R.id.Button05);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">OnClickListenerlistener5=newOnClickListener(){</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">@Override</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicvoidonClick(Viewv){</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">cmd=CMD_DELETE;<br style="line-height:25px"> doAction();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">};</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">button5.setOnClickListener(listener5);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Buttonbutton6=(Button)findViewById(R.id.Button06);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">OnClickListenerlistener6=newOnClickListener(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">@Override</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicvoidonClick(Viewv){</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">cmd=CMD_DELETE_ALL;<br style="line-height:25px"> doAction();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">};</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">button6.setOnClickListener(listener6);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">mHandler=newHandler();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">intcnt=0;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateHandlermHandler;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">intcmd=0;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">finalintCMD_ADD=1;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">finalintCMD_UPDATE=2;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">finalintCMD_QUERY=3;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">finalintCMD_QUERY_ALL=4;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">finalintCMD_DELETE=5;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">finalintCMD_DELETE_ALL=6;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Peoplepeople=newPeople();</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">class</span><span style="color:#ff6600; line-height:25px">DatabaseThread</span><span style="color:#3366ff; line-height:25px">implementsRunnable{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#ff6600; line-height:25px">run()</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(dbHelper==null)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">dbHelper=newDBHelper(Hello.this);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(cmd==CMD_ADD){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people.name="robin"+System.currentTimeMillis()%100;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people.phone=""+System.currentTimeMillis();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people.age=1;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">dbHelper.insert(people);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><span style="color:#993300; line-height:25px">elseif</span><span style="color:#3366ff; line-height:25px">(cmd==CMD_UPDATE){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people.phone=""+System.currentTimeMillis();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">dbHelper.update(people);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><span style="color:#993300; line-height:25px">elseif</span><span style="color:#3366ff; line-height:25px">(cmd==CMD_QUERY){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Peoplep=dbHelper.get(people.id);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">printPeople(p);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><span style="color:#993300; line-height:25px">elseif</span><span style="color:#3366ff; line-height:25px">(cmd==CMD_QUERY_ALL){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">List<People>list=dbHelper.getAll();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">inttotal=list.size();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">for(inti=0;i<total;i++)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">printPeople(list.get(i));</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">elseif</span><span style="color:#3366ff; line-height:25px">(cmd==CMD_DELETE)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">dbHelper.delete(people.id);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">elseif</span><span style="color:#3366ff; line-height:25px">(cmd==CMD_DELETE_ALL)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">dbHelper.deleteAll();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">cnt++;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">void</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">printPeople</span><span style="color:#3366ff; line-height:25px">(Peoplep)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i(tag,"id:"+p.id);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i(tag,"name:"+p.name);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i(tag,"phone:"+p.phone);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i(tag,"age:"+p.age);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">DatabaseThreaddataDealer=newDatabaseThread();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">void</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">doAction()</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px">mHandler.post(dataDealer);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"> people.java文件<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">packagecom.teleca;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">People</span><span style="color:#3366ff; line-height:25px">{<br style="line-height:25px"> publiclongid;<br style="line-height:25px"> publicStringname;<br style="line-height:25px"> publicStringphone;<br style="line-height:25px"> publicintage;<br style="line-height:25px"> }</span></wbr></wbr></wbr>
关于一些SQL基本命令的用法( CREATE,INSERT,UPDATE,DELETE 和 SELECT ),
请参阅SQLite文档( http://www.sqlite.org/lang.html<wbr style="line-height:25px">)<br style="line-height:25px"><span style="color:#003366; line-height:25px">与</span><span style="color:#993300; line-height:25px">SharedPreferences</span><span style="color:#003366; line-height:25px">不同,</span><span style="color:#ff6600; line-height:25px">不能</span><span style="color:#003366; line-height:25px">将数据库设为</span><span style="color:#ff6600; line-height:25px">WORLD_READABLE</span><span style="color:#003366; line-height:25px">,每个数据库都只能创建它的包访问,<br style="line-height:25px"> 这意味着只有由创建数据库的进程可访问它。如果需要在进程间传递数据,<br style="line-height:25px"> 则可以使用AIDL/Binder或创建一个</span><span style="color:#993300; line-height:25px">ContentProvider</span><span style="color:#003366; line-height:25px">,</span><span style="color:#000080; line-height:25px">但是不能跨越进程/包边界直接来使用数据库</span><span style="color:#003366; line-height:25px">。<br style="line-height:25px"> 数据库文件存放在</span><span style="color:#0000ff; line-height:25px">/data/data/[PACKAGE_NAME]/database/</span><span style="color:#003366; line-height:25px">下。这些文件是SQLite专有的。</span><br style="line-height:25px"> 可以通过ADBshell(sqlite3工具)来操作它。<br style="line-height:25px"> 详细命令可参考<br style="line-height:25px"><a target="_blank" rel="nofollow" href="http://www.sqlite.org/sqlite.html" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">http://www.sqlite.org/sqlite.html</a><wbr style="line-height:25px">。<br style="line-height:25px"><span style="line-height:25px">实例1</span>:<br style="line-height:25px"> DBHelper.java文件<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">packagecom.teleca;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.ArrayList;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.List;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.content.ContentValues;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.content.Context;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.database.Cursor;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.database.SQLException;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.database.sqlite.SQLiteDatabase;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.database.sqlite.SQLiteOpenHelper;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.util.Log;;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff00ff; line-height:25px">DBHelper</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicstatic</span><span style="color:#3366ff; line-height:25px">StringDB_NAME="</span><span style="color:#99cc00; line-height:25px">peopledb</span><span style="color:#3366ff; line-height:25px">";</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicstatic</span><span style="color:#3366ff; line-height:25px">String</span><span style="color:#0000ff; line-height:25px">DB_TABLE_NAME</span><span style="color:#3366ff; line-height:25px">="</span><span style="color:#99cc00; line-height:25px">people</span><span style="color:#3366ff; line-height:25px">";</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">SQLiteDatabasedb;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">final</span><span style="color:#3366ff; line-height:25px">DBOpenHelperdbOpenHelper;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">finalstatic</span><span style="color:#3366ff; line-height:25px">StringCOLS[]=newString[]</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{"_id","name","phone","age"};</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">final</span><span style="color:#3366ff; line-height:25px">Stringtag="robin";</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">DBHelper(Contextcontext)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">this.dbOpenHelper=newDBOpenHelper(context,DB_NAME,1);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">establishDb();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">privatevoid</span><span style="color:#3366ff; line-height:25px">establishDb()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(db==null)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px">db=dbOpenHelper.getWritableDatabase();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">cleanup()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(db!=null)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">db.close();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">db=null;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">insert(Peoplepeople)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ContentValuesvalues=newContentValues();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">values.put("name",people.name);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">values.put("phone",people.phone);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">values.put("age",people.age);</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">people.id=db.insert(DBHelper.DB_TABLE_NAME,null,values);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i(tag,"insert:"+people.id);</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">//selecttop1idfromtablenameorderbyid</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px"><wbr style="line-height:25px">Cursorc=db.query(DBHelper.DB_TABLE_NAME,newString[]{"_id"},null,null,null,null,"_idDESC","1");</wbr></span><br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#3366ff; line-height:25px">c.moveToFirst();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i("hubin","idMax:"+c.getInt(0));</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">c.close();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">update(Peoplepeople)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">ContentValuesvalues=newContentValues();<br style="line-height:25px"> values.put("name",people.name);<br style="line-height:25px"> values.put("phone",people.phone);<br style="line-height:25px"> values.put("age",people.age);<br style="line-height:25px"> db.update(DBHelper.DB_TABLE_NAME,values,"_id="+people.id,null);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">delete(longid)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px">db.delete(DB_TABLE_NAME,"_id="+id,null);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">deleteAll()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px">db.delete(DB_TABLE_NAME,null,null);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">Peopleget(longid)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Peoplepeople=newPeople();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Cursorc=null;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">try</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">c=db.query(DB_TABLE_NAME,COLS,"_id='"+id+"'",null,null,null,null);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i(tag,"count:"+c.getCount());</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(c.getCount()>0)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">c.moveToFirst();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people=newPeople();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people.id=c.getLong(0);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people.name=c.getString(1);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people.phone=c.getString(2);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people.age=c.getInt(3);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><span style="color:#993300; line-height:25px">catch</span><span style="color:#3366ff; line-height:25px">(SQLExceptione)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i(tag,"",e);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">finally</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(c!=null&&!c.isClosed())</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">c.close();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">people;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">List<People>getAll()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ArrayList<People>ret=newArrayList<People>();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Cursorc=null;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">try</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">c=db.query(DB_TABLE_NAME,COLS,null,null,null,null,null);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">intcount=c.getCount();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">c.moveToFirst();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Peoplepeople;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">for(inti=0;i<count;i++)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people=newPeople();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people.id=c.getLong(0);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people.name=c.getString(1);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people.phone=c.getString(2);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people.age=c.getInt(3);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ret.add(people);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">c.moveToNext();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><span style="color:#993300; line-height:25px">catch</span><span style="color:#3366ff; line-height:25px">(SQLExceptione)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i(tag,"",e);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">finally</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(c!=null&&!c.isClosed())</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">c.close();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">ret;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">class</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff00ff; line-height:25px">DBOpenHelper</span><span style="color:#3366ff; line-height:25px">extendsSQLiteOpenHelper</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">privatestaticfinal</span><span style="color:#3366ff; line-height:25px">StringDB_CREATE="CREATETABLE"</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">+DBHelper.DB_TABLE_NAME</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">+"(_idINTEGERPRIMARYKEY,nameTEXTUNIQUENOTNULL,"</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">+"phoneTEXT,ageINTEGER);";</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">finalstatic</span><span style="color:#3366ff; line-height:25px">Stringtag="robin";</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">DBOpenHelper(Contextcontext,StringdbName,intversion)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super(context,dbName,null,version);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">onCreate(SQLiteDatabasedb)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">try{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">db.execSQL(DB_CREATE);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">catch(SQLExceptione)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.e(tag,"",e);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">onOpen(SQLiteDatabasedb)<br style="line-height:25px"> {<br style="line-height:25px"> super.onOpen(db);<br style="line-height:25px"> }<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">onUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion)<br style="line-height:25px"> {<br style="line-height:25px"> db.execSQL("DROPTABLEIFEXISTS"+DBHelper.DB_TABLE_NAME);<br style="line-height:25px"> this.onCreate(db);<br style="line-height:25px"> }<br style="line-height:25px"> }</span><br style="line-height:25px"> Hello.java文件<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">packagecom.teleca;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.ArrayList;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.List;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.app.Activity;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.os.Bundle;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.os.Handler;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.util.Log;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.view.View;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.view.View.OnClickListener;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.widget.Button;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff00ff; line-height:25px">Hello</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">extends</span><span style="color:#3366ff; line-height:25px">Activity{</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/**Calledwhentheactivityisfirstcreated.*/</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">DBHelperdbHelper;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">finalstatic</span><span style="color:#3366ff; line-height:25px">Stringtag="hubin";</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">@Override</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#ff6600; line-height:25px">onCreate</span><span style="color:#3366ff; line-height:25px">(BundlesavedInstanceState){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super.onCreate(savedInstanceState);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">setContentView(R.layout.main);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Buttonbutton=(Button)findViewById(R.id.Button01);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">OnClickListenerlistener=newOnClickListener(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">@Override</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicvoidonClick(Viewv){</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">cmd=CMD_ADD;<br style="line-height:25px"> doAction();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">};</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">button.setOnClickListener(listener);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Buttonbutton2=(Button)findViewById(R.id.Button02);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">OnClickListenerlistener2=newOnClickListener(){</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">@Override</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicvoidonClick(Viewv){</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">cmd=CMD_UPDATE;<br style="line-height:25px"> doAction();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">};</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">button2.setOnClickListener(listener2);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Buttonbutton3=(Button)findViewById(R.id.Button03);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">OnClickListenerlistener3=newOnClickListener(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">@Override</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicvoidonClick(Viewv){</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">cmd=CMD_QUERY;<br style="line-height:25px"> doAction();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">};</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">button3.setOnClickListener(listener3);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Buttonbutton4=(Button)findViewById(R.id.Button04);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">OnClickListenerlistener4=newOnClickListener(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">@Override</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicvoidonClick(Viewv){</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">cmd=CMD_QUERY_ALL;<br style="line-height:25px"> doAction();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">};</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">button4.setOnClickListener(listener4);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Buttonbutton5=(Button)findViewById(R.id.Button05);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">OnClickListenerlistener5=newOnClickListener(){</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">@Override</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicvoidonClick(Viewv){</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">cmd=CMD_DELETE;<br style="line-height:25px"> doAction();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">};</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">button5.setOnClickListener(listener5);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Buttonbutton6=(Button)findViewById(R.id.Button06);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">OnClickListenerlistener6=newOnClickListener(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">@Override</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicvoidonClick(Viewv){</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">cmd=CMD_DELETE_ALL;<br style="line-height:25px"> doAction();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">};</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">button6.setOnClickListener(listener6);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">mHandler=newHandler();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">intcnt=0;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateHandlermHandler;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">intcmd=0;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">finalintCMD_ADD=1;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">finalintCMD_UPDATE=2;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">finalintCMD_QUERY=3;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">finalintCMD_QUERY_ALL=4;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">finalintCMD_DELETE=5;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">finalintCMD_DELETE_ALL=6;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Peoplepeople=newPeople();</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">class</span><span style="color:#ff6600; line-height:25px">DatabaseThread</span><span style="color:#3366ff; line-height:25px">implementsRunnable{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#ff6600; line-height:25px">run()</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(dbHelper==null)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">dbHelper=newDBHelper(Hello.this);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(cmd==CMD_ADD){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people.name="robin"+System.currentTimeMillis()%100;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people.phone=""+System.currentTimeMillis();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people.age=1;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">dbHelper.insert(people);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><span style="color:#993300; line-height:25px">elseif</span><span style="color:#3366ff; line-height:25px">(cmd==CMD_UPDATE){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">people.phone=""+System.currentTimeMillis();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">dbHelper.update(people);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><span style="color:#993300; line-height:25px">elseif</span><span style="color:#3366ff; line-height:25px">(cmd==CMD_QUERY){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Peoplep=dbHelper.get(people.id);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">printPeople(p);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><span style="color:#993300; line-height:25px">elseif</span><span style="color:#3366ff; line-height:25px">(cmd==CMD_QUERY_ALL){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">List<People>list=dbHelper.getAll();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">inttotal=list.size();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">for(inti=0;i<total;i++)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">printPeople(list.get(i));</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">elseif</span><span style="color:#3366ff; line-height:25px">(cmd==CMD_DELETE)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">dbHelper.delete(people.id);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">elseif</span><span style="color:#3366ff; line-height:25px">(cmd==CMD_DELETE_ALL)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">dbHelper.deleteAll();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">cnt++;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">void</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">printPeople</span><span style="color:#3366ff; line-height:25px">(Peoplep)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i(tag,"id:"+p.id);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i(tag,"name:"+p.name);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i(tag,"phone:"+p.phone);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Log.i(tag,"age:"+p.age);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">DatabaseThreaddataDealer=newDatabaseThread();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">void</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">doAction()</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px">mHandler.post(dataDealer);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"> people.java文件<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">packagecom.teleca;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">People</span><span style="color:#3366ff; line-height:25px">{<br style="line-height:25px"> publiclongid;<br style="line-height:25px"> publicStringname;<br style="line-height:25px"> publicStringphone;<br style="line-height:25px"> publicintage;<br style="line-height:25px"> }</span></wbr></wbr></wbr>
本文介绍了Android内置SQLite数据库系统的使用方法,包括SQL基本命令和数据库管理,以及如何通过DBHelper类进行增删改查操作。

被折叠的 条评论
为什么被折叠?



