轉載請註明出處:http://blog.csdn.net/linglongxin24/article/details/53316096
本文出自【DylanAndroid的博客】
- 【玩轉SQLite系列】文章目錄
【玩轉SQLite系列】(四)通過Android提供的API操作SQLite數據庫
在上一篇【玩轉SQLite系列】(三)通過sql語句操作SQLite數據庫中介紹了用sql語句來操作SQLite數據庫,那麼,就需要用戶必須要由很熟練的sql語句拼寫和sql語法的熟練掌握。
其實,在Android裏面提供了相應的API來操作數據庫,即使對sql不是很熟練,也能夠很好的去操作SQLite數據庫。
這篇文章介紹一下通過Android提供的API對數據庫的以下操作:1.創建表
2.刪除表
3.新增數據
4.修改數據
5.刪除數據
6.查詢數據
0.創建或打開一個數據庫
- 在SD卡的sqlite文件夾下創建和打開一個info.db數據庫
File dataBaseFile = new File(Environment.getExternalStorageDirectory() + "/sqlite", Contacts.DATABASE_NAME);
if (!dataBaseFile.getParentFile().exists()) {
dataBaseFile.mkdirs();
}
sqLiteDatabase = SQLiteDatabase.openOrCreateDatabase(dataBaseFile, null);
- 通過adb shell命令即可看到數據庫文件。
cd storage/emulated/legacy/sqlite
1.創建表
/**
* 1.創建數據表user
* 表名 user
* *數據表user表結構字段
* 主鍵:id
* 名字:name
* 年齡:age:
*
* @param v
*/
public void create(View v) {
String sql = "CREATE TABLE " +
"IF NOT EXISTS " +
"user(" +
"id Integer PRIMARY KEY AUTOINCREMENT," +
"name varchar," +
"age Integer)";
sqLiteDatabase.execSQL(sql);
}
我們通過 (sqlite3 info2).命令打開數據庫,再執行(.table)命令查看數據表
2.刪除表格
/**
* 2.刪除數據表user
*
* @param v
*/
public void drop(View v) {
String sql = "DROP TABLE " +
"IF EXISTS " +
"user";
sqLiteDatabase.execSQL(sql);
}
3.插入(新增)數據
/**
* 3.給user表中新增一條數據
* <p>
* long insert(String table, String nullColumnHack, ContentValues values)
* 第一個參數:數據庫表名
* 第二個參數:當values參數爲空或者裏面沒有內容的時候,
* insert是會失敗的(底層數據庫不允許插入一個空行),
* 爲了防止這種情況,要在這裏指定一個列名,
* 到時候如果發現將要插入的行爲空行時,
* 就會將你指定的這個列名的值設爲null,然後再向數據庫中插入。
* 第三個參數:要插入的值
* 返回值:成功操作的行號,錯誤返回-1
*
* @param v
*/
public void insert(View v) {
String table = "user";
ContentValues contentValues = new ContentValues();
contentValues.put("name", "張三");
contentValues.put("age", 25);
long num = sqLiteDatabase.insert(table, null, contentValues);
if (num == -1) {
Toast.makeText(this, "插入失敗", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "成功插入到第" + num + "行", Toast.LENGTH_SHORT).show();
}
}
我們通過select * from user;命令去查看一下數據庫中內容
成功插入了兩條數據
4.修改數據
/**
* 4.修改user表中id爲2的名字改成“李四”
* <p>
* int update(String table, ContentValues values, String whereClause, String[] whereArgs)
* 第一個參數:表名
* 第二個參數:所要修改該的字段對應的值
* 第三個參數:修改的條件字段
* 第四個參數:修改的條件字段對應的值
* 返回值:影響的行數
*
* @param v
*/
public void update(View v) {
String table = "user";
ContentValues contentValues = new ContentValues();
contentValues.put("name", "李四");
String where = "id=2";
/**
* 方式一
*/
sqLiteDatabase.update(table, contentValues, where, null);
/**
* 方式二
*/
where = "id=?";
String[] whereArgs = new String[]{"2"};
int num = sqLiteDatabase.update(table, contentValues, where, whereArgs);
Toast.makeText(this, "修改了" + num + "行", Toast.LENGTH_SHORT).show();
}
我們來查看一下修改的結果
成功將id爲2的姓名修改成了李四
5.刪除數據
/**
* 5.刪除user表中id爲2的記錄
* <p>
* int delete(String table, String whereClause, String[] whereArgs)
* 第一個參數:刪除的表名
* 第二個參數:修改的條件的字段
* 第三個參數:修改的條件字段對應的值
* 返回值:影響的行數
*
* @param v
*/
public void delete(View v) {
String table = "user";
String where = "id=2";
sqLiteDatabase.delete(table, where, null);
where = "id=?";
String[] whereArgs = new String[]{"3"};
int num = sqLiteDatabase.delete(table, where, whereArgs);
Toast.makeText(this, "刪除了" + num + "行", Toast.LENGTH_SHORT).show();
}
我們來查看一下刪除的結果
成功將id爲2的數據刪除
6.查詢數據
/**
* 6.查詢數據
* <p>
* Cursor query(String table, String[] columns, String selection,String[] selectionArgs, String groupBy, String having,String orderBy)
* 第一個參數:表名
* 第二個參數:要查詢的字段名
* 第三個參數:要查詢的條件字段
* 第四個參數:要查詢的條件字段對應的值
* 第五個參數:分組的字段
* 第六個參數:篩選的字段
* 第七個參數:排序的字段
* 返回值:遊標
*
* @param v
*/
public void query(View v) {
String table = "user";
Cursor cursor = sqLiteDatabase.query(table, null, null, null, null, null, null);
if (cursor == null) {
return;
}
while (cursor.moveToNext()) {
Log.d(Contacts.TAG, "id=" + cursor.getInt(0) + ",name=" + cursor.getString(1) + ",age=" + cursor.getInt(2));
}
cursor.close();
}
我們通過Log來查看結果