代码改变世界

一个Android操作SQLite的完整示例

  莫耶  阅读(1724)  评论(1)    收藏  举报

DbHelper类代码:

复制代码
  1 package com.locke.android;
2
3 import java.text.SimpleDateFormat;
4 import java.util.ArrayList;
5 import java.util.Date;
6 import java.util.List;
7
8 import android.content.ContentValues;
9 import android.content.Context;
10 import android.database.Cursor;
11 import android.database.sqlite.SQLiteDatabase;
12 import android.database.sqlite.SQLiteException;
13 import android.database.sqlite.SQLiteOpenHelper;
14 import android.util.Log;
15
16 import com.locke.android.model.User;
17
18 public class DbHelper extends SQLiteOpenHelper {
19 private final static String TAG = "DbHelper"; //程序标志
20 private final static String DATABASE_NAME = "locke_db";
21 private final static int DATABASE_VERSION = 1;
22 private final static String TABLE_NAME = "tbl_users";
23 private final static String CREATE_TABLE_USERS =
24 "CREATE TABLE IF NOT EXISTS tbl_users(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,password TEXT,regdate date)";
25 private final static String DROP_TABLE_USERS =
26 "DROP TABLE IF EXIST tbl_users";
27 private final static String FIELD_ID = "id";
28 public final static String SQL_RAWQUERY ="select id,name,password,regdate FROM tbl_users";
29
30 public DbHelper(Context context){
31 super(context,DATABASE_NAME,null,DATABASE_VERSION);
32 }
33
34 @Override
35 public void onCreate(SQLiteDatabase db){
36 db.execSQL(CREATE_TABLE_USERS);
37 }
38
39 @Override
40 public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
41 db.execSQL(DROP_TABLE_USERS);
42 onCreate(db);
43 }
44
45 /*
46 * 添加一条User记录
47 * @param user 用户记录
48 * @return 返回插入的行 id
49 */
50 public long addUser(User user){
51 SQLiteDatabase db = null;
52 try{
53 //转化SQLite日期格式
54 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
55 ContentValues values = new ContentValues();
56 values.put("name", user.GetName());
57 values.put("password", user.GetPassword());
58 values.put("regdate", dateFormat.format(user.GetRegdate()) );
59
60 db = this.getWritableDatabase();
61
62 long result = db.insert(TABLE_NAME, null, values);
63 return result;
64 }catch(SQLiteException e){
65 return -1;
66 }finally{
67 if(db.isOpen())
68 db.close();
69 }
70 }
71
72 /*
73 * 查询记录
74 * @return 获取到的行记录
75 */
76 public List<User> queryUsers(){
77 Cursor c = null;
78 SQLiteDatabase db = null;
79 List<User> result = new ArrayList<User>();
80 try
81 {
82 db = this.getReadableDatabase();
83 c = db.query(TABLE_NAME,new String[]{"id","name","password","regdate"}, null,null,null,null,null,"id asc");
84
85 c.moveToFirst();
86 while(!c.isAfterLast()){
87 User user = new User();
88 user.SetId(c.getInt(0));
89 user.SetName(c.getString(1));
90 user.SetPassword(c.getString(2));
91 user.SetRegdate(new Date(c.getString(0)));
92
93 result.add(user);
94 c.moveToNext();
95 }
96 return result;
97 }catch(SQLiteException e){
98 Log.e(TAG, e.getMessage());
99 return null;
100 }finally{
101 if(!c.isClosed()){
102 c.close();
103 }
104 if(db.isOpen()){
105 db.close();
106 }
107 }
108 }
109
110 /*
111 * 删除一条用户记录
112 * @return 是否删除成功
113 */
114 public boolean delete(User user)
115 {
116 SQLiteDatabase db=null;
117 try{
118 db=this.getWritableDatabase();
119 String where=FIELD_ID+"=?";
120 String[] whereValue={Integer.toString(user.GetId())};
121 long result = db.delete(TABLE_NAME, where, whereValue);
122 return result > 0;
123 }catch(SQLiteException e){
124 return false;
125 }finally{
126 if(db.isOpen()){
127 db.close();
128 }
129 }
130 }
131
132 /*
133 * 更新一条记录
134 * @return 是否更新成功
135 */
136 public boolean update(User user){
137 SQLiteDatabase db = null;
138 try{
139 db = this.getWritableDatabase();
140 String where=FIELD_ID+"=?";
141 String[] whereValue={Integer.toString(user.GetId())};
142 ContentValues values=new ContentValues();
143 values.put("name", user.GetName());
144 values.put("password", user.GetPassword());
145
146 long result = db.update(TABLE_NAME, values, where, whereValue);
147 return result>0;
148 }catch(SQLiteException e){
149 return false;
150 }finally{
151 if(db.isOpen()){
152 db.close();
153 }
154 }
155 }
156 }
复制代码

项目源码:

https://files.cnblogs.com/moye/SQLiteTest.zip

点击右上角即可分享
微信分享提示