纸上得来终觉浅,绝知此事要躬行。

 

Android操作Sqlite数据库

首先要继承抽象类:SQLiteOpenHelper

可以这样写:

package sRoger.pack;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * sqlite 辅助类
 * @author sRoger
 *
 */
public class SqliteHelper extends SQLiteOpenHelper {

    /**
     * 版本号
     */
    private static final int VERSION = 1;
    
    private final String tag="SqliteHelper";
    
    /**
     * 构造函数
     * @param context
     * @param name
     * @param factory
     * @param version
     */
    public SqliteHelper(Context context, String name, CursorFactory factory, int version) {
        // 调用父类构造函数
        super(context, name, factory, version);
    }
    
    public SqliteHelper(Context context,String name,int version){
        this(context,name,null,version);
    }
    
    public SqliteHelper(Context context,String name){
        this(context,name,VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d(tag, "创建了一个Sqlite数据库");
        db.execSQL("create table user" +
                    "(" +
                    "    id int," +
                    "    name varchar(20)" +
                    ")"
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.d(tag, "Upgrade execute.");
    }

}

然后添加几个按钮,分别为:增、删、改、查

代码如下:

package sRoger.pack;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class LayoutSample14 extends Activity {
    
    private String tag="Sqlite3";
    private Button btnCreate = null;
    private Button btnUgrageDatabase = null;
    private Button btnInsert = null;
    private Button btnUpdateSql = null;
    private Button btnQuerySql = null;
    private Button btnDeleteSql=null;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
       // 创建SQLite3数据库
       btnCreate = (Button) findViewById(R.id.Create);
       btnCreate.setOnClickListener(new OnClickListener() {
        
            public void onClick(View v) {
                //这个构造函数中的两个参数,第一个代表当前实例上下文,第二是Sqlite3 数据库名称
                SqliteHelper helper=new SqliteHelper(getApplicationContext(), "sRoger_Db");
                SQLiteDatabase dbase = helper.getReadableDatabase();
                Toast.makeText(getApplicationContext(), "createDatabase", Toast.LENGTH_SHORT).show();
                Log.d(tag, "Create Database");
            }
       });
       
       // 更新版本
       btnUgrageDatabase = (Button)findViewById(R.id.Upgrade);
       btnUgrageDatabase.setOnClickListener(new OnClickListener() {
            
               public void onClick(View v) {
                SqliteHelper helper=new SqliteHelper(getApplicationContext(), "sRoger_Db", 2);
                SQLiteDatabase dbase=helper.getReadableDatabase();
                Log.d(tag,"Upgrade Database");
            }
            
        });
       
       // 插入记录
       btnInsert = (Button)findViewById(R.id.Insert);
       btnInsert.setOnClickListener(new OnClickListener() {
        
            public void onClick(View v) {
                ContentValues cv=new ContentValues();
                cv.put("Id", 1);
                cv.put("name", "sRoger");
                SqliteHelper helper=new SqliteHelper(getApplicationContext(), "sRoger_Db", 3);
                SQLiteDatabase dbase=helper.getWritableDatabase();
                //调用Insert方法,向SQLite3中插入记录
                dbase.insert("user", null, cv);
                Log.d(tag,"insert data to Database");
            }
            
        });
       
       // 更新记录
       btnUpdateSql=(Button)findViewById(R.id.updateSql);
       btnUpdateSql.setOnClickListener(new OnClickListener() {
        
            public void onClick(View v) {
                SqliteHelper helper=new SqliteHelper(getApplicationContext(), "sRoger_Db",4);
                SQLiteDatabase db=helper.getWritableDatabase();
                ContentValues cv=new ContentValues();
                cv.put("name", "baihongying");
                /*
                 *    第一个参数是:表名
                 *    第二个参数ContentValues对象
                 *    第三个参数where子句,里面的问题是一个占位符
                 *    第四个参数是占位符对应的值 
                 *    注意:有几个占位符第四个参数里面就应该有几个值
                 */
                db.update("user", cv, "id=?", new String[]{"1"});
                Log.d(tag, "update data to Database");
            }
            
        });
       
       //查询记录
       btnQuerySql = (Button)findViewById(R.id.querySql);
       btnQuerySql.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                SqliteHelper helper=new SqliteHelper(getApplicationContext(), "sRoger_Db", 5);
                SQLiteDatabase db=helper.getReadableDatabase();
                Cursor cur = db.query("user", new String[]{"id","name"}, "id=?", new String[]{"1"}, "id", null, null);
                while(cur.moveToNext()){
                    String name=cur.getString(cur.getColumnIndex("name"));
                    Log.d(tag, name);
                }
            }
        });
       
       //删除操作
       btnDeleteSql = (Button)findViewById(R.id.deleteSql);
       btnDeleteSql.setOnClickListener(new OnClickListener() {
        
            public void onClick(View v) {
                SqliteHelper helper = new SqliteHelper(getApplicationContext(), "sRoger_Db", 6);
                SQLiteDatabase db=helper.getWritableDatabase();
                db.delete("user", "id=?", new String[]{"1"});
            }
        });
    }
}

posted on 2012-06-13 18:14  JRoger  阅读(4384)  评论(0编辑  收藏  举报

导航