Android sqlite

转载 http://blog.csdn.net/s874154731/article/details/7086238

import
android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class DatabaseHelper extends SQLiteOpenHelper { static String dbName= "mydb.db"; static int dbVersion = 2; public DatabaseHelper(Context context) { super(context, dbName, null, dbVersion); } //只在初次使用数据库的时候会被自动调用一次 public void onCreate(SQLiteDatabase db) { Log.i("TAG","onCrete被调用了"); String sql = "create table person(personid integer primary key autoincrement," + "name varchar(20), age integer)"; db.execSQL(sql); } public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.i("TAG","onUpgrade被调用了"); String sql = "alter table person add phone char(20) null"; db.execSQL(sql); } }
import java.util.ArrayList;  
import java.util.List;  
import android.content.Context;  
import android.database.Cursor;  
import android.database.sqlite.SQLiteDatabase;  
import cn.class3g.domain.Person;  
public class PersonService {  
    private DatabaseHelper dbHelper;  
    public PersonService(Context context){  
        dbHelper = new DatabaseHelper(context);   
    }     
    public void add(Person person){  
        SQLiteDatabase db = dbHelper.getReadableDatabase();  
        String sql = "insert into person(name, age, phone) values(?,?,?)";  
        db.execSQL(sql,new Object[]{person.getName(), person.getAge(), person.getPhone()});  
    }  
        public void delete(int id){  
        SQLiteDatabase db = dbHelper.getReadableDatabase();  
        String sql = "delete from person where personid=?";  
        db.execSQL(sql,new Object[]{id});  
    }     
    public void update(Person person, int id){  
        SQLiteDatabase db = dbHelper.getReadableDatabase();  
        String sql = "update person set name=?, age=?, phone=? where personid=?";  
        db.execSQL(sql,new Object[]{person.getName(),person.getAge(),person.getPhone(), id});  
    }     
    public Person find(int id){  
        SQLiteDatabase db = dbHelper.getReadableDatabase();  
        String sql = "select * from person where personid=?";  
        Cursor cursor = db.rawQuery(sql, new String[]{String.valueOf(id) });          
        if(cursor.moveToNext()){  
            Person person = new Person();             
            person.setPersionid(cursor.getInt(0));  
            person.setName(cursor.getString(cursor.getColumnIndex("name")));  
            person.setAge(cursor.getInt(2));  
            person.setPhone(cursor.getString(3));             
            return person;  
        }         
        return null;  
    }     
    public int getRecordsCount(){  
        SQLiteDatabase db = dbHelper.getReadableDatabase();  
        String sql = "select count(*) from person";  
        Cursor cursor = db.rawQuery(sql, null);  
        cursor.moveToFirst();  
        int count = cursor.getInt(0);  
        cursor.close();  
        return count;  
    }     
    //跳过前面的start条记录,读取其后count记录  
    public List<Person> getScrollData(int start, int count){        
        SQLiteDatabase db = dbHelper.getReadableDatabase();  
        String sql = "select * from person limit ?,?";  
        Cursor cursor = db.rawQuery(sql,  
                        new String[] { String.valueOf(start),  
                                       String.valueOf(count) });  
        List<Person> list = new ArrayList<Person>();          
        while(cursor.moveToNext()){  
            Person p = new Person();  
            p.setPersionid(cursor.getInt(0));  
            p.setName(cursor.getString(1));  
            p.setAge(cursor.getInt(2));           
            list.add(p);  
        }         
        cursor.close();  
        return list;  
    }  
} 

 

posted @ 2015-11-10 11:10  webglcn  阅读(181)  评论(0编辑  收藏  举报