1.Sqlite簡介
Sqlite是一款輕型的數據庫,它包含在一個相對小的C庫中,它的設計目標是嵌入式的,由于它占用資源非常少,可能只需要幾百K的內存就可以了,并且支持Windows/Linux/Unix等等主流的操作系統,同時可以和很多種程序語言相結合,比如:C#/Java/php等,所以在嵌入式設備中特別受歡迎,這一點也正好符合android的開發要求,所以在Android開發中經常要用到該數據庫。
2. 運用Sqlite,一般要有一個SQLiteOpenHelper來輔助創建數據庫,連接數據庫。
eg:
注意:
(1)DatabaseHelper 繼承SQLiteOpenHelper。必須實現3點,
構造函數:用于初始化數據庫,創建數據庫
public void onCreate(SQLiteDatabase db) :用于創建表。在調getReadableDatabase或getWritableDatabase時,會判斷指定的數據庫是否存在,不存在則調 SQLiteDatabase.create創建, onCreate只在數據庫第一次創建時才執行
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion):用于數據庫更新操作
3.//單例模式 Dao 類,做一個對數據表進行操作的類
eg:
package cdv.cq.mobilestation.Activity.praise;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;
//單例模式
public class Dao {
private static Dao dao = null;
private Context context;
private Dao(Context contex) {
this.context = contex;
}
public static Dao getInstance(Context context) {
if (dao == null) {
dao = new Dao(context);
}
return dao;
}
// 連接數據庫
public SQLiteDatabase getConnection() {
SQLiteDatabase sqLiteDatabase = null;
try {
sqLiteDatabase = new DatabaseHelper(context, DatabaseHelper.DBNAME)
.getReadableDatabase();
} catch (Exception e) {
e.printStackTrace();
}
return sqLiteDatabase;
}
public void createTable(SQLiteDatabase sqLiteDatabase){
sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS priaseTable(id integer primary key autoincrement,clickId varchar(100),clickType varchar(100),clickCount INTEGER)");
}
public synchronized int isHasInfors(String clickId, String clickType) {
SQLiteDatabase database = getConnection();
int id = -1;
Cursor cursor = null;
try {
cursor = database.query("priaseTable", null,
"clickId=? AND clickType=?", new String[] { clickId,
clickType }, null, null, null);
if (cursor.moveToFirst()) {
id = cursor.getInt(0);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != database) {
database.close();
}
if (null != cursor) {
cursor.close();
}
}
return id;
}
//增
public synchronized void insert(String clickId, String clickType,
int count) {
SQLiteDatabase database = getConnection();
// int id = dao.isHasInfors(clickId,clickType);
// if(id == 0){
// ContentValues cv = new ContentValues();
// cv.put("clickId", clickId);
// cv.put("clickType", clickType);
// cv.put("clickCount", 1);
// database.insert("priaseTable", null, cv);
// String sql =
// "insert into priaseTable(clickId,clickType,clickCount) values(?,?,?)";
// Object[] info ={clickId,clickType,"1"};
// database.execSQL(sql,info);
// }
// else{
// //更改數據操作
//
// }
try {
String sql = "insert into priaseTable(clickId,clickType,clickCount) values(?,?,?)";
Object[] info = { clickId, clickType, count};
database.execSQL(sql, info);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != database) {
database.close();
}
}
}
// 改
public synchronized void update(int Id, boolean isAdd) {
SQLiteDatabase database = getConnection();
int count = 0;
try{
Cursor curor = database.rawQuery("select * from priaseTable where id =?",new String[]{String.valueOf(Id)});
if(curor.moveToFirst())
count = curor.getInt(3);
if(isAdd){
String sql = "update priaseTable set clickCount=? where id = ?";
Object[] info = {++count,Id};
database.execSQL(sql,info);
//這是在新的線程打開的,Toast只能在主線程修改UI界面
//Toast.makeText(context, "點贊加1成功",2*1000).show();
}
else{
String sql = "update priaseTable set clickCount=? where id = ?";
Object[] info = {--count,Id};
database.execSQL(sql,info);
//Toast.makeText(context, "點贊減1成功",2*1000).show();
}
}catch(Exception e){
e.printStackTrace();
}finally{
if (null != database) {
database.close();
}
}
}
//查 點贊數
public synchronized int queryPraise(String id){
SQLiteDatabase database = getConnection();
int count = 0;
try{
Cursor curor = database.rawQuery("select from priaseTable where id =?", new String[]{id});
while(curor.moveToFirst())
count = curor.getInt(4);
}catch(Exception e){
e.printStackTrace();
}finally{
if (null != database) {
database.close();
}
}
return count;
}
}
注意:
(1)sqlite 數據庫中的表 列號是從0開始的,
eg:Cursor curor = database.rawQuery("select * from priaseTable where id =?",new String[]{String.valueOf(Id)});
while(curor.moveToFirst())
count = curor.getInt(3); //一共4列
(2)做成一個DAO類單例模式可以很好的對數據庫中的表進行操作
(3)增刪改查功能跟SQLserver語句一樣的但是要注意:不必記太多的android自帶的方法
增刪改::database.execSQL(sql, info); 直接對數據庫進行操作
查詢用: database.query(sql, info)用query()方法可以直接返回一個游標進行相關操作