Android之SQLite學習

來源:互聯網
上載者:User

我們在使用SQLite的時候,一般都是直接封裝一個SQLiteOpenHelper類,然後對類進行操作,所以這裡我主要講一下如何使用SQLiteOpenHelper。

package com.android.liu.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDataBaseHelper {
 
 public static final String TAG="MyDataBaseHelper";
 
 public static final String KEY_ID="_id";
 
 public static final String KEY_USERNAME="username";
 
 public static final String KEY_SEX="sex";
 
 public static final String DB_NAME="user.db";
 
 public static final String DB_TABLE="userTbl";
 
 public static final int DB_VERSION=1;
 
 public static final String DB_CREATE= "CREATE TABLE "+ DB_TABLE+" ("+ KEY_ID+" INTEGER PRIMARY KEY,"+KEY_USERNAME+" TEXT"+KEY_SEX+" TEXT)";
 
 private Context mContext=null;
 
 private SQLiteDatabase mSQLiteDatabase=null;
 
 private DatabaseHelper mDatabaseHelper=null;
 
 private static class DatabaseHelper extends SQLiteOpenHelper{

  public DatabaseHelper(Context context) {
   super(context, DB_NAME, null, DB_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
   db.execSQL(DB_CREATE);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   db.execSQL("DROP DB_TABLE IF EXISTS NOTES");
   onCreate(db);
  }};

  public MyDataBaseHelper(Context context)
  {
   mContext=context;
  }
  
  public void open() throws SQLException
  {
   mDatabaseHelper=new DatabaseHelper(mContext);
   mSQLiteDatabase=mDatabaseHelper.getWritableDatabase();
  }
  
  public void close()
  {
   mDatabaseHelper.close();
  }
  
  /**插入一條資料*/
  public long insertData(int num,String data,String sex)
  {
   ContentValues initialValues=new ContentValues();
   initialValues.put(KEY_ID, num);
   initialValues.put(KEY_USERNAME, data);
   initialValues.put(KEY_SEX, sex);
   return mSQLiteDatabase.insert(DB_TABLE, KEY_ID, initialValues);
  }
  
  /**刪除一條資料*/
  public boolean deleteData(long rowId)
  {
   return mSQLiteDatabase.delete(DB_TABLE, KEY_ID+"="+rowId,null)>0;
  }
  
  /**
   * 查詢所有資料
   * */
  public Cursor fetchAllData()
  {
   return mSQLiteDatabase.query(DB_TABLE, new String[]{KEY_ID,KEY_USERNAME,KEY_SEX},null,null,null,null,null);
  }
  
  /**查詢指定資料*/
  
  public Cursor fetchData(long rowId) throws SQLException
  {
   Cursor mCursor=mSQLiteDatabase.query(true, DB_TABLE, new String[]{KEY_ID,KEY_USERNAME,KEY_SEX}, KEY_ID+"="+rowId,null,null,null,null,null);
   if(mCursor!=null)
   {
    mCursor.moveToFirst();
   }
   
   return mCursor;
  }
  
  /**
   * 更新一條資料*/
  public boolean updateData(long rowId,String username,String sex)
  {
   ContentValues args=new ContentValues();
   args.put(KEY_USERNAME, username);
   args.put(KEY_SEX, sex);
   return mSQLiteDatabase.update(DB_TABLE, args, KEY_ID+"="+rowId, null)>0;
  }
    
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.