實(shí)例講解Android中SQLiteDatabase使用方法
SQLite數(shù)據(jù)庫是android系統(tǒng)內(nèi)嵌的數(shù)據(jù)庫,小巧強(qiáng)大,能夠滿足大多數(shù)SQL語句的處理工作,而SQLite數(shù)據(jù)庫僅僅是個(gè)文件而已。雖然SQLite的有點(diǎn)很多,但并不是如同PC端的mysql般強(qiáng)大,而且android系統(tǒng)中不允許通過JDBC操作遠(yuǎn)程數(shù)據(jù)庫,所以只能通過webservice等手段于php、servlet交互獲取數(shù)據(jù)。
SQLiteDatabase類,代表了一個(gè)數(shù)據(jù)庫對(duì)象,通過SQLiteDatabase來操作管理數(shù)據(jù)庫。
一些基本的用法:
static SQLiteDatabase openDatabase(String path,SQLiteDatabase.CUrsorFactory factory,int flag);
static SQLiteDatabase openOrCreateDatabase(File file,SQLiteDatabase.CursorFactory factory);
static SQLiteDatabase openOrCreateDatabase(String path,SQLiteDatabse.CursorFactory factory);
通過這些靜態(tài)方法可以很方便的打開和新建一個(gè)數(shù)據(jù)庫。
1、execSQL(String sql,Object[] bindArgs)
2、execSQL(String sql)
3、rawQuery(String sql,String[] selectionArgs);
4、beginTransaction()
5、endTransaction()
這些函數(shù)可以完成SQL功能,對(duì)于查詢出來的結(jié)果是用Cursor表示的,類似于JDBC中的ResultSet類,在這些類中通過方法move(int offset)、moveToFirst()、moveToLast()、moveToNext()、moveToPosition(int position)、moveToPrivious()獲取需要的結(jié)果行。
下面通過一個(gè)實(shí)例來說明一下SQLiteDatabase的基本使用:
main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".Main" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="key" /> <EditText android:id="@+id/keys" android:layout_width="100sp" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="value" /> <EditText android:id="@+id/values" android:layout_width="100sp" android:layout_height="wrap_content" /> <Button android:id="@+id/btn" android:layout_width="100sp" android:layout_height="wrap_content" android:text="submit" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
用于填充數(shù)據(jù)的mytextview.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/listkey" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" /> <TextView android:id="@+id/listvalue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="300sp" /> </LinearLayout>
Main.java
package com.app.main; import android.annotation.SuppressLint; import android.app.Activity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CursorAdapter; import android.widget.EditText; import android.widget.ListView; import android.widget.SimpleCursorAdapter; public class Main extends Activity { EditText ed1 = null; EditText ed2 = null; Button btn = null; ListView lv = null; SQLiteDatabase db = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ed1 = (EditText) this.findViewById(R.id.keys); ed2 = (EditText) this.findViewById(R.id.values); btn = (Button) this.findViewById(R.id.btn); lv = (ListView) this.findViewById(R.id.lv); db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString() + "/my.db3", null); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { String key = ed1.getText().toString(); String value = ed2.getText().toString(); try { insertData(db, key, value); Cursor cursor = db.rawQuery("select * from tb_info", null); inflateListView(cursor); } catch (Exception e) { String sql = "create table tb_info(_id integer primary key autoincrement,db_key varchar(20),db_value varchar(50))"; db.execSQL(sql); insertData(db, key, value); Cursor cursor = db.rawQuery("select * from tb_info", null); inflateListView(cursor); } } }); } // 向數(shù)據(jù)庫中插入數(shù)據(jù) private void insertData(SQLiteDatabase db, String key, String value) { db.execSQL("insert into tb_info values (null,?,?)", new String[] { key, value }); System.out.println("------------------"); } // 向ListView中填充數(shù)據(jù) @SuppressLint("NewApi") public void inflateListView(Cursor cursor) { SimpleCursorAdapter adapter = new SimpleCursorAdapter(Main.this, R.layout.mytextview, cursor, new String[] { "db_key", "db_value" }, new int[] { R.id.listkey, R.id.listvalue }, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); lv.setAdapter(adapter); } @Override protected void onDestroy() { super.onDestroy(); if (db != null && db.isOpen()) { db.close(); } } }
實(shí)現(xiàn)的效果:
需要特別指出,在用SimpleCursorAdapter封裝Cursor的時(shí)候,要求底層數(shù)據(jù)庫表的主鍵列的列名為_id,因?yàn)镾impleCursorAdapter只能識(shí)別主鍵列名為_id的表。
以上就是本文的全部內(nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Android輸入框?qū)崟r(shí)模糊搜索效果的示例代碼
這篇文章主要介紹了Android輸入框?qū)崟r(shí)模糊搜索效果的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08flutter?Bloc?add兩次只響應(yīng)一次問題解析
這篇文章主要為大家介紹了flutter?Bloc?add兩次只響應(yīng)一次問題解析記錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Android利用Sensor實(shí)現(xiàn)傳感器功能
這篇文章主要為大家詳細(xì)介紹了Android利用Sensor實(shí)現(xiàn)傳感器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11Android實(shí)戰(zhàn)教程第六篇之一鍵鎖屏應(yīng)用問題解決
這篇文章主要為大家詳細(xì)介紹了Android一鍵鎖屏應(yīng)用開發(fā)過程中出現(xiàn)問題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android之用PopupWindow實(shí)現(xiàn)彈出菜單的方法詳解
本篇文章是對(duì)在Android中,用PopupWindow實(shí)現(xiàn)彈出菜單的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06Android使用Handler實(shí)現(xiàn)倒計(jì)時(shí)功能
這篇文章主要為大家詳細(xì)介紹了Android使用Handler實(shí)現(xiàn)倒計(jì)時(shí)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Android自定義View實(shí)現(xiàn)縱向跑馬燈效果詳解
對(duì)于跑馬燈效果在我們?nèi)粘J褂玫腶pp中還是很常見的,比如外賣app的商家公告就使用了此效果,但是它是橫向滾動(dòng)的,橫向滾動(dòng)多適用于單條信息;但凡涉及到多條信息的滾動(dòng)展示,用縱向滾動(dòng)效果會(huì)有更好的用戶體驗(yàn),今天我們通過自定義View來看看如何實(shí)現(xiàn)縱向跑馬燈效果。2016-11-11Retrofit Rxjava實(shí)現(xiàn)圖片下載、保存并展示實(shí)例
本篇文章主要介紹了Retrofit Rxjava實(shí)現(xiàn)圖片下載、保存并展示實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06