网上找了些Android数据库操作的代码

1、DBAdapter类: 
1 package com.cnzcom.android.quickdial;
2
3  import android.content.ContentValues;
4  import android.content.Context;
5 import android.database.Cursor;
6 import android.database.SQLException;
7 import android.database.sqlite.SQLiteDatabase;
8 import android.database.sqlite.SQLiteOpenHelper;
9 import android.util.Log;
10
11 /**
12 *
13 * @author zhangjie
14 *
15 * 数据库相关操作的类
16 */
17 public class DBAdapter {
18 /**
19 * 数据库名
20 */
21 private static final String DATABASE_NAME = "quickdial.db";
22
23 /**
24 * 数据表名
25 */
26 private static final String DATABASE_TABLE = "quickdial";
27
28 /**
29 * 数据库版本
30 */
31 private static final int DATABASE_VERSION = 1;
32
33 /**
34 * key_id :主键
35 */
36 @SuppressWarnings("unused")
37 private static final String KEY_ID = "key_id";
38
39 /**
40 * position :位置信息,表示数据是第几项
41 */
42 private static final String POSITION = "position";
43
44 /**
45 * name :姓名
46 */
47 private static final String NAME = "name";
48
49 /**
50 * phone_number :电话号码
51 */
52 private static final String PHONE_NUMBER = "phone_number";
53
54 /**
55 * ip :是否ip播出
56 */
57 private static final String IP = "ip";
58
59 /**
60 *
61 */
62 private static final String DATABASE_CREATE =
63 "create table quickdial (key_id INTEGER PRIMARY KEY, "
64 + "position INTEGER, "
65 + "name TEXT, "
66 + "phone_number TEXT, "
67 + "ip INTEGER"
68 + ");";
69
70 /**
71 *
72 */
73 private final Context context;
74
75 /**
76 *
77 */
78 private DatabaseHelper DBHelper;
79
80 /**
81 *
82 */
83 private SQLiteDatabase db;
84
85 public DBAdapter(Context ctx) {
86 context = ctx;
87 DBHelper = new DatabaseHelper(context);
88
89
90 }
91
92 private static class DatabaseHelper extends SQLiteOpenHelper {
93 public DatabaseHelper(Context context) {
94 super(context, DATABASE_NAME, null, DATABASE_VERSION);
95 }
96
97 @Override
98 public void onCreate(SQLiteDatabase db) {
99 // TODO Auto-generated method stub
100 db.execSQL(DATABASE_CREATE);
101 }
102
103 @Override
104 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
105 // TODO Auto-generated method stub
106 db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
107 onCreate(db);
108 }
109
110 }
111
112 /**
113 * 打开数据库
114 * @return
115 * @throws SQLException
116 */
117 public SQLiteDatabase open() throws SQLException
118 {
119 db = DBHelper.getWritableDatabase();
120
121 Cursor cursor = getAll();
122
123 if(cursor.getCount() == 0) {
124
125 Log.e("数据库为空", "插入数据");
126 /**
127 * 先使用假数据初始化数据库
128 */
129 for(int i = 0; i < 10; i++) {
130 insert(i, "", "", 0);
131 }
132 } else {
133 Log.e("数据库不为空", "读取数据");
134 }
135 cursor.close();
136
137 return db;
138 }
139
140 /**
141 * 关闭数据库
142 */
143 public void close()
144 {
145 DBHelper.close();
146 }
147
148 /**
149 * 向数据库中插入数据
150 */
151
152 public long insert(int position, String name, String phone_number, int ip) {
153
154 ContentValues initialValues = new ContentValues();
155 initialValues.put(POSITION, position);
156 initialValues.put(NAME, name);
157 initialValues.put(PHONE_NUMBER, phone_number);
158 initialValues.put(IP, ip);
159
160 return db.insert(DATABASE_TABLE, null, initialValues);
161 }
162
163 /**
164 * 删除数据,其实不是真正意义上的删除,而是将name = ""、phone_number = ""、ip = 0
165 */
166 public boolean delete(int position) {
167
168 ContentValues initialValues = new ContentValues();
169 initialValues.put(POSITION, position);
170 initialValues.put(NAME, "");
171 initialValues.put(PHONE_NUMBER, "");
172 initialValues.put(IP, 0);
173
174 return db.update(DATABASE_TABLE, initialValues, POSITION + "=" + position, null) > 0;
175
176 // return db.delete(DATABASE_TABLE, POSITION + "=" + position, null) > 0;
177 }
178
179 /**
180 * 更改数据
181 */
182 public boolean update(int position, String name, String phone_number, int ip) {
183
184 ContentValues initialValues = new ContentValues();
185 //initialValues.put(POSITION, position);
186 initialValues.put(NAME, name);
187 initialValues.put(PHONE_NUMBER, phone_number);
188 initialValues.put(IP, ip);
189
190 return db.update(DATABASE_TABLE, initialValues, POSITION + "=" + position, null) > 0;
191 }
192
193 public Cursor getAll() {
194
195 Cursor cur = db.query(DATABASE_TABLE,
196 null, null, null, null, null, null);
197 return cur;
198
199 }
200
201 public Cursor get(long rowId) throws SQLException {
202 Cursor cur = db.query(true, DATABASE_TABLE, new String[] {POSITION,
203 NAME,
204 PHONE_NUMBER,
205 IP},
206
207 POSITION + "=" + rowId, null, null, null, null, null);
208
209 if(cur != null) {
210 cur.moveToFirst();
211 }
212
213 return cur;
214 }
215
216 }


2、Activity中调用:
1 public DBAdapter m_DBAdapter;
2
3 m_DBAdapter = new DBAdapter(this);
4 m_DBAdapter.open();
posted @ 2011-06-02 14:11  奔跑的猪Elvis  阅读(8574)  评论(1编辑  收藏  举报