您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關Android中如何使用SQLite,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
SQLite介紹
SQLite是輕量級的、嵌入式的、關系型數(shù)據(jù)庫,目前已經(jīng)在iPhone、Android等手機系統(tǒng)中使用,SQLite可移植性好,很容易使用,很小,高效而且可靠。SQLite嵌入到使用它的應用程序中,它們共用相同的進程空間,而不是單獨的一個進程。從外部看,它并不像一個RDBMS,但在進程內部,它卻是完整的,自包含的數(shù)據(jù)庫引擎。
在android中當需要操作SQLite數(shù)據(jù)庫的時候需要得到一個SQLiteOpenHelper對象,而SQLiteOpenHelper是一個抽象類,用戶需要繼承這個類,并實現(xiàn)該類中的一些方法。
1、繼承SQLiteOpenHelper之后就擁有了以下兩個方法:
◆getReadableDatabase() 創(chuàng)建或者打開一個查詢數(shù)據(jù)庫
◆getWritableDatabase()創(chuàng)建或者打開一個可寫數(shù)據(jù)庫
◆他們都會返回SQLiteDatabase對象,用戶通過得到的SQLiteDatabase對象進行后續(xù)操作
2、同時用戶還可以覆蓋以下回調函數(shù),再對數(shù)據(jù)庫進行操作的時候回調以下方法:
◆onCreate(SQLiteDatabase):在數(shù)據(jù)庫***次創(chuàng)建的時候會調用這個方法,一般我們在這個方法里邊創(chuàng)建數(shù)據(jù)庫表。
◆onUpgrade(SQLiteDatabase,int,int):當數(shù)據(jù)庫需要修改的時候,Android系統(tǒng)會主動的調用這個方法。一般我們在這個方法里邊刪除數(shù)據(jù)庫表,并建立新的數(shù)據(jù)庫表,當然是否還需要做其他的操作,完全取決于應用程序的需求。
◆onOpen(SQLiteDatabase):這是當打開數(shù)據(jù)庫時的回調函數(shù),一般也不會用到。
需要注意
1、在SQLiteOepnHelper的子類當中,必須有以下該構造函數(shù)
public DatabaseHelper(Context context, String name, CursorFactory factory, int version) { //必須通過super調用父類當中的構造函數(shù) super(context, name, factory, version); }
為了方便,也可以創(chuàng)建其它的構造函數(shù),含二個參數(shù)或者三個參數(shù)的。
2、函數(shù)public void onCreate(SQLiteDatabase db)是在調用getReadableDatabase()或者是getWritableDatabase()***次創(chuàng)建數(shù)據(jù)庫的時候執(zhí)行,實際上是在***次得到SQLiteDatabse對象的時候,才會調用這個方法.
public void onCreate(SQLiteDatabase db) { System.out.println("create a Database"); //execSQL函數(shù)用于執(zhí)行SQL語句 db.execSQL("create table user(id int,name varchar(20))"); }
在向數(shù)據(jù)庫的表中插入記錄時,需要先將數(shù)據(jù)包含在一個ContentValues中,向該對象當中插入鍵值對,其中鍵是列名,值是希望插入到這一列的值,值必須和數(shù)據(jù)庫當中的數(shù)據(jù)類型一致。接著調用Databasehelper的getWritableDatabase方法來獲得可以寫入的Databasehelper對象,再向其中insert記錄。注意調用DatabaseHelper對象的insert,update或者query方法的參數(shù)的傳遞。
另外執(zhí)行query方法后,返回的是一個Cursor游標,游標最開始指向的是記錄集合中***行的上一行,因此首先需要先調用cursor.next()將游標移動到記錄集合的***行,接著再獲取數(shù)據(jù)即可。
Java代碼
public class SQLiteActivity extends Activity { /** Called when the activity is first created. */ private Button createButton; private Button insertButton; private Button updateButton; private Button updateRecordButton; private Button queryButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); createButton = (Button)findViewById(R.id.createDatabase); updateButton = (Button)findViewById(R.id.updateDatabase); insertButton = (Button)findViewById(R.id.insert); updateRecordButton = (Button)findViewById(R.id.update); queryButton = (Button)findViewById(R.id.query); createButton.setOnClickListener(new CreateListener()); updateButton.setOnClickListener(new UpdateListener()); insertButton.setOnClickListener(new InsertListener()); updateRecordButton.setOnClickListener(new UpdateRecordListener()); queryButton.setOnClickListener(new QueryListener()); } class CreateListener implements OnClickListener{ @Override public void onClick(View v) { //創(chuàng)建一個DatabaseHelper對象 DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,"test_mars_db"); //只有調用了DatabaseHelper對象的getReadableDatabase()方法,或者是getWritableDatabase()方法之后,才會創(chuàng)建,或打開一個數(shù)據(jù)庫 SQLiteDatabase db = dbHelper.getReadableDatabase(); } } class UpdateListener implements OnClickListener{ @Override public void onClick(View v) { DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,"test_mars_db",2); SQLiteDatabase db = dbHelper.getReadableDatabase(); } } class InsertListener implements OnClickListener{ @Override public void onClick(View v) { //生成ContentValues對象 ContentValues values = new ContentValues(); //想該對象當中插入鍵值對,其中鍵是列名,值是希望插入到這一列的值,值必須和數(shù)據(jù)庫當中的數(shù)據(jù)類型一致 values.put("id", 1); values.put("name","zhangsan"); DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,"test_mars_db",2); SQLiteDatabase db = dbHelper.getWritableDatabase(); //調用insert方法,就可以將數(shù)據(jù)插入到數(shù)據(jù)庫當中 db.insert("user", null, values); } } //更新操作就相當于執(zhí)行SQL語句當中的update語句 //UPDATE table_name SET XXCOL=XXX WHERE XXXXCOL=XX... class UpdateRecordListener implements OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub //得到一個可寫的SQLiteDatabase對象 DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,"test_mars_db"); SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("name", "zhangsanfeng"); //***個參數(shù)是要更新的表名 //第二個參數(shù)是一個ContentValeus對象 //第三個參數(shù)是where子句 db.update("user", values, "id=?", new String[]{"1"}); } } class QueryListener implements OnClickListener{ @Override public void onClick(View v) { System.out.println("aaa------------------"); Log.d("myDebug", "myFirstDebugMsg"); DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,"test_mars_db"); SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor cursor = db.query("user", new String[]{"id","name"}, "id=?", new String[]{"1"}, null, null, null); while(cursor.moveToNext()){ String name = cursor.getString(cursor.getColumnIndex("name")); System.out.println("query--->" + name); } } } }
以上就是Android中如何使用SQLite,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。