设为首页 收藏本站
查看: 1255|回复: 0

[经验分享] Sqlite中INTEGER PRIMARY KEY的修正

[复制链接]

尚未签到

发表于 2016-11-30 11:54:11 | 显示全部楼层 |阅读模式
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>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-307724-1-1.html 上篇帖子: Android数据存储之三SQLite嵌入式数据库(2) 下篇帖子: C#使用SQLite数据库(asp.net/winform)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表