Android SQLite 사례 깊이 연구 (5)

본문http://www.eoeandroid.com/thread-82030-1-1.html
public void delete(Integer... ids){

if(ids.length>0){
StringBuilder sb = new StringBuilder();
String[] strIds = new String[ids.length];


for(int i=0 ; i < ids.length ; i++){
sb.append('?').append(',');
strIds[i] = String.valueOf(ids[i]);
}

sb.deleteCharAt(sb.length()-1);
SQLiteDatabase database = dbmanger.getWritableDatabase();
//                
database.delete("person", "personid in("+ sb + ")", strIds);
}
}

public List getScrollData(int startResult, int maxResult){
List persons = new ArrayList();
SQLiteDatabase database = dbmanger.getWritableDatabase();
//                                           
Cursor cursor = database.query("person", new String[]{"personid", "name", "age"},null, null, null, null, "personid desc", startResult+ ","+ maxResult);

while(cursor.moveToNext()){
persons.add(new Person(cursor.getInt(0), cursor.getString(1), cursor.getShort(2)));
}
return persons;
}
public long getCount(){
SQLiteDatabase database = dbmanger.getWritableDatabase();
Cursor cursor = database.query("person", new String[]{"count(*)"}, null, null, null, null, null);
if(cursor.moveToNext()){
return cursor.getLong(0);
}
return 0;
}
}

package eoe.demo;

import java.util.ArrayList;
import java.util.List;
import it.bean.Person;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
*        sql           
*   SQLiteDatabase         sql        
*
*/
public class PersonSQLservice {
private MangerDatabase dbmanger;
public PersonSQLservice(Context context) {
dbmanger=new MangerDatabase(context);
}

public void save(Person person){
SQLiteDatabase database = dbmanger.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", person.getName());
values.put("age", person.getAge());
//        insert            
database.insert("person", "name", values);
}

public void update(Person person){
SQLiteDatabase database = dbmanger.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", person.getName());
values.put("age", person.getAge());
//                    

database.update("person", values, "personid=?" , new String[]{String.valueOf(person.getPersonId())});

}
public Person find(Integer id){
SQLiteDatabase database = dbmanger.getWritableDatabase();
//                                          

Cursor cursor = database.query("person", new String[]{"personid", "name", "age"},"personid=?", new String[]{String.valueOf(id)}, null, null, null);

if(cursor.moveToNext()){
return new Person(cursor.getInt(0), cursor.getString(1), cursor.getShort(2));
}
return null;

}

좋은 웹페이지 즐겨찾기