學習Android有幾天了,今天研究了下SQLite的簡單操作,現在分享給奮鬥在一線的苦逼程序員們,共勉吧。
Android系統提供了一個SQLiteOpenHelper的一個輔助類,使用此類可以完成對數據庫的創建及更新,寫了一個簡單的類,代碼如下:
package com.van.sqlite.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBOpenHelper extends SQLiteOpenHelper{
/**數據庫名稱*/
private static final String DATABASE_NAME="MyAppDB";
/**數據庫版本*/
private static final int DATABASE_VERSION=1;
/** 創建數據表語句*/
private static final String DDL_CREATE_TABLE_APPINFO="CREATE TABLE IF NOT EXISTS AppInfo (appId integer primary key autoincrement,appName text,appDescription text, remark text)";
/**
* 實例化數據庫連接.
* @param context
*/
public DBOpenHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
/**初始化數據表*/
this.getWritableDatabase().execSQL(DDL_CREATE_TABLE_APPINFO);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
佈局文件: main.xml ,只添加了一個ListView用於顯示數據
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView_appList"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
數據列表的項實現:list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView_appName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView_appDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
SQLiteDemoActivity 代碼:
package com.van.sqlite;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import com.van.sqlite.db.DBOpenHelper;
public class SQLiteDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//initData(); //這個方法只是用來臨時加載一下數據以備顯示
ListView listView=(ListView)findViewById(R.id.listView_appList);
//獲取查詢結果
ArrayList<HashMap<String, Object>> listData=fillList();
//獲取適配器
SimpleAdapter adapter=fillAdapter(listData);
//添加並且顯示
listView.setAdapter(adapter);
}
/**
* 插入數據操作
*/
public void initData(){
DBOpenHelper helper=new DBOpenHelper(this);
ContentValues values=new ContentValues();
values.put("appName", "手機QQ2212");
values.put("appDescription", "手機QQ2012是騰訊公司基於移動終端開發的一款應用軟件。");
values.put("remark", "手機QQ2012是騰訊公司基於移動終端開發的一款應用軟件。");
helper.getWritableDatabase().insert("AppInfo", null, values);
}
/**
* 查詢AppInfo返回Map集合
* @return
*/
public ArrayList<HashMap<String, Object>> fillList(){
//生成動態數組,並且轉載數據
ArrayList<HashMap<String, Object>> dataList = new ArrayList<HashMap<String, Object>>();
DBOpenHelper helper=new DBOpenHelper(this);
SQLiteDatabase db=helper.getReadableDatabase();
try{
Cursor cursor=db.rawQuery("SELECT * FROM AppInfo", null);
cursor.moveToFirst();
if(cursor.moveToFirst()) {
Integer appId = cursor.getInt(cursor.getColumnIndex("appId"));
String appName = cursor.getString(cursor.getColumnIndex("appName"));
String appDescription = cursor.getString(cursor.getColumnIndex("appDescription"));
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("appId",appId);
map.put("appName", appName);
map.put("appDescription", appDescription);
dataList.add(map);
}
}catch(Exception ex){
ex.printStackTrace();
}finally{
if(db.isOpen()){
db.close();
}
}
return dataList;
}
/**
* 填充數據,取得數據適配器.
* @param listData
* @return
*/
public SimpleAdapter fillAdapter(ArrayList<HashMap<String, Object>> listData){
//生成適配器,數組===》ListItem
SimpleAdapter adapter = new SimpleAdapter(this,
listData,//數據來源
R.layout.list_item,//ListItem的XML實現
//動態數組與ListItem對應的子項
new String[] {"appName", "appDescription"},
//ListItem的XML文件裏面的兩個TextView ID
new int[] {R.id.textView_appName,R.id.textView_appDescription});
return adapter;
}
}
運行結果如下:
把工程目錄結構也貼上: