亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

實例講解Android中SQLiteDatabase使用方法

 更新時間:2016年05月25日 14:48:10   作者:summerpxy  
這篇文章主要以一個簡單的實例為大家詳細講解Android中SQLiteDatabase使用方法,感興趣的小伙伴們可以參考一下

SQLite數據庫是android系統(tǒng)內嵌的數據庫,小巧強大,能夠滿足大多數SQL語句的處理工作,而SQLite數據庫僅僅是個文件而已。雖然SQLite的有點很多,但并不是如同PC端的mysql般強大,而且android系統(tǒng)中不允許通過JDBC操作遠程數據庫,所以只能通過webservice等手段于php、servlet交互獲取數據。

SQLiteDatabase類,代表了一個數據庫對象,通過SQLiteDatabase來操作管理數據庫。

一些基本的用法:

  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)方法可以很方便的打開和新建一個數據庫。

1、execSQL(String sql,Object[] bindArgs)

2、execSQL(String sql)

3、rawQuery(String sql,String[] selectionArgs);

4、beginTransaction()

5、endTransaction()

這些函數可以完成SQL功能,對于查詢出來的結果是用Cursor表示的,類似于JDBC中的ResultSet類,在這些類中通過方法move(int offset)、moveToFirst()、moveToLast()、moveToNext()、moveToPosition(int position)、moveToPrivious()獲取需要的結果行。

下面通過一個實例來說明一下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>

用于填充數據的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);
        }

      }

    });

  }

  // 向數據庫中插入數據
  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中填充數據
  @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();
    }
  }

}

實現的效果:

需要特別指出,在用SimpleCursorAdapter封裝Cursor的時候,要求底層數據庫表的主鍵列的列名為_id,因為SimpleCursorAdapter只能識別主鍵列名為_id的表。

以上就是本文的全部內容,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Android輸入框實時模糊搜索效果的示例代碼

    Android輸入框實時模糊搜索效果的示例代碼

    這篇文章主要介紹了Android輸入框實時模糊搜索效果的示例代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • flutter?Bloc?add兩次只響應一次問題解析

    flutter?Bloc?add兩次只響應一次問題解析

    這篇文章主要為大家介紹了flutter?Bloc?add兩次只響應一次問題解析記錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • android底層去掉虛擬按鍵的實例講解

    android底層去掉虛擬按鍵的實例講解

    今天小編就為大家分享一篇android底層去掉虛擬按鍵的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Android利用Sensor實現傳感器功能

    Android利用Sensor實現傳感器功能

    這篇文章主要為大家詳細介紹了Android利用Sensor實現傳感器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Android實戰(zhàn)教程第六篇之一鍵鎖屏應用問題解決

    Android實戰(zhàn)教程第六篇之一鍵鎖屏應用問題解決

    這篇文章主要為大家詳細介紹了Android一鍵鎖屏應用開發(fā)過程中出現問題的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android之用PopupWindow實現彈出菜單的方法詳解

    Android之用PopupWindow實現彈出菜單的方法詳解

    本篇文章是對在Android中,用PopupWindow實現彈出菜單的方法進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • Android使用Handler實現倒計時功能

    Android使用Handler實現倒計時功能

    這篇文章主要為大家詳細介紹了Android使用Handler實現倒計時功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Android 實現自己的LOG信息

    Android 實現自己的LOG信息

    本文主要講解Android LOG,這里對如何創(chuàng)建自己的Android LOG信息做了詳細的介紹,并附簡單代碼示例,有需要的小伙伴可以參考下
    2016-08-08
  • Android自定義View實現縱向跑馬燈效果詳解

    Android自定義View實現縱向跑馬燈效果詳解

    對于跑馬燈效果在我們日常使用的app中還是很常見的,比如外賣app的商家公告就使用了此效果,但是它是橫向滾動的,橫向滾動多適用于單條信息;但凡涉及到多條信息的滾動展示,用縱向滾動效果會有更好的用戶體驗,今天我們通過自定義View來看看如何實現縱向跑馬燈效果。
    2016-11-11
  • Retrofit Rxjava實現圖片下載、保存并展示實例

    Retrofit Rxjava實現圖片下載、保存并展示實例

    本篇文章主要介紹了Retrofit Rxjava實現圖片下載、保存并展示實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06

最新評論