Sqlite中INTEGER PRIMARY KEY的修正

android 的Sqlite中对于 INTEGERPRIMARYKEY
如果插入数据A,B,C,它们的_id为1,2,3,那么如果把他们都删除了,
再插入一条数据,那么它的id为为1而不是4。
因此我就写了这个工具来生成id,它能让生成相同的id相隔尽量的远。<wbr style="line-height:25px"><br style="line-height:25px"><wbr style="line-height:25px">然后在插据入数时手动设置id,及把这个作为插入参数id传进去。<br style="line-height:25px"> 删除时,也要做相应的处理。</wbr></wbr>

实例1:
文件1:<wbr style="line-height:25px"><br style="line-height:25px"> SerialManager.java<br style="line-height:25px"> packagecom.teleca;<br style="line-height:25px"><br style="line-height:25px"> publicclassSerialManager{<br style="line-height:25px"> Noderoot=null;<br style="line-height:25px"> longmin=Long.MIN_VALUE;<br style="line-height:25px"> longmax=Long.MAX_VALUE;<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">longcursor=1;</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> public<span style="line-height:25px"><wbr style="line-height:25px">SerialManager()</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> {<br style="line-height:25px"> }<br style="line-height:25px"> public<span style="line-height:25px"><wbr style="line-height:25px">SerialManager(longmin,longmax)</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> {<br style="line-height:25px"> if(min&gt;max)<br style="line-height:25px"> {<br style="line-height:25px"> longtemp=min;<br style="line-height:25px"> min=max;<br style="line-height:25px"> max=temp;<br style="line-height:25px"> }<br style="line-height:25px"> this.min=min;<br style="line-height:25px"> this.max=max;<br style="line-height:25px"> }<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">publicvoidsetSerial(longid,booleanflag)</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> {<br style="line-height:25px"> longtemp=0;<br style="line-height:25px"> Nodenode=findNode(id);<br style="line-height:25px"> if(flag)<br style="line-height:25px"> {<br style="line-height:25px"> if(node==null)<br style="line-height:25px"> {<br style="line-height:25px"> node=findNode(id-1);<br style="line-height:25px"> if(node!=null)<br style="line-height:25px"> {<br style="line-height:25px"> node.data.end=id;<br style="line-height:25px"> Nodenode2=findNode(id+1);<br style="line-height:25px"> if(node2!=null)<br style="line-height:25px"> {<br style="line-height:25px"> node.data.end=node2.data.end;<br style="line-height:25px"> this.removeNode(node2);<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> else<br style="line-height:25px"> {<br style="line-height:25px"> Nodenode2=findNode(id+1);<br style="line-height:25px"> if(node2!=null)<br style="line-height:25px"> {<br style="line-height:25px"> node2.data.start=id;<br style="line-height:25px"> }<br style="line-height:25px"> else<br style="line-height:25px"> addNewNode(id,id);<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> return;<br style="line-height:25px"> }<br style="line-height:25px"> else<br style="line-height:25px"> {<br style="line-height:25px"> if(node==null)<br style="line-height:25px"> return;<br style="line-height:25px"> Blockdata=node.data;<br style="line-height:25px"> if(data.start==data.end)<br style="line-height:25px"> {<br style="line-height:25px"> removeNode(node);<br style="line-height:25px"> }<br style="line-height:25px"> elseif(id==data.start)<br style="line-height:25px"> data.start=id+1;<br style="line-height:25px"> elseif(id==data.end)<br style="line-height:25px"> data.end=id-1;<br style="line-height:25px"> else<br style="line-height:25px"> {<br style="line-height:25px"> temp=data.end;<br style="line-height:25px"> data.end=id-1;<br style="line-height:25px"> addNewNode(id+1,temp);<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> publicvoid<span style="line-height:25px"><wbr style="line-height:25px">setSerial(longid1,longid2)</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> {<br style="line-height:25px"> longtemp=0;<br style="line-height:25px"> if(id1&gt;id2)<br style="line-height:25px"> {<br style="line-height:25px"> temp=id1;<br style="line-height:25px"> id1=id2;<br style="line-height:25px"> id2=temp;<br style="line-height:25px"> }<br style="line-height:25px"> Nodenode1=findNode(id1);<br style="line-height:25px"> Nodenode2=null;<br style="line-height:25px"> if(node1==null)<br style="line-height:25px"> {<br style="line-height:25px"> node1=findNode(id1-1);<br style="line-height:25px"> if(node1!=null)<br style="line-height:25px"> {<br style="line-height:25px"> node1.data.end=id2;<br style="line-height:25px"> node2=findNode(id2+1);<br style="line-height:25px"> if(node2!=null)<br style="line-height:25px"> {<br style="line-height:25px"> node1.data.end=node2.data.end;<br style="line-height:25px"> this.removeNode(node2);<br style="line-height:25px"> }<br style="line-height:25px"> return;<br style="line-height:25px"> }<br style="line-height:25px"> else<br style="line-height:25px"> {<br style="line-height:25px"> node2=findNode(id2);<br style="line-height:25px"> if(node2==null)<br style="line-height:25px"> {<br style="line-height:25px"> node2=findNode(id2+1);<br style="line-height:25px"> if(node2!=null)<br style="line-height:25px"> node2.data.start=id1;<br style="line-height:25px"> else<br style="line-height:25px"> addNewNode(id1,id2);<br style="line-height:25px"> return;<br style="line-height:25px"> }<br style="line-height:25px"> else<br style="line-height:25px"> {<br style="line-height:25px"> node2.data.start=id1;<br style="line-height:25px"> return;<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> }<br style="line-height:25px"> else<br style="line-height:25px"> {<br style="line-height:25px"> Blockdata=node1.data;<br style="line-height:25px"> if(id2&lt;=data.end)<br style="line-height:25px"> {<br style="line-height:25px"> return;<br style="line-height:25px"> }<br style="line-height:25px"> else<br style="line-height:25px"> {<br style="line-height:25px"> node2=findNode(id2);<br style="line-height:25px"> if(node2==null)<br style="line-height:25px"> {<br style="line-height:25px"> data.end=id2;<br style="line-height:25px"> return;<br style="line-height:25px"> }<br style="line-height:25px"> else<br style="line-height:25px"> {<br style="line-height:25px"> data.end=node2.data.end;<br style="line-height:25px"> removeNode(node2);<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> publiclong<span style="line-height:25px"><wbr style="line-height:25px">getSerial()</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> {<br style="line-height:25px"><br style="line-height:25px"> Nodenode=findNode(cursor);<br style="line-height:25px"> longstart=cursor;<br style="line-height:25px"> while(node!=null)<br style="line-height:25px"> {<br style="line-height:25px"> cursor=node.data.end+1;<br style="line-height:25px"> if(cursor&gt;max)<br style="line-height:25px"> cursor=min;<br style="line-height:25px"> elseif(cursor==0)<br style="line-height:25px"> cursor++;<br style="line-height:25px"> if(cursor==start)<br style="line-height:25px"> {<br style="line-height:25px"> return0;<br style="line-height:25px"> }<br style="line-height:25px"> node=findNode(cursor);<br style="line-height:25px"> }<br style="line-height:25px"> longres=cursor;<br style="line-height:25px"> cursor++;<br style="line-height:25px"> if(cursor&gt;max)<br style="line-height:25px"> cursor=min;<br style="line-height:25px"> elseif(cursor==0)<br style="line-height:25px"> cursor++;<br style="line-height:25px"> returnres;<br style="line-height:25px"> }<br style="line-height:25px"> publicboolean<span style="line-height:25px"><wbr style="line-height:25px">isKeyUsed(longid)<br style="line-height:25px"></wbr></span><wbr style="line-height:25px">{<br style="line-height:25px"> returnfindNode(id)!=null;<br style="line-height:25px"> }<br style="line-height:25px"> privateNodefindNode(longid)<br style="line-height:25px"> {<br style="line-height:25px"> Nodenode=null;<br style="line-height:25px"> NodetempNode=root;<br style="line-height:25px"> Blockblock=null;<br style="line-height:25px"> while(tempNode!=null)<br style="line-height:25px"> {<br style="line-height:25px"> block=tempNode.data;<br style="line-height:25px"> if(block.start&lt;=id&amp;&amp;id&lt;=block.end)<br style="line-height:25px"> {<br style="line-height:25px"> node=tempNode;<br style="line-height:25px"> break;<br style="line-height:25px"> }<br style="line-height:25px"> tempNode=tempNode.next;<br style="line-height:25px"> }<br style="line-height:25px"> returnnode;<br style="line-height:25px"> }<br style="line-height:25px"> privatevoidaddNewNode(longid1,longid2)<br style="line-height:25px"> {<br style="line-height:25px"> Nodenode=newNode();<br style="line-height:25px"> node.data=newBlock(id1,id2);<br style="line-height:25px"> addNode(node);<br style="line-height:25px"> }<br style="line-height:25px"> privatevoidaddNode(Nodenode)<br style="line-height:25px"> {<br style="line-height:25px"> if(root==null)<br style="line-height:25px"> {<br style="line-height:25px"> root=node;<br style="line-height:25px"> node.prev=null;<br style="line-height:25px"> node.next=null;<br style="line-height:25px"> return;<br style="line-height:25px"> }<br style="line-height:25px"> NodetempNode=root;<br style="line-height:25px"> while(tempNode!=null)<br style="line-height:25px"> {<br style="line-height:25px"> if(tempNode.data.start&gt;node.data.end)<br style="line-height:25px"> {<br style="line-height:25px"> if(tempNode==root)<br style="line-height:25px"> {<br style="line-height:25px"> node.prev=null;<br style="line-height:25px"> node.next=root;<br style="line-height:25px"> tempNode.prev=node;<br style="line-height:25px"> root=node;<br style="line-height:25px"> }<br style="line-height:25px"> else<br style="line-height:25px"> {<br style="line-height:25px"> node.prev=tempNode.prev;<br style="line-height:25px"> node.next=tempNode;<br style="line-height:25px"> tempNode.prev.next=node;<br style="line-height:25px"> tempNode.prev=node;<br style="line-height:25px"> }<br style="line-height:25px"> break;<br style="line-height:25px"> }<br style="line-height:25px"> elseif(tempNode.next==null)<br style="line-height:25px"> {<br style="line-height:25px"> tempNode.next=node;<br style="line-height:25px"> node.prev=tempNode;<br style="line-height:25px"> node.next=null;<br style="line-height:25px"> break;<br style="line-height:25px"> }<br style="line-height:25px"> tempNode=tempNode.next;<br style="line-height:25px"><br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> }<br style="line-height:25px"> privatevoidremoveNode(Nodenode)<br style="line-height:25px"> {<br style="line-height:25px"> Nodeprev=node.prev;<br style="line-height:25px"> if(prev==null)<br style="line-height:25px"> {<br style="line-height:25px"> root=node.next;<br style="line-height:25px"> }<br style="line-height:25px"> else<br style="line-height:25px"> {<br style="line-height:25px"> prev.next=node.next;<br style="line-height:25px"> }<br style="line-height:25px"> if(node.next!=null)<br style="line-height:25px"> node.next.prev=prev;<br style="line-height:25px"> node.prev=null;<br style="line-height:25px"> node.next=null;<br style="line-height:25px"> }<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">publicvoidclear()<br style="line-height:25px"></wbr></span><wbr style="line-height:25px">{<br style="line-height:25px"> NodetempNode=root;<br style="line-height:25px"> Nodenode=null;<br style="line-height:25px"> while(tempNode!=null)<br style="line-height:25px"> {<br style="line-height:25px"> node=tempNode;<br style="line-height:25px"> tempNode=tempNode.next;<br style="line-height:25px"> node.prev=null;<br style="line-height:25px"> node.next=null;<br style="line-height:25px"><br style="line-height:25px"> }<br style="line-height:25px"> root=null;<br style="line-height:25px"> cursor=1;<br style="line-height:25px"> }<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">publicvoidprintln()<br style="line-height:25px"></wbr></span><wbr style="line-height:25px">{<br style="line-height:25px"> NodetempNode=root;<br style="line-height:25px"> while(tempNode!=null)<br style="line-height:25px"> {<br style="line-height:25px"> System.out.println("start:"+tempNode.data.start+"end:"+tempNode.data.end);<br style="line-height:25px"> tempNode=tempNode.next;<br style="line-height:25px"><br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> classNode<br style="line-height:25px"> {<br style="line-height:25px"> Nodeprev=null;<br style="line-height:25px"> Blockdata;<br style="line-height:25px"> Nodenext=null;<br style="line-height:25px"> }<br style="line-height:25px"> classBlock<br style="line-height:25px"> {<br style="line-height:25px"> longstart=0;<br style="line-height:25px"> longend=0;<br style="line-height:25px"> Block(longid1,longid2)<br style="line-height:25px"> {<br style="line-height:25px"> start=id1;<br style="line-height:25px"> end=id2;<br style="line-height:25px"> }<br style="line-height:25px"> publiclonggetStart(){<br style="line-height:25px"> returnstart;<br style="line-height:25px"> }<br style="line-height:25px"> publicvoidsetStart(longstart){<br style="line-height:25px"> this.start=start;<br style="line-height:25px"> }<br style="line-height:25px"> publiclonggetEnd(){<br style="line-height:25px"> returnend;<br style="line-height:25px"> }<br style="line-height:25px"> publicvoidsetEnd(longend){<br style="line-height:25px"> this.end=end;<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> 文件2:<br style="line-height:25px"> DBHelper.java<br style="line-height:25px"> packagecom.teleca;<br style="line-height:25px"> importjava.util.ArrayList;<br style="line-height:25px"> importjava.util.List;<br style="line-height:25px"> importandroid.content.ContentValues;<br style="line-height:25px"> importandroid.content.Context;<br style="line-height:25px"> importandroid.database.Cursor;<br style="line-height:25px"> importandroid.database.SQLException;<br style="line-height:25px"> importandroid.database.sqlite.SQLiteDatabase;<br style="line-height:25px"> importandroid.database.sqlite.SQLiteOpenHelper;<br style="line-height:25px"> importandroid.util.Log;;<br style="line-height:25px"> publicclassDBHelper{<br style="line-height:25px"> publicstaticStringDB_NAME="peopledb";<br style="line-height:25px"> publicstaticStringDB_TABLE_NAME="people";<br style="line-height:25px"> SQLiteDatabasedb;<br style="line-height:25px"> finalDBOpenHelperdbOpenHelper;<br style="line-height:25px"> finalstaticStringCOLS[]=newString[]<br style="line-height:25px"> {"_id","name","phone","age"};<br style="line-height:25px"> finalStringtag="hubin";<br style="line-height:25px"> publicDBHelper(Contextcontext)<br style="line-height:25px"> {<br style="line-height:25px"> this.dbOpenHelper=newDBOpenHelper(context,DB_NAME,1);<br style="line-height:25px"> establishDb();<br style="line-height:25px"> }<br style="line-height:25px"> SerialManagerserialManager=newSerialManager();<br style="line-height:25px"> privatevoidestablishDb()<br style="line-height:25px"> {<br style="line-height:25px"> if(db==null)<br style="line-height:25px"> db=dbOpenHelper.getWritableDatabase();<br style="line-height:25px"><span style="color:#0000ff; line-height:25px"><wbr style="line-height:25px">initSerial();</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> }<br style="line-height:25px"> privatevoid<span style="line-height:25px"><wbr style="line-height:25px">initSerial()</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> {<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">serialManager.clear();</span><br style="line-height:25px"> Cursorc=db.query(DBHelper.DB_TABLE_NAME,newString[]{"_id"},null,null,null,null,"_idDESC",null);<br style="line-height:25px"> c.moveToFirst();<br style="line-height:25px"> intcount=c.getCount();<br style="line-height:25px"> c.moveToFirst();<br style="line-height:25px"> longid=0;<br style="line-height:25px"> for(inti=0;i&lt;count;i++)<br style="line-height:25px"> {<br style="line-height:25px"> id=c.getLong(0);<br style="line-height:25px"><span style="color:#0000ff; line-height:25px"><span style="line-height:25px"></span>serialManager.setSerial(id,true);</span><br style="line-height:25px"> Log.i("hubin","id:"+id+"settoserial");<br style="line-height:25px"> c.moveToNext();<br style="line-height:25px"> }<br style="line-height:25px"> c.close();<br style="line-height:25px"> }<br style="line-height:25px"> publicvoidcleanup()<br style="line-height:25px"> {<br style="line-height:25px"> if(db!=null)<br style="line-height:25px"> {<br style="line-height:25px"> db.close();<br style="line-height:25px"> db=null;<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> publicvoidinsert(Peoplepeople)<br style="line-height:25px"> {<br style="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"><span style="color:#0000ff; line-height:25px">longid=serialManager.getSerial();</span><br style="line-height:25px"> values.put("_id",id);<br style="line-height:25px"> people.id=db.insert(DBHelper.DB_TABLE_NAME,null,values);<br style="line-height:25px"><span style="color:#0000ff; line-height:25px"><wbr style="line-height:25px">serialManager.setSerial(id,true);</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> Log.i(tag,"insert:"+people.id);<br style="line-height:25px"> }<br style="line-height:25px"> publicvoidupdate(Peoplepeople)<br style="line-height:25px"> {<br style="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);<br style="line-height:25px"> }<br style="line-height:25px"> publicvoiddelete(longid)<br style="line-height:25px"> {<br style="line-height:25px"> db.delete(DB_TABLE_NAME,"_id="+id,null);<br style="line-height:25px"><span style="line-height:25px"><span style="color:#0000ff; line-height:25px"><wbr style="line-height:25px">serialManager.setSerial(id,false);</wbr></span></span><wbr style="line-height:25px"><br style="line-height:25px"> }<br style="line-height:25px"> publicvoiddeleteAll()<br style="line-height:25px"> {<br style="line-height:25px"> db.delete(DB_TABLE_NAME,null,null);<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">serialManager.clear();</wbr></span><br style="line-height:25px"><wbr style="line-height:25px">}<br style="line-height:25px"> publicPeopleget(longid)<br style="line-height:25px"> {<br style="line-height:25px"> Peoplepeople=newPeople();<br style="line-height:25px"> Cursorc=null;<br style="line-height:25px"> try{<br style="line-height:25px"> c=db.query(DB_TABLE_NAME,COLS,"_id='"+id+"'",null,null,null,null);<br style="line-height:25px"> Log.i(tag,"count:"+c.getCount());<br style="line-height:25px"> if(c.getCount()&gt;0)<br style="line-height:25px"> {<br style="line-height:25px"> c.moveToFirst();<br style="line-height:25px"> people=newPeople();<br style="line-height:25px"> people.id=c.getLong(0);<br style="line-height:25px"> people.name=c.getString(1);<br style="line-height:25px"> people.phone=c.getString(2);<br style="line-height:25px"> people.age=c.getInt(3);<br style="line-height:25px"> }<br style="line-height:25px"> }catch(SQLExceptione)<br style="line-height:25px"> {<br style="line-height:25px"> Log.i(tag,"",e);<br style="line-height:25px"> }<br style="line-height:25px"> finally<br style="line-height:25px"> {<br style="line-height:25px"> if(c!=null&amp;&amp;!c.isClosed())<br style="line-height:25px"> {<br style="line-height:25px"> c.close();<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> returnpeople;<br style="line-height:25px"> }<br style="line-height:25px"> publicList&lt;People&gt;getAll()<br style="line-height:25px"> {<br style="line-height:25px"> ArrayList&lt;People&gt;ret=newArrayList&lt;People&gt;();<br style="line-height:25px"> Cursorc=null;<br style="line-height:25px"> try<br style="line-height:25px"> {<br style="line-height:25px"> c=db.query(DB_TABLE_NAME,COLS,null,null,null,null,null);<br style="line-height:25px"> intcount=c.getCount();<br style="line-height:25px"> c.moveToFirst();<br style="line-height:25px"> Peoplepeople;<br style="line-height:25px"> for(inti=0;i&lt;count;i++)<br style="line-height:25px"> {<br style="line-height:25px"> people=newPeople();<br style="line-height:25px"> people.id=c.getLong(0);<br style="line-height:25px"> people.name=c.getString(1);<br style="line-height:25px"> people.phone=c.getString(2);<br style="line-height:25px"> people.age=c.getInt(3);<br style="line-height:25px"> ret.add(people);<br style="line-height:25px"> c.moveToNext();<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> }catch(SQLExceptione)<br style="line-height:25px"> {<br style="line-height:25px"> Log.i(tag,"",e);<br style="line-height:25px"> }<br style="line-height:25px"> finally<br style="line-height:25px"> {<br style="line-height:25px"> if(c!=null&amp;&amp;!c.isClosed())<br style="line-height:25px"> {<br style="line-height:25px"> c.close();<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> returnret;<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> classDBOpenHelperextendsSQLiteOpenHelper<br style="line-height:25px"> {<br style="line-height:25px"> privatestaticfinalStringDB_CREATE="CREATETABLE"<br style="line-height:25px"> +DBHelper.DB_TABLE_NAME<br style="line-height:25px"> +"(_idINTEGERPRIMARYKEY,nameTEXTUNIQUENOTNULL,"<br style="line-height:25px"> +"phoneTEXT,ageINTEGER);";<br style="line-height:25px"> finalstaticStringtag="hubin";<br style="line-height:25px"> publicDBOpenHelper(Contextcontext,StringdbName,intversion)<br style="line-height:25px"> {<br style="line-height:25px"> super(context,dbName,null,version);<br style="line-height:25px"> }<br style="line-height:25px"> publicvoidonCreate(SQLiteDatabasedb)<br style="line-height:25px"> {<br style="line-height:25px"> try{<br style="line-height:25px"> db.execSQL(DB_CREATE);<br style="line-height:25px"> }<br style="line-height:25px"> catch(SQLExceptione)<br style="line-height:25px"> {<br style="line-height:25px"> Log.e(tag,"",e);<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> publicvoidonOpen(SQLiteDatabasedb)<br style="line-height:25px"> {<br style="line-height:25px"> super.onOpen(db);<br style="line-height:25px"> }<br style="line-height:25px"> publicvoidonUpgrade(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"> }<br style="line-height:25px"> 文件3:<br style="line-height:25px"> People.java<br style="line-height:25px"> packagecom.teleca;<br style="line-height:25px"> publicclassPeople{<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"> }<br style="line-height:25px"> 文件4:<br style="line-height:25px"> Hello.java<br style="line-height:25px"> packagecom.teleca;<br style="line-height:25px"> importjava.util.List;<br style="line-height:25px"> importandroid.app.Activity;<br style="line-height:25px"> importandroid.os.Bundle;<br style="line-height:25px"> importandroid.os.Handler;<br style="line-height:25px"> importandroid.util.Log;<br style="line-height:25px"> importandroid.view.View;<br style="line-height:25px"> importandroid.view.View.OnClickListener;<br style="line-height:25px"> importandroid.widget.Button;<br style="line-height:25px"> publicclassHelloextendsActivity{<br style="line-height:25px"> DBHelperdbHelper;<br style="line-height:25px"> finalstaticStringtag="hubin";<br style="line-height:25px"> /**Calledwhentheactivityisfirstcreated.*/<br style="line-height:25px"> @Override<br style="line-height:25px"> publicvoidonCreate(BundlesavedInstanceState){<br style="line-height:25px"> super.onCreate(savedInstanceState);<br style="line-height:25px"> setContentView(R.layout.main);<br style="line-height:25px"> Buttonbutton=(Button)findViewById(R.id.Button01);<br style="line-height:25px"> OnClickListenerlistener=newOnClickListener(){<br style="line-height:25px"> publicvoidonClick(Viewv){<br style="line-height:25px"> cmd=CMD_ADD;<br style="line-height:25px"> doAction();<br style="line-height:25px"> }<br style="line-height:25px"> };<br style="line-height:25px"> button.setOnClickListener(listener);<br style="line-height:25px"> Buttonbutton2=(Button)findViewById(R.id.Button02);<br style="line-height:25px"> OnClickListenerlistener2=newOnClickListener(){<br style="line-height:25px"> @Override<br style="line-height:25px"> publicvoidonClick(Viewv){<br style="line-height:25px"> cmd=CMD_UPDATE;<br style="line-height:25px"> doAction();<br style="line-height:25px"> }<br style="line-height:25px"> };<br style="line-height:25px"> button2.setOnClickListener(listener2);<br style="line-height:25px"> Buttonbutton3=(Button)findViewById(R.id.Button03);<br style="line-height:25px"> OnClickListenerlistener3=newOnClickListener(){<br style="line-height:25px"> @Override<br style="line-height:25px"> publicvoidonClick(Viewv){<br style="line-height:25px"> cmd=CMD_QUERY;<br style="line-height:25px"> doAction();<br style="line-height:25px"> }<br style="line-height:25px"> };<br style="line-height:25px"> button3.setOnClickListener(listener3);<br style="line-height:25px"> Buttonbutton4=(Button)findViewById(R.id.Button04);<br style="line-height:25px"> OnClickListenerlistener4=newOnClickListener(){<br style="line-height:25px"> @Override<br style="line-height:25px"> publicvoidonClick(Viewv){<br style="line-height:25px"> cmd=CMD_QUERY_ALL;<br style="line-height:25px"> doAction();<br style="line-height:25px"> }<br style="line-height:25px"> };<br style="line-height:25px"> button4.setOnClickListener(listener4);<br style="line-height:25px"> Buttonbutton5=(Button)findViewById(R.id.Button05);<br style="line-height:25px"> OnClickListenerlistener5=newOnClickListener(){<br style="line-height:25px"> @Override<br style="line-height:25px"> publicvoidonClick(Viewv){<br style="line-height:25px"> cmd=CMD_DELETE;<br style="line-height:25px"> doAction();<br style="line-height:25px"> }<br style="line-height:25px"> };<br style="line-height:25px"> button5.setOnClickListener(listener5);<br style="line-height:25px"> Buttonbutton6=(Button)findViewById(R.id.Button06);<br style="line-height:25px"> OnClickListenerlistener6=newOnClickListener(){<br style="line-height:25px"> @Override<br style="line-height:25px"> publicvoidonClick(Viewv){<br style="line-height:25px"> cmd=CMD_DELETE_ALL;<br style="line-height:25px"> doAction();<br style="line-height:25px"> }<br style="line-height:25px"> };<br style="line-height:25px"> button6.setOnClickListener(listener6);<br style="line-height:25px"> mHandler=newHandler();<br style="line-height:25px"><br style="line-height:25px"> }<br style="line-height:25px"> publicvoidonStop()<br style="line-height:25px"> {<br style="line-height:25px"> super.onStop();<br style="line-height:25px"> Log.v(tag,"onStop");<br style="line-height:25px"> }<br style="line-height:25px"> intcnt=0;<br style="line-height:25px"> privateHandlermHandler;<br style="line-height:25px"> intcmd=0;<br style="line-height:25px"> finalintCMD_ADD=1;<br style="line-height:25px"> finalintCMD_UPDATE=2;<br style="line-height:25px"> finalintCMD_QUERY=3;<br style="line-height:25px"> finalintCMD_QUERY_ALL=4;<br style="line-height:25px"> finalintCMD_DELETE=5;<br style="line-height:25px"> finalintCMD_DELETE_ALL=6;<br style="line-height:25px"> Peoplepeople=newPeople();<br style="line-height:25px"> classDatabaseThreadimplementsRunnable{<br style="line-height:25px"> publicvoidrun(){<br style="line-height:25px"> if(dbHelper==null)<br style="line-height:25px"> dbHelper=newDBHelper(Hello.this);<br style="line-height:25px"> if(cmd==CMD_ADD){<br style="line-height:25px"> people.name="robin"+System.currentTimeMillis()%100;<br style="line-height:25px"> people.phone=""+System.currentTimeMillis();<br style="line-height:25px"> people.age=1;<br style="line-height:25px"> dbHelper.insert(people);<br style="line-height:25px"> }elseif(cmd==CMD_UPDATE){<br style="line-height:25px"> people.phone=""+System.currentTimeMillis();<br style="line-height:25px"> dbHelper.update(people);<br style="line-height:25px"> }elseif(cmd==CMD_QUERY){<br style="line-height:25px"> Peoplep=dbHelper.get(people.id);<br style="line-height:25px"> printPeople(p);<br style="line-height:25px"> }elseif(cmd==CMD_QUERY_ALL){<br style="line-height:25px"> List&lt;People&gt;list=dbHelper.getAll();<br style="line-height:25px"> inttotal=list.size();<br style="line-height:25px"> for(inti=0;i&lt;total;i++)<br style="line-height:25px"> {<br style="line-height:25px"> printPeople(list.get(i));<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> elseif(cmd==CMD_DELETE)<br style="line-height:25px"> {<br style="line-height:25px"> dbHelper.delete(people.id);<br style="line-height:25px"> }<br style="line-height:25px"> elseif(cmd==CMD_DELETE_ALL)<br style="line-height:25px"> {<br style="line-height:25px"> dbHelper.deleteAll();<br style="line-height:25px"> }<br style="line-height:25px"> cnt++;<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"> voidprintPeople(Peoplep)<br style="line-height:25px"> {<br style="line-height:25px"> Log.i(tag,"id:"+p.id);<br style="line-height:25px"> Log.i(tag,"name:"+p.name);<br style="line-height:25px"> Log.i(tag,"phone:"+p.phone);<br style="line-height:25px"> Log.i(tag,"age:"+p.age);<br style="line-height:25px"> }<br style="line-height:25px"> DatabaseThreaddataDealer=newDatabaseThread();<br style="line-height:25px"> voiddoAction(){<br style="line-height:25px"> mHandler.post(dataDealer);<br style="line-height:25px"> }<br style="line-height:25px"> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值