1.SQLite數(shù)據(jù)類型
一般數(shù)據(jù)采用的固定的靜態(tài)數(shù)據(jù)類型,而SQLite采用的是動態(tài)數(shù)據(jù)類型,會根據(jù)存入值自動判斷。SQLite具有以下五種常用的數(shù)據(jù)類型:
NULL: 這個值為空值
VARCHAR(n):長度不固定且其最大長度為 n 的字串,n不能超過 4000。
CHAR(n):長度固定為n的字串,n不能超過 254。
INTEGER: 值被標識為整數(shù),依據(jù)值的大小可以依次被存儲為1,2,3,4,5,6,7,8.
REAL: 所有值都是浮動的數(shù)值,被存儲為8字節(jié)的IEEE浮動標記序號.
TEXT: 值為文本字符串,使用數(shù)據(jù)庫編碼存儲(TUTF-8, UTF-16BE or UTF-16-LE).
BLOB: 值是BLOB數(shù)據(jù)塊,以輸入的數(shù)據(jù)格式進行存儲。如何輸入就如何存儲,不改??變格式。
DATA :包含了 年份、月份、日期。
TIME: 包含了 小時、分鐘、秒。
相信學過數(shù)據(jù)庫的童鞋對這些數(shù)據(jù)類型都不陌生的!!!!!!!!!!
2.SQLiteDatabase的介紹
Android提供了創(chuàng)建和是用SQLite數(shù)據(jù)庫的API。SQLiteDatabase代表一個數(shù)據(jù)庫對象,提供了操作數(shù)據(jù)庫的一些方法。在Android的SDK目錄下有sqlite3工具,我們可以利用它創(chuàng)建數(shù)據(jù)庫、創(chuàng)建表和執(zhí)行一些SQL語句。下面是SQLiteDatabase的常用方法。
SQLiteDatabase的常用方法
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?????方法名稱? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?????方法表示含義
openOrCreateDatabase(String path,SQLiteDatabase.CursorFactory??factory)???????????????打開或創(chuàng)建數(shù)據(jù)庫
insert(String table,String nullColumnHack,ContentValues??values)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 插入一條記錄
delete(String table,String whereClause,String[]??whereArgs)????????????????????????????????????????????刪除一條記錄
query(String table,String[] columns,String?
selection,String[]??selectionArgs,String groupBy,String having,String??orderBy)????????????????查詢一條記錄
update(String table,ContentValues values,String whereClause,String[]??whereArgs)????????????修改記錄
execSQL(String sql)????????????????????????????????????????????????????????????????????????????????????????????????????????????執(zhí)行一條SQL語句
close()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?關閉數(shù)據(jù)庫
3.SqLite基本操作語句
3.1、打開或者創(chuàng)建數(shù)據(jù)庫
方法一:
通過sql語句直接創(chuàng)建,創(chuàng)建前需要判斷是否已存在。
//數(shù)據(jù)庫對象
privateSQLiteDatabase? ? db;
db = this.openOrCreateDatabase("test.db",MODE_PRIVATE,null);
db.execSQL("create table if not exists staff(_id integer,name text,sex text,department text,salary float)");
方法二:
通過創(chuàng)建MySqLiteHelper類進行創(chuàng)建,它繼承自SQLiteOpenHelper類
創(chuàng)建表時不需要判斷
public class? MySqLiteHelper? extends? SQLiteOpenHelper {
//必須要有構造函數(shù)
public? ?MySqLiteHelper? (Context context, String name,
SQLiteDatabase.CursorFactory factory,intversion) {
????super(context, name, factory, version);
}
//當?shù)谝淮蝿?chuàng)建數(shù)據(jù)庫時調用
@Override
public void? onCreate(SQLiteDatabase db) {
//不需要添加判斷表是否存在的判斷語句
db.execSQL("create table staff(_id integer,name text,sex text,department text,salary float)");
//輸出創(chuàng)建數(shù)據(jù)庫的日志信息
Log.i("create_database", "create Database------------->");
}
//更新數(shù)據(jù)庫的時候執(zhí)行
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//輸出更新數(shù)據(jù)庫的日志信息
Log.i("update_database", "update Database------------->");
}
3.2、添加數(shù)據(jù)
插入數(shù)據(jù)有兩種方法:
1.編寫插入數(shù)據(jù)的SQL語句,直接調用SQLiteDatabase的execSQL()方法來執(zhí)行
2.SQLiteDatabase的insert(String table,String nullColumnHack,ContentValues??values)方法,
參數(shù)1??表名稱,
參數(shù)2??空列的默認值
參數(shù)3??ContentValues類型的一個封裝了列名稱和列值的Map;
方法一:
//插入數(shù)據(jù)SQL語句
String sql="insert into staff(_id,name,sex,department,salary)
values(10001,'tom','男','軟件研發(fā)',50000)";
//執(zhí)行SQL語句
db.execSQL(sql);
方法二:
int_id =10001;
String name ="tom";
String sex ="男";
String department ="軟件研發(fā)";
floatsalary =50000;
//存放列名和列值
ContentValues values =newContentValues();
values.put("_id",_id);
values.put("name",name);
values.put("sex",sex);
values.put("department",department);
values.put("salary",salary);
//執(zhí)行插入操作
db.insert("staff",null,values);
3.3、刪除數(shù)據(jù)
1.編寫刪除SQL語句,調用SQLiteDatabase的execSQL()方法來執(zhí)行刪除。
2.調用SQLiteDatabase的delete(String table,String whereClause,String[]??whereArgs)方法
參數(shù)1??表名稱
參數(shù)2??刪除條件
參數(shù)3??刪除條件值數(shù)組
方法一:
//刪除SQL語句
String sql = "delete from staff where _id = 10001";
//執(zhí)行SQL語句
db.execSQL(sql)
方法二:
//刪除條件
String whereClause ="_id=?";
//刪除條件參數(shù)數(shù)組
String [] whereArgs = {"10001"};
db.delete("staff",whereClause,whereArgs);
3.4、更新數(shù)據(jù)
1.編寫更新的SQL語句,調用SQLiteDatabase的execSQL執(zhí)行更新。
2.調用SQLiteDatabase的update(String table,ContentValues values,String??whereClause, String[]??whereArgs)方法
參數(shù)1??表名稱
參數(shù)2??跟行列ContentValues類型的鍵值對Key-Value
參數(shù)3??更新條件(where字句)
參數(shù)4??更新條件數(shù)組
方法一:
//修改SQL語句
String sql = "update staff set salary = 100000 where id = 10001";
//執(zhí)行SQL
db.execSQL(sql);
方法二:
//實例化內容
ContentValues values =newContentValues();
//在內容中添加內容
values.put("salary",100000);
//修改條件
String whereClause ="_id=?";
//添加修改條件參數(shù)
String [] whereArgs = {"10001"};
//修改
db.update("staff",values,whereClause,whereArgs);
3.5、數(shù)據(jù)查詢
public??Cursor query(String table,String[] columns,String selection,String[]??selectionArgs,String groupBy,String having,String orderBy,String limit);
各個參數(shù)的意義說明:
參數(shù)table:表名稱
參數(shù)columns:列名稱數(shù)組
參數(shù)selection:條件字句,相當于where
參數(shù)selectionArgs:條件字句,參數(shù)數(shù)組
參數(shù)groupBy:分組列
參數(shù)having:分組條件
參數(shù)orderBy:排序列
參數(shù)limit:分頁查詢限制
參數(shù)Cursor:返回值,相當于結果集ResultSet
Cursor是一個游標接口,提供了遍歷查詢結果的方法,如移動指針方法move(),獲得列值方法getString()等.
Cursor游標常用方法:
????????????????????????方法名稱? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 方法描述
????????????????getCount()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 獲得總的數(shù)據(jù)項數(shù)
????????????????isFirst()????????????????????????????????????????????????????????????????????????????????判斷是否第一條記錄
????????????????isLast()????????????????????????????????????????????????????????????????????????????????判斷是否最后一條記錄
????????????????moveToFirst()????????????????????????????????????????????????????????????????????????移動到第一條記錄
????????????????moveToLast()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?移動到最后一條記錄
????????????????move(int offset)????????????????????????????????????????????????????????????????????????移動到指定記錄
????????????????moveToNext()????????????????????????????????????????????????????????????????????????移動到下一條記錄
????????????????moveToPrevious()????????????????????????????????????????????????????????????????????移動到上一條記錄
????????????????getColumnIndexOrThrow(String??columnName)? ? ? ? ? ? ? ? ? 根據(jù)列名稱獲得列索引
????????????????getInt(int columnIndex)????????????????????????????????????????????????????????獲得指定列索引的int類型值
????????????????getString(int columnIndex)????????????????????????????????????????????????獲得指定列縮影的String類型值
public void queryData(View view){
String [] name = {"tom"};
StringBuffer stringBuffer =newStringBuffer();
//相當于ResultSet
Cursor cursor =db.rawQuery("select * from staff where name=?",name);
while(cursor.moveToNext()){
stringBuffer.append("ID:"+ cursor.getInt(0) +"\n\r"+"name:"+ cursor.getString(1)
+"\n\r"+"sex:"+ cursor.getString(2) +"\n\r"+"所在部門:"+ cursor.getString(3)
+"\n\r"+"工資:"+ cursor.getFloat(4) +"\n\r");
}
showResult.setText(stringBuffer.toString());
Toast.makeText(this,"查詢數(shù)據(jù)成功",Toast.LENGTH_SHORT).show();
}