日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android软件的data使用方法,实例讲解Android中SQLiteDatabase使用方法

發布時間:2024/4/11 Android 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android软件的data使用方法,实例讲解Android中SQLiteDatabase使用方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

SQLite數據庫是android系統內嵌的數據庫,小巧強大,能夠滿足大多數SQL語句的處理工作,而SQLite數據庫僅僅是個文件而已。雖然SQLite的有點很多,但并不是如同PC端的mysql般強大,而且android系統中不允許通過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);

通過這些靜態方法可以很方便的打開和新建一個數據庫。

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:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".Main" >

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="center"

android:text="key" />

android:id="@+id/keys"

android:layout_width="100sp"

android:layout_height="wrap_content" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="center"

android:text="value" />

android:id="@+id/values"

android:layout_width="100sp"

android:layout_height="wrap_content" />

android:id="@+id/btn"

android:layout_width="100sp"

android:layout_height="wrap_content"

android:text="submit" />

android:layout_width="match_parent"

android:layout_height="wrap_content" >

android:id="@+id/lv"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

用于填充數據的mytextview.xml:

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

android:id="@+id/listkey"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="left" />

android:id="@+id/listvalue"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="300sp" />

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软件的data使用方法,实例讲解Android中SQLiteDatabase使用方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。