Android操作Sqlite数据库

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"});
            }
        });
    }

}

参考:http://www.cnblogs.com/sjrhero/articles/2548307.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值