Application of Android Database

Source: Internet
Author: User

Very simple application, so I am straightforward and straightforward.

the use of databases in Android: 1. Defining class Inheritance Sqliteopenhelper  PS:  public class DBHelper extends SQLiteOpenHelper {    public DBHelper(Context context) {    super(context, DBInfo.DB.DB_NAME, null, DBInfo.DB.DB_VERSION);  } @Override//这个是数据库第一次创建的时候用 public void onCreate(SQLiteDatabase db) {  db.execSQL(DBInfo.Table.USER_INFO_CREATE); } @Override// This is the method that is called when the database upgrade version  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  db.execSQL(DBInfo.Table.USER_INFO_DROP);  onCreate(db); }}      当你完成了对数据库的操作(例如你的 Activity 已经关闭),需要调用 SQLiteDatabase 的 Close() 方法来释放掉数据库连接。
   下面是对数据库的增删改查(每种方法包括两种方法介绍:优点与缺点):  /**  * 添加一条记录到数据库  *  * @param id  * @param name  *///第一种方法 public void add(String id, String name) {// Create the database, only it executes, the database can be created in addition also can be used .  getreadabledatabase ();   SQLiteDatabase db = dbHelper.getWritableDatabase();   // 不好之处:无返回值,无法判断是否插入成功  db.execSQL("insert into user_table (userId,userName) values (?,?)",    new Object[] { id, name });  db.close(); }//第二种方法 public long addAndroid(String id, String name) {  SQLiteDatabase db = dbHelper.getWritableDatabase();  ContentValues values = new ContentValues();  values.put("userId", id);  values.put("userName", name);  // 好处:有返回值  但是当表多的时候查询就会麻烦  long result = db.insert(Table.USER_INFO_TB_NAME, null, values);// 返回值是插入的是第几行,大于0代表添加成功  db.close();  return result; }    /**  * 查询某条记录是否存在  *  * @param name  * @return  */ public boolean find(String name) {  SQLiteDatabase db = dbHelper.getReadableDatabase();  Cursor cursor = db.rawQuery(    "select * from user_table where userName = ?",    new String[] { name });  boolean result = cursor.moveToNext();  db.close();  return result; } public boolean findAndroid(String name) {  SQLiteDatabase db = dbHelper.getReadableDatabase();  Cursor cursor = db.query(Table.USER_INFO_TB_NAME, null, "userName = ?",    new String[] { name }, null, null, null);  boolean result = cursor.moveToNext();// true代表查找到了//有许多的方法,可以看api文档介绍  db.close();  return result; }   /**  * 修改一条记录  *  * @param id  * @param name  */ public void update(String id, String name) {  SQLiteDatabase db = dbHelper.getWritableDatabase();  // 缺点无返回值  db.execSQL("update user_table set userName = ? where userId = ?",    new Object[] { name, id });  db.close(); } public int updateAndroid(String id, String name) {  SQLiteDatabase db = dbHelper.getWritableDatabase();  ContentValues values = new ContentValues();//是一个map集合,谷歌封装好的 所有有key与value值。  values.put("userName", name);  // 返回值大于0代表修改更新成功  int result = db.update(Table.USER_INFO_TB_NAME, values, "userId = ?",    new String[] { id });  db.close();  return result; }   /**  * 删除一条记录  *  * @param name  */ public void delete(String name) {  SQLiteDatabase db = dbHelper.getWritableDatabase();  db.execSQL("delete from user_table where userName = ?",    new String[] { name });  db.close(); } public int deleteAndroid(String name) {  SQLiteDatabase db = dbHelper.getWritableDatabase();  int result = db.delete(Table.USER_INFO_TB_NAME, "userName = ?",    new String[] { name });// 返回值为受影响的行数,大于0代表成功  db.close();  return result; }

Application of Android Database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.