Tuesday, February 23, 2010

在Android里使用数据库 - 非常方便

package com.flybull.aabb;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.widget.TextView;

public class AaBbActivity extends Activity {
    /** Called when the activity is first created. 中 */
   
    private static final String DATABASE_NAME = "con_tact.db";
    private static final int DATABASE_VERSION = 2;
    private static final String CONT_TABLE_NAME = "cont";
    private static final String COL_NAME = "name";
    private static final String COL_TEL = "tel";
   

   
    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + CONT_TABLE_NAME + " ("
                    + BaseColumns._ID + " INTEGER PRIMARY KEY,"
                    + COL_NAME + " TEXT,"
                    + COL_TEL + " TEXT"
                    + ");");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
//                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS notes");
            onCreate(db);
        }
    }
   
    private DatabaseHelper mOpenHelper;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        TextView tv = new TextView(this); // 定义一个TextView用来显示到屏幕上
        setContentView(tv);
       
        mOpenHelper = new DatabaseHelper(this);       
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
       
        // 在数据库中插入两条记录
        ContentValues values = new ContentValues();
        values.put(COL_NAME, "zhangshan");
        values.put(COL_TEL, "13512341234");
        long result = db.insert(CONT_TABLE_NAME, COL_NAME, values);
       
        values.clear();       
        values.put(COL_NAME, "lisi");
        values.put(COL_TEL, "13512341238");
        result = db.insert(CONT_TABLE_NAME, COL_NAME, values);
       


        // query
        // 把所有记录查上来,显示到屏幕中
        String[] columns = new String[]{
                COL_NAME,
                COL_TEL,};
        Cursor c = db.query(CONT_TABLE_NAME, columns, "name = 'zhangshan'", null, null, null, null);
        c.moveToFirst();
        while(!c.isAfterLast())
        {
            tv.append(c.getString(0));
            tv.append(" - ");
            tv.append(c.getString(1));
            tv.append("\n");
            c.moveToNext();
        }

        // update
        values.clear();
        values.put(COL_NAME, "lisi");
        values.put(COL_TEL, "13599999999");
        db.update(CONT_TABLE_NAME, values, "name = 'zhangshan'", null);
       
        c = db.query(CONT_TABLE_NAME, columns, null, null, null, null, null);
        c.moveToFirst();
        while(!c.isAfterLast())
        {
            tv.append(c.getString(0));
            tv.append(" - ");
            tv.append(c.getString(1));
            tv.append("\n");
            c.moveToNext();
        }

        // delete
        db.delete(CONT_TABLE_NAME, "name = 'lisi'", null);
       

    }
   

}



No comments: