Created
January 6, 2013 15:51
-
-
Save meigesir/4468049 to your computer and use it in GitHub Desktop.
android使用SQLite的一个简单实例(User、UserDao、工具类DBHelp)。使用SQLite数据库:DBHelp继承SQLiteOpenHelper,必须实现其中的三个方法,其中onCreate只会执行一次,初级版本建表,其中版本更新后,表变动会自动调用onUpgrade方法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.mei.util; | |
| import android.content.Context; | |
| import android.database.sqlite.SQLiteDatabase; | |
| import android.database.sqlite.SQLiteOpenHelper; | |
| public class DBHelp extends SQLiteOpenHelper{ | |
| private static final String DATABASE_NAME = "mysqlitedb"; | |
| public DBHelp(Context context) { | |
| super(context, DATABASE_NAME, null, 1); | |
| } | |
| @Override | |
| public void onCreate(SQLiteDatabase db) { | |
| String sql = "create table t_user(_id integer primary key autoincrement,username text,password text)"; | |
| db.execSQL(sql); | |
| } | |
| @Override | |
| public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.mei.entity; | |
| public class User { | |
| private int id; | |
| private String username; | |
| private String password; | |
| public int getId() { | |
| return id; | |
| } | |
| public void setId(int id) { | |
| this.id = id; | |
| } | |
| public String getUsername() { | |
| return username; | |
| } | |
| public void setUsername(String username) { | |
| this.username = username; | |
| } | |
| public String getPassword() { | |
| return password; | |
| } | |
| public void setPassword(String password) { | |
| this.password = password; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.mei.dao; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import android.content.Context; | |
| import android.database.Cursor; | |
| import android.database.sqlite.SQLiteDatabase; | |
| import com.mei.entity.User; | |
| import com.mei.util.DBHelp; | |
| public class UserDao { | |
| private DBHelp dbHelp; | |
| private SQLiteDatabase db; | |
| public UserDao(Context context){ | |
| dbHelp = new DBHelp(context); | |
| } | |
| public void save(User user){ | |
| db = dbHelp.getWritableDatabase(); | |
| String sql = "insert into t_user(username,password) values(?,?)"; | |
| db.execSQL(sql, new String[]{user.getUsername(),user.getPassword()}); | |
| db.close(); | |
| } | |
| public void update(User user){ | |
| db = dbHelp.getWritableDatabase(); | |
| String sql = "update t_user set username = ?,password = ? where _id = ?"; | |
| db.execSQL(sql,new Object[]{user.getUsername(),user.getPassword(),user.getId()}); | |
| db.close(); | |
| } | |
| public void del(int id){ | |
| db = dbHelp.getWritableDatabase(); | |
| String sql = "delete from t_user where _id = ?"; | |
| db.execSQL(sql,new Object[]{id}); | |
| db.close(); | |
| } | |
| public List<User> findAll(){ | |
| List<User> list = new ArrayList<User>(); | |
| db = dbHelp.getReadableDatabase(); | |
| String sql = "select _id,username,password from t_user"; | |
| Cursor cursor = db.rawQuery(sql, null); | |
| while(cursor.moveToNext()){ | |
| User user = new User(); | |
| user.setId(cursor.getInt(cursor.getColumnIndex("_id"))); | |
| user.setUsername(cursor.getString(cursor.getColumnIndex("username"))); | |
| user.setPassword(cursor.getString(cursor.getColumnIndex("password"))); | |
| list.add(user); | |
| } | |
| db.close(); | |
| return list; | |
| } | |
| public User findByID(int id){ | |
| User user = null; | |
| db = dbHelp.getReadableDatabase(); | |
| String sql = "select _id,username,password from t_user where _id = ?"; | |
| Cursor cursor = db.rawQuery(sql, new String[]{String.valueOf(id)}); | |
| while(cursor.moveToNext()){ | |
| user = new User(); | |
| user.setId(cursor.getInt(cursor.getColumnIndex("_id"))); | |
| user.setUsername(cursor.getString(cursor.getColumnIndex("username"))); | |
| user.setPassword(cursor.getString(cursor.getColumnIndex("password"))); | |
| } | |
| db.close(); | |
| return user; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment