1、建立資料庫
android 中提供sqliteopenhelper類幫助建立乙個資料庫,sqliteopenhelper 是乙個抽象類,要建立乙個自己的幫助類去繼承它,子類至少要實現三個方法:帶參的構造方法; oncreate()用來建立資料庫;onupgrade(),用來公升級資料庫;資料庫檔案會存放在/data/data//databases/目錄下。
在activity中就可以通過mydatabasehelper建立資料庫public
class
mydatabasehelper
extends
sqliteopenhelper
@override
public
void
oncreate(sqlitedatabase db)
@override
public
void
onupgrade(sqlitedatabase arg0, int arg1, int arg2)
}
資料庫總要公升級的,此時建立一張teacher表,不能寫在oncreate()方法裡了,就要用到onupgrade()方法了,只要在mydatabasehelper的構造方法裡將引數修改為乙個比1大的數字,onupgrade()方法就會執行。mydatabasehelper dbhelper = new mydatabasehelper(this, "school.db",null, 1);
dbhelper.getwritabledatabase();
mydatabasehelper中需要修改的**:
activity中需要修改的**://新增一條建表語句
public
static
final string create_teacher = "create table teacher("
+ "id integer primary key autoincrement," + "name text,"
+ "age integer)";
//oncreate()
public
void
oncreate(sqlitedatabase db)
//onupgrade
public
void
onupgrade(sqlitedatabase db, int arg1, int arg2)
mydatabasehelper dbhelper = new mydatabasehelper(this, "school.db",null, 2);
2、android增、刪、改、查增加tom、jerry,年齡為20、21
修改tom年齡為25sqlitedatabase db = dbhelper.getwritabledatabase();
contentvalues values = new contentvalues();
// 開始組裝第一條資料,第乙個引數為列名
values.put("name", "tom");
values.put("age", 20);
// 引數依次為表名,將某些未賦值列自動賦值null,一般直接傳入null,contentvalues
db.insert("person", null, values);
values.clear();
// 開始組裝第二條資料
values.put("name", "jerry");
values.put("age", 21);
db.insert("person", null, values);
查詢person表sqlitedatabase db = dbhelper.getwritabledatabase();
contentvalues values = new contentvalues();
values.put("age", 25);
//引數依次為表名,contentvalues,第三第四個參數列條件,第四個引數為字串陣列
db.update("person", values, "name = ?", new
string);
刪除年齡》23的
3、sql運算元據庫增:sqlitedatabase db = dbhelper.getwritabledatabase();
db.delete("person", "age > ?", new
string );
改:sqlitedatabase db = dbhelper.getwritabledatabase();
db.execsql("insert into person (name,age) values (?,?)", new
string );
查:sqlitedatabase db = dbhelper.getwritabledatabase();
db.execsql("update person set age = ? where name = ?", new
string );
刪:sqlitedatabase db = dbhelper.getwritabledatabase();
db.rawquery("select * from person",null);
4、使用事務事務的特性可以保證讓某一系列的操作要麼全部完成,要麼乙個都不會完成。將person表中的資料全部刪除,替換成新資料。sqlitedatabase db = dbhelper.getwritabledatabase();
db.execsql("delete from person where age > ?",new
string);
5、公升級資料庫的最佳寫法正式工作中,公升級資料庫不可能將上個版本的資料庫全部刪掉,要保證在保留上個版本的資料基礎上公升級資料庫。向上面第二版中要建立新的資料庫,可以這麼寫:sqlitedatabase db = dbhelper.getwritabledatabase();
db.begintransaction();// 開啟事務
try catch (exception e) finally
如果使用者是在第一版的基礎上公升級,就只會執行db.execsql(create_teacher);如果使用者是直接安裝的第二版,就會執行oncreate()方法裡的public
void
onupgrade(sqlitedatabase db, int oldversion, int newversion)
}
這裡注意,每乙個case後面都是不加break的,當從第一版往上公升級時,每一版都會執行。public
void
oncreate(sqlitedatabase db)
litepal框架運算元據庫:
隨筆(二十七)
1.安卓開發外掛程式推薦 2.必知必會 android 測試相關的方方面面都在這兒 3android ui效能優化 檢測應用中的ui卡頓 4.recyclerview的拖動和滑動 第一部分 基本的itemtouchhelper示例 支援側滑和排序 5 android觸控事件分發機制詳解 6.andr...
題解二十七
給你兩個有序整數陣列 nums1 和 nums2,請你將 nums2 合併到 nums1 中,使 nums1 成為乙個有序陣列。說明 初始化 nums1 和 nums2 的元素數量分別為 m 和 n 你可以假設 nums1 有足夠的空間 空間大小大於或等於 m n 來儲存 nums2 中的元素。示例...
二十七 快速排序
快速排序其實是逐次對每個基數進行排序,當達到乙個臨界值 也就是當元素個數達到一定數量時,簡單的插入排序速度會大於快排 的時候就用插入排序來進行,其實這也是乙個分治處理的過程,和歸併思想大同小異,由於歸併要進行多次遞迴,而快排基於基數每次進行線性時間的分組,所以理想情況下快排優於歸併。這裡就直接上 i...