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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

android sqlite 示例,android SQLite数据库使用示例

發(fā)布時(shí)間:2025/3/15 数据库 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android sqlite 示例,android SQLite数据库使用示例 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

數(shù)據(jù)庫的操作,需用到數(shù)據(jù)庫類SQLiteDatabase,重寫管理員類SQLiteOpenHelper,用Cursor查詢數(shù)據(jù)

一、操作數(shù)據(jù)庫的輔助類,包含打開、關(guān)閉、增刪改查方法

//自定義的數(shù)據(jù)庫的接口,其中包含SQLiteHelper

public class NotesDbAdapter

{

//創(chuàng)建數(shù)據(jù)庫所需語句及相關(guān)字符串

private static final String DATABASE_NAME = "note.db";

private static final int DATABASE_VERSION = 1;

private static final String DATABASE_TABLE = "note";

private static final String DATABASE_CREATE =

"create table note("

+"_id INTEGER PRIMARY KEY,"

+"note TEXT,"

+"date TEXT,"

+"created INTEGER,"

+"modified INTEGER"

+");";

//數(shù)據(jù)庫對(duì)象

private SQLiteDatabase db;

//定義數(shù)據(jù)庫管理員類

private static class DatabaseHelper extends

SQLiteOpenHelper

{

public DatabaseHelper(Context context) {

super(context, DATABASE_NAME, null, DATABASE_VERSION);

// TODO Auto-generated constructor stub

}

@Override

public void onCreate(SQLiteDatabase db) {

// TODO Auto-generated method stub

db.execSQL(DATABASE_CREATE);

}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int

newVersion) {

// TODO Auto-generated method stub

db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE);

onCreate(db);

}

}

//抽象界面

private Context mCtx =

null;

//數(shù)據(jù)庫管理員對(duì)象

private DatabaseHelper dbHelper ;

//數(shù)據(jù)庫各列名稱

public static final String KEY_ROWID = "_id";

public static final String KEY_NOTE = "note";

public static final String KEY_DATE = "date";

public static final String KEY_CREATED = "created";

public NotesDbAdapter(Context ctx) {

this.mCtx = ctx;

}

//打開數(shù)據(jù)庫

public NotesDbAdapter open () throws SQLException {

dbHelper = new DatabaseHelper(mCtx);

db = dbHelper.getWritableDatabase();

return this;

}

//關(guān)閉數(shù)據(jù)庫

public void close() {

dbHelper.close();

}

//查詢所有項(xiàng)目,返回一個(gè)Cursor

public Cursor getall()

{

return db.query(DATABASE_TABLE,

new String[] {KEY_ROWID, KEY_NOTE, KEY_DATE,

KEY_CREATED},

null, null, null, null, null);

}

// 新增一項(xiàng)

public long create(String Note,String mDate) {

Date now = new Date();

ContentValues args = new ContentValues();

args.put(KEY_NOTE, Note);

args.put(KEY_DATE, mDate);

args.put(KEY_CREATED, now.getTime());

return db.insert(DATABASE_TABLE, null, args);

}

//刪除一項(xiàng)

public boolean delete(long rowId) {

return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null)

> 0;

}

//查詢一項(xiàng)

public Cursor get(long rowId) throws SQLException

{

Cursor mCursor = db.query(true,

DATABASE_TABLE,

new String[] {KEY_ROWID, KEY_NOTE, KEY_DATE,

KEY_CREATED},

KEY_ROWID + "=" + rowId,

null, null, null, null, null);

if (mCursor != null) {

mCursor.moveToFirst();

}

return mCursor;

}

//修改一項(xiàng)

public boolean update(long rowId, String note,String

date)

{

ContentValues args = new ContentValues();

args.put(KEY_NOTE, note);

args.put(KEY_DATE, date);

return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId,

null) > 0;

}

}

二、在主類中通過寫好的輔助類操作數(shù)據(jù)庫

private NotesDbAdapter mDbHelper;

private Cursor mNotesCursor;

//用自定義的數(shù)據(jù)庫接口提供的方法打開數(shù)據(jù)庫

mDbHelper = new NotesDbAdapter(this);

mDbHelper.open();

//獲取cursor,當(dāng)需要表中數(shù)據(jù)時(shí)再通過cursor訪問

mNotesCursor = mDbHelper.getall();

startManagingCursor(mNotesCursor);

//實(shí)例化各list存儲(chǔ)數(shù)據(jù)庫各列內(nèi)容

aryListNoteContent=new

ArrayList();

aryListNoteDate=new

ArrayList();

aryListNoteID=new

ArrayList();

linkListNoteIcon=new

LinkedList();

//先用Cursor獲取內(nèi)容,裝入ArrayList,最后關(guān)閉cursor

try

{

if(mNotesCursor.moveToFirst())

{

do

{

//獲取數(shù)據(jù)庫rowID,以便操作數(shù)據(jù)庫

String noteId=mNotesCursor.getString(0);

String noteContent=mNotesCursor.getString(1);

String noteDate=mNotesCursor.getString(2);

aryListNoteID.add(noteId);

aryListNoteContent.add(noteContent);

aryListNoteDate.add(noteDate);

linkListNoteIcon.add(getApplicationContext().getResources().getDrawable(R.drawable.icon));

}while(mNotesCursor.moveToNext());

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally

{

mNotesCursor.close();

}

//刪除一項(xiàng)

String RowId=aryListNoteID.get(info.position);

mDbHelper.delete(Long.parseLong(RowId));

//新增一項(xiàng)

mDbHelper.create(String,String);

//查詢一項(xiàng)

//若該行id不為空,返回該行cursor,根據(jù)cursor讀取內(nèi)容,讀取完畢關(guān)閉cursor

if (mRowId != null) {

Cursor cursor = mDbHelper.get(mRowId);

startManagingCursor(cursor);

try

{

mEditText.setText(cursor.getString(

cursor.getColumnIndexOrThrow(NotesDbAdapter.KEY_NOTE)

));

} catch (IllegalArgumentExceptione) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally

{

cursor.close();

}

}

//修改一項(xiàng)

mDbHelper.update(mRowId, String,String);

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的android sqlite 示例,android SQLite数据库使用示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。