android自帶資料庫之資料插入

2021-09-01 10:15:05 字數 1515 閱讀 6423

1、建立資料庫

android 不自動提供資料庫。在 android 應用程式中使用 sqlite,必須自己建立資料庫,然後建立表、索引,填充資料。android 提供了 sqliteopenhelper 幫助你建立乙個資料庫,你只要繼承 sqliteopenhelper 類,就可以輕鬆的建立資料庫。sqliteopenhelper 類根據開發應用程式的需要,封裝了建立和更新資料庫使用的邏輯。sqliteopenhelper 的子類,實現三個方法:

建構函式,呼叫父類 sqliteopenhelper 的建構函式。這個方法需要四個引數:上下文環境(例如,乙個 activity),資料庫名字,乙個可選的游標工廠(通常是 null),乙個代表你正在使用的資料庫模型版本的整數。

oncreate()方法,它需要乙個 sqlitedatabase 物件作為引數,根據需要對這個物件填充表和初始化資料。

onupgrage() 方法,它需要三個引數,乙個 sqlitedatabase 物件,乙個舊的版本號和乙個新的版本號,這樣你就可以清楚如何把乙個資料庫從舊的模型轉變到新的模型。

**如下:

public class dbhelper extends sqliteopenhelper 

/*** 建立資料庫後,對資料庫的操作

*/public void oncreate(sqlitedatabase db)

/*** 資料庫更新的方法

*/public void onupgrade(sqlitedatabase db, int oldversion, int newversion)

}public void onopen(sqlitedatabase db)

有了這個乙個類,我們就可以建立資料庫,並執行資料庫相關的操作、下面介紹資料庫插入資料的應用。

**如下:

//例項化物件

dbhelper dbhelp=new dbhelper(this, db_name, null, version);

//獲取寫入資料物件

sqlitedatabase db=dbhelp.getwritabledatabase();

//例項化放置資料物件

contentvalues values = new contentvalues();

//資料準備

values.put(dbhelper.location, 「我的位置」);

//插入資料

db.insert(dbhelper.tb_name, dbhelper.id, values);

這個「我的位置」這個資料就被存入到資料庫當中了、當然插入資料不是只有這種方法

如下使用sql 語言

db.execsql("insert into " + dbhelper.tb_name + "(" dbhelper.location + ") values " + "("+"我的位置"+")");

這樣也可以向資料庫中插入資料、

Android系統自帶的常用資料庫

data data com.android.providers.contacts databases contacts2.db1.當需要讀取聯絡歷史的時候,需要用到系統的資料庫,現在需要去檢視原始碼android清單檔案 路徑 android 7.0.0 r1 packages providers ...

資料庫自帶角色

blkadmin 執行bulk insert 語句 dcreator 建修改刪除和還原資料庫 diskadmin 盤檔案 processadmin 管理在 sql中執行的程序 securityadmin 理伺服器登入賬戶 serveradmin 置伺服器 範圍設定 setupadmin sysadm...

Android資料庫之SQLite

在android作業系統中使用的是sqlite資料庫,我們借助sqliteopenhelper這個sqlitedatabase的工具類來建立資料庫。首先我們需要建立乙個 sqliteopenhelper的子類 class databasehelper extends sqliteopenhelper...