存取SQLite資料庫的說明如下:
資料新增的說明如下:
1.呼叫getWritableDatabase()建立、開啟資料庫並回傳資料庫物件
SQLiteDatabase db = getWritableDatabase(); 2.建立ContentValues物件儲存欲新增的資料 ContentValues values = new ContentValues() 3.呼叫ContentValues的put()新增資料到指定欄位內 values.put(COL_id, site.getId()); 4.呼叫insert()新增資料,新增成功會回傳該資料列的ID;新增失敗則回傳-1 long rowId = db.insert(TABLE_NAME, null, values);
1.設計Ch8_3_InsertData.java
- package com.example.ch8_3_insertdata;
- import java.util.ArrayList;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- public class Ch8_3_Insert extends Activity {
- EditText et1,et2,et3,et4;
- Button btn1,btn2,btn3,btn4;
- TextView tv2;
- DBHP dbHlp;
- ArrayList sites;
- int index=0;
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- connectDB();
- findViews();
- showSites(index);
- }
- private void connectDB() {
- if(dbHlp == null)
- dbHlp = new DBHP(this);
- dbHlp.fillDB();
- sites=dbHlp.getAllSites();
- }
- void findViews(){
- et1=(EditText)findViewById(R.id.editText1);
- et2=(EditText)findViewById(R.id.editText2);
- et3=(EditText)findViewById(R.id.editText3);
- et4=(EditText)findViewById(R.id.editText4);
- btn1=(Button)findViewById(R.id.button1);
- btn2=(Button)findViewById(R.id.button2);
- btn3=(Button)findViewById(R.id.button3);
- btn4=(Button)findViewById(R.id.button4);
- tv2=(TextView)findViewById(R.id.textView2);
- btn1.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- index=index+1;
- if(index>=sites.size())
- index=0;
- showSites(index);
- }} );
- btn2.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- index=index-1;
- if(index<0)
- index=sites.size()-1;
- showSites(index);
- }} );
- btn3.setOnClickListener(new OnClickListener(){
- public void onClick(View v) {
- String id=et1.getText().toString().trim();
- String name=et2.getText().toString().trim();
- String phoneNo=et3.getText().toString().trim();
- String address=et4.getText().toString().trim();
- if(id.length()<=0 || name.length()<=0)
- Toast.makeText(Ch8_2_BrowseData.this, "未輸入代號或名稱!", Toast.LENGTH_LONG).show();
- StringBuilder sb=new StringBuilder();
- Site site=new Site(id,name,phoneNo,address);
- long rowId=dbHlp.insertDB(site);
- if(rowId !=-1)
- sb.append("新增資料成功!");
- else
- sb.append("新增資料失敗!");
- Toast.makeText(Ch8_2_BrowseData.this, sb, Toast.LENGTH_LONG).show();
- sites=dbHlp.getAllSites();//再呼叫一次getAllSites()取得資料庫中最新的內容
- }} );
- btn4.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- et1.setText("");
- et2.setText("");
- et3.setText("");
- et4.setText("");
- }} );
- }
- void showSites(int index){
- if(sites.size()>0){
- tv2.setText((index+1)+"/"+sites.size()+getString(R.string.count));
- et1.setText(sites.get(index).getId());
- et2.setText(sites.get(index).getName());
- et3.setText(sites.get(index).getPhoneNo());
- et4.setText(sites.get(index).getAddress());
- }else{
- tv2.setText("0/0"+getString(R.string.count));
- et1.setText("");
- et2.setText("");
- et3.setText("");
- et4.setText("");
- }
- }
- }
復制代碼
2.設計DBHP.java
- package com.example.ch8_3_insertdata;
- import java.util.ArrayList;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteDatabase.CursorFactory;
- import android.database.sqlite.SQLiteOpenHelper;
- public class DBHP extends SQLiteOpenHelper {
- private static final String DATABASE_NAME = "sites";
- private static final int DATABASE_VERSION = 1;
- private static final String TABLE_NAME = "sitesInfo";
- private static final String TABLE_CREATE =
- "CREATE TABLE " + TABLE_NAME + " (id TEXT NOT NULL,name TEXT NOT NULL,phoneNo TEXT, address TEXT, PRIMARY KEY (id)); ";
- public DBHP(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(TABLE_CREATE);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
- onCreate(db);
- }
- public void fillDB() {
- SQLiteDatabase db = getWritableDatabase();
- ContentValues[] values = new ContentValues[3];
- for(int i=0; i
- values[i] = new ContentValues();
- values[0].put("id", "yangmingshan");
- values[0].put("name", "陽明山國家公園管理處");
- values[0].put("phoneNo", "02-28613601");
- values[0].put("address", "台北市北投區竹子湖路1之20號");
- values[1].put("id", "yushan");
- values[1].put("name", "玉山國家公園管理處");
- values[1].put("phoneNo", "049-2773121");
- values[1].put("address", "南投縣水里鄉中山路一段300號");
- values[2].put("id", "taroko");
- values[2].put("name", "太魯閣國家公園管理處");
- values[2].put("phoneNo", "03-8621100");
- values[2].put("address", "花蓮縣秀林鄉258號");
- for(ContentValues row : values){
- db.insert(TABLE_NAME, null, row);
- }
- db.close();
- }
- public ArrayList getAllSites()
- {
- SQLiteDatabase db = getReadableDatabase();
- String columns[]={"id","name","phoneNo","address"};
- Cursor cursor =db.query(TABLE_NAME, columns, null, null, null, null, null);
- ArrayList sites = new ArrayList();
- while(cursor.moveToNext()){
- String id=cursor.getString(0);
- String name=cursor.getString(1);
- String phoneNo=cursor.getString(2);
- String address=cursor.getString(3);
- Site site=new Site(id,name,phoneNo,address);
- sites.add(site);
- }
- cursor.close();
- db.close();
- return sites;
- }
- public long insertDB(Site site)
- {
- SQLiteDatabase db = getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put("id", site.getId());
- values.put("name", site.getName());
- values.put("phoneNo", site.getPhoneNo());
- values.put("address", site.getAddress());
- long rowId=db.insert(TABLE_NAME, null, values);
- return rowId;
- }
- }
復制代碼
3.設計Site.java
- package com.example.ch8_3_insertdata;
- public class Site {
- String id,name,phoneNo,address;
- Site(String id,String name,String phoneNo,String address)
- {
- this.id=id;
- this.name=name;
- this.phoneNo=phoneNo;
- this.address=address;
- }
- String getId(){
- return id;
- }
- String getName(){
- return name;
- }
- String getPhoneNo(){
- return phoneNo;
- }
- String getAddress(){
- return address;
- }
- }
復制代碼 加入更新及刪除功能 Ch8_3_InsertData.java
- btn5.setOnClickListener(new OnClickListener(){
- public void onClick(View v) {
- String id=et1.getText().toString().trim();
- String name=et2.getText().toString().trim();
- String phoneNo=et3.getText().toString().trim();
- String address=et4.getText().toString().trim();
- if(id.length()<=0 || name.length()<=0){
- Toast.makeText(getApplicationContext(), "未輸入ID或NAME", Toast.LENGTH_SHORT).show();
- }else{
- Site site=new Site(id,name,phoneNo,address);
- int rowId=dbHlp.updateDB(site);
- if(rowId!=-1){
- Toast.makeText(getApplicationContext(), "更新成功", Toast.LENGTH_SHORT).show();
- sites=dbHlp.getAllSites();
- showSites(index);
- }else
- Toast.makeText(getApplicationContext(), "更新失敗", Toast.LENGTH_SHORT).show();
- }
- }
- });
- btn6.setOnClickListener(new OnClickListener(){
- public void onClick(View v) {
- String id=et1.getText().toString().trim();
- int count=dbHlp.deleteDB(id);
- if(count!=-1){
- Toast.makeText(getApplicationContext(), "刪除成功", Toast.LENGTH_SHORT).show();
- }else
- Toast.makeText(getApplicationContext(), "刪除失敗", Toast.LENGTH_SHORT).show();
- sites=dbHlp.getAllSites();
- index=0;
- showSites(index);
- }
- });
復制代碼 DBHP.java
- public int updateDB(Site site){
- SQLiteDatabase db=getWritableDatabase();
- ContentValues values=new ContentValues();
- values.put("id", site.getId());
- values.put("name", site.getName());
- values.put("phoneNo", site.getPhoneNo());
- values.put("address", site.getAddress());
- String whereClause="id='"+site.getId()+"'";
- int rowId=db.update(TABLE_NAME, values, whereClause, null);
- return rowId;
- }
- public int deleteDB(String id){
- SQLiteDatabase db=getWritableDatabase();
-
- String whereClause="id='"+id+"'";
- int rowId=db.delete(TABLE_NAME, whereClause, null);
- return rowId;
- }
復制代碼
|