1. 程式人生 > >使用ContentValues對資料庫進行操作

使用ContentValues對資料庫進行操作




在main.xml中:

<?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"

    android:gravity="center_horizontal">

    <Button

        android:id="@+id/insertBut"

        android:layout_marginTop="8dp"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="增加資料" />

    <Button

        android:id="@+id/updateBut"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="修改資料" />

    <Button

        android:id="@+id/deleteBut"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="刪除資料" />

</LinearLayout>

建立資料庫的建表操作類Mytab.java

package com.li.sqlite;

import android.content.ContentValues;

import android.database.sqlite.SQLiteDatabase;

public class Mytab {

  private static final String TABLENAME = "mytab"; // 表示要操作的資料表名稱

  private SQLiteDatabase db = null; // 資料庫操作

  public Mytab(SQLiteDatabase db) { 

     this.db = db;

  }

  public void insert(String name,String birthday) {  //向表中增加資料

     ContentValues cv = new ContentValues();

     cv.put("name",name);

     cv.put("birthday",birthday);

     this.db.insert(TABLENAME, null, cv);

     this.db.close() ;

  }

  public void update(int id, String name, String birthday) {  //修改表的資料

     ContentValues cv = new ContentValues();

     cv.put("name",name);

     cv.put("birthday",birthday);

     String whereCaluse = "id=?";

     String whereArgs[] = new String[]{String.valueOf(id)};

     this.db.update(TABLENAME, cv, whereCaluse, whereArgs);

     this.db.close() ;

  }

  public void delete(int id) {     //刪除表的資料

     String whereCaluse = "id=?";

     String whereArgs[] = new String[]{String.valueOf(id)};

     this.db.delete(TABLENAME, whereCaluse, whereArgs);

     this.db.close() ;

  }

}

在MyDatabaseHelper.java類中:

package com.li.sqlite;

//資料庫的輔助操作類

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

public class MyDatabaseHelper extends SQLiteOpenHelper {

  private static final String DATABASENAME = "liyewen.db" ;

  private static final int DATABASERVERSION = 1 ;  // 設定資料庫的版本

  private static final String TABLENAME = "mytab" ;

  public MyDatabaseHelper(Context context) {  // 使用者最關心的也肯定只是Context

     super(context, DATABASENAME, null, DATABASERVERSION);

  }

  @Override

  public void onCreate(SQLiteDatabase db) { // 建立資料表

     String sql = "CREATE TABLE " + TABLENAME + "("

         + "id    INTEGER       PRIMARY KEY ,"   // 在SQLite中設定為Integer、PRIMARY KEY則ID自動增長

         + "name   VARCHAR(50)   NOT NULL ,"

         + "birthday DATE NOT    NULL" + ")";

     db.execSQL(sql) ;  // 執行SQL

     System.out.println("****************** 建立:onCreate()。");

  }

  @Override

  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

     String sql = "DROP TABLE IF EXISTS " + TABLENAME ;

     db.execSQL(sql) ;

     System.out.println("****************** 更新:onUpgrade()。");

     this.onCreate(db) ;

  }

}

在MySQLiteDemo.java中:

package com.li.sqlite;

import android.app.Activity;

import android.database.sqlite.SQLiteOpenHelper;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class MySQLiteDemo extends Activity {

  private Button inserBut = null;

  private Button updateBut = null;

  private Button deleteBut = null;

  private SQLiteOpenHelper helper = null;

  private Mytab mtab = null;

  private static int count = 0;

  @Override

  public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     super.setContentView(R.layout.main);

     this.inserBut = (Button)super.findViewById(R.id.insertBut);

     this.updateBut = (Button)super.findViewById(R.id.updateBut);

     this.deleteBut = (Button)super.findViewById(R.id.deleteBut);

     this.helper = new MyDatabaseHelper(this);

     this.inserBut.setOnClickListener(new InertOnClickListenerImpl());

     this.updateBut.setOnClickListener(new UpdateOnClickListenerImpl());

     this.deleteBut.setOnClickListener(new DeleteOnClickListenerImpl());

  }

  private class InertOnClickListenerImpl implements OnClickListener{

     public void onClick(View v) {

       MySQLiteDemo.this.mtab = new Mytab(

            MySQLiteDemo.this.helper.getWritableDatabase());

       MySQLiteDemo.this.mtab.insert("liyewen" + count++, "1988-08-16");

     }

  }

  private class UpdateOnClickListenerImpl implements OnClickListener{

     public void onClick(View v) {

       MySQLiteDemo.this.mtab = new Mytab(

            MySQLiteDemo.this.helper.getWritableDatabase());

       MySQLiteDemo.this.mtab.update(72, "update", "1988/8/15");

     }

  }

  private class DeleteOnClickListenerImpl implements OnClickListener{

     public void onClick(View v) {

       MySQLiteDemo.this.mtab = new Mytab(

            MySQLiteDemo.this.helper.getWritableDatabase());

       MySQLiteDemo.this.mtab.delete(73);

     }

  }

}