SQLite数据操作

1. DataBaseOpenHelp:

 1 package com.vanceinfo.service;
2
3 import android.content.Context;
4 import android.database.sqlite.SQLiteDatabase;
5 import android.database.sqlite.SQLiteOpenHelper;
6
7 public class DataBaseOpenHelp extends SQLiteOpenHelper
8 {
9
10 private static final String SQLITE_NAME = "itcast";
11
12 private static final int version = 1;
13
14 public DataBaseOpenHelp(Context context)
15 {
16 super(context, SQLITE_NAME, null, version);
17
18 }
19
20 @Override
21 public void onCreate(SQLiteDatabase db)
22 {
23 db.execSQL("CREATE TABLE person (personid integer primary key autoincrement,name varchar(20),age INTEGER)");
24 }
25
26 @Override
27 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
28 {
29 db.execSQL("DROP TABLE IF EXISTS person");
30 onCreate(db);
31 }
32
33 }

 

2. PersonService

 

  1 package com.vanceinfo.service;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import android.content.Context;
7 import android.database.Cursor;
8 import android.database.sqlite.SQLiteDatabase;
9
10 import com.vanceinfo.domian.Person;
11
12 public class PersonService
13 {
14 private DataBaseOpenHelp dbOpenHelp;
15
16 public PersonService(Context context)
17 {
18 dbOpenHelp = new DataBaseOpenHelp(context);
19 }
20
21 /**
22 * 保存
23 *
24 * @param person
25 */
26 public void save(Person person)
27 {
28 SQLiteDatabase database = dbOpenHelp.getWritableDatabase();
29 database.execSQL("insert into person(name,age) values(?,?)", new Object[] {person.getName(), person.getAge()});
30 }
31
32 /**
33 * 更新
34 *
35 * @param person
36 */
37 public void update(Person person)
38 {
39 SQLiteDatabase database = dbOpenHelp.getWritableDatabase();
40 database.execSQL("update person set name=? ,age=? where personid=?",
41 new Object[] {person.getName(), person.getAge(), person.getPersonid()});
42 }
43
44 /**
45 * 删除
46 *
47 * @param ids
48 */
49 public void delete(Integer... ids)
50 {
51 if (ids.length > 0)
52 {
53 StringBuilder sb = new StringBuilder();
54 for (Integer id : ids)
55 {
56 sb.append('?').append(',');
57 }
58 sb.deleteCharAt(sb.length() - 1);
59 SQLiteDatabase database = dbOpenHelp.getWritableDatabase();
60 database.execSQL("delete from person where personid in(" + sb + ")", (Object[]) ids);
61 }
62 }
63
64 /**
65 * 查询
66 *
67 * @param id
68 * @return
69 */
70 public Person find(Integer id)
71 {
72 SQLiteDatabase database = dbOpenHelp.getWritableDatabase();
73 Cursor cursor = database.rawQuery("select * from person where personid=?", new String[] {String.valueOf(id)});
74 while (cursor.moveToNext())
75 {
76 return new Person(cursor.getInt(0), cursor.getString(1), cursor.getShort(2));
77 }
78 return null;
79 }
80
81 /**
82 * 条件查询
83 *
84 * @param start
85 * @return
86 */
87 public List<Person> getScrollData(int startResult, int maxResult)
88 {
89 List<Person> persons = new ArrayList<Person>();
90 SQLiteDatabase database = dbOpenHelp.getWritableDatabase();
91 Cursor cursor = database.rawQuery("select * from person limit ?,?", new String[] {String.valueOf(startResult),
92 String.valueOf(maxResult)});
93 while (cursor.moveToNext())
94 {
95 persons.add(new Person(cursor.getInt(0), cursor.getString(1), cursor.getShort(2)));
96 }
97 return persons;
98
99 }
100
101 /**
102 * 获得数量
103 *
104 * @return
105 */
106 public long getCount()
107 {
108 SQLiteDatabase database = dbOpenHelp.getWritableDatabase();
109 Cursor cursor = database.rawQuery("select count(*) from person", null);
110 if (cursor.moveToNext())
111 {
112 return cursor.getLong(0);
113 }
114 return 0;
115
116 }
117
118 public void drop()
119 {
120 SQLiteDatabase database = dbOpenHelp.getWritableDatabase();
121 database.execSQL("drop table person");
122 }
123
124 }




posted @ 2011-10-30 15:44  程序学习笔记  阅读(250)  评论(0编辑  收藏  举报