Android下的資料儲存之SQLite資料庫

2021-07-24 17:22:44 字數 2077 閱讀 7337

第一步:

寫個類 ,繼承 sqliteopenhelper

public

classmydatabaseopenhelperextendssqliteopenhelper {}

第二步:

新增乙個建構函式,並且  指定必要的引數,

// context :

應用程式上下文

// name : 

資料庫的名稱

// factory : 

游標工廠

// version : 

資料庫的

版本publicmydatabaseopenhelper(context context)

並且在oncreate方法中,通過 db物件來執行sql語句, 建立表

db.execsql("createtable users (_id integer primary key autoincrement, name varchar(10), passwordvarchar(20))");

第三步:

new 物件, 然後 獲得 資料庫例項物件, 那麼就可以建立資料庫了 //

這裡這行**的執行

只是建立了

helper

的例項物件

,但是並不會

建立資料庫

檔案mydatabaseopenhelper helper =newmydatabaseopenhelper(this);

//這行**執行

,資料庫檔案才會建立

sqlitedatabasedb =helper.getreadabledatabase();

用db物件即可執行spl語句,運算元據庫中的資料 : db.execsql(sql語句)

oncreate 方法只有在首次建立資料庫的時候才會得到執行

一般在這裡主要去建立表。

onupgrade 方法只有在資料庫的版本公升級的時候才會得到執行

資料庫的版本只能夠公升級,不能夠降級, 資料庫的版本公升級的時候可以跳級 ,

一般在這裡主要用來修改已經有的表的結構. 或者 新新增其他的表.

如果需要新增一列

:db.execsql("altertable info add money varchar(10)");

主鍵: 這列的值,必須是唯一的, 並且不會為 空

建立表:

create table users (id integer primary keyautoincrement, name varchar(10), password varchar(20));

插入資料到表中

:insert into users values(null,'zs','123');

刪除表中的資料

:delete from users;        刪除所有的資料

delete from users where name='ww' andpwd='789';     刪除 資料時帶限定 條件,這個表示刪除name=』ww』 並且 pwd=』789』的記錄

修改表中的資料

:update users set name='ls',pwd='123' where name='lh';  修改資料,  表示 修改 name=』lhh』的name=』ls』 和pwd=』123』

update users set name='werx" where name='zl';   修改資料, 將 name=』zl』 的 name  改為name=』werx』                                          

查詢表中的資料

:select * from users;   查詢表中所有的資料

select id,name,pwd from users;   查詢 表中所有的 id, name, pwd資訊顯示出來

select id,name,pwd from users where name='werx';   查詢 name=』werx』的  id,name,pwd資訊             

Android下的資料儲存之sd卡

if environment.media mounted equals environment.getexternalstoragestate 獲得sd 卡的總容量的 大小,以及可用的 容量的大小 longtotalspace environment.getexternalstoragedirect...

Android之資料儲存

概述 1.android中包含5中資料儲存方式 sharedpreferences儲存資料。contentprovider儲存 檔案儲存 sqllite資料庫儲存 網路儲存 preference file database 這三種方式分別對應的目錄是 data data package name s...

Android下的資料儲存方式

安卓系統預設提供了一下幾種資料儲存的方式 shared preferences 內部儲存 外部儲存 sqlite資料庫 儲存到網路伺服器 使用shared preferences shared preferences類主要用於儲存鍵值對的資料型別。我們可以使用它儲存一些簡單的資料型別。獲得share...