码迷,mamicode.com
首页 > 数据库 > 详细

SQlite-数据库的访问实例(转)

时间:2015-03-02 20:39:41      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

1、DBAdapter类:  1 package com.cnzcom.android.quickdial;

 import android.content.ContentValues;
 import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * 
 * @author zhangjie
 *
 * 数据库相关操作的类
 */
public class DBAdapter {
    /**
     * 数据库名
     */
    private static final String DATABASE_NAME = "quickdial.db";
    
    /**
     * 数据表名
     */
    private static final String DATABASE_TABLE = "quickdial";
    
    /**
     * 数据库版本
     */
    private static final int DATABASE_VERSION = 1;
    
    /**
     * key_id :主键
     */
    @SuppressWarnings("unused")
    private static final String KEY_ID = "key_id";
    
    /**
     * position :位置信息,表示数据是第几项
     */
    private static final String POSITION = "position";
    
    /**
     * name :姓名
     */
    private static final String NAME = "name";
    
    /**
     * phone_number :电话号码
     */
    private static final String PHONE_NUMBER = "phone_number";
    
    /**
     * ip :是否ip播出
     */
    private static final String IP = "ip";
    
    /**
     * 
     */
    private static final String DATABASE_CREATE =
        "create table quickdial (key_id INTEGER PRIMARY KEY, "
            + "position INTEGER, " 
            + "name TEXT, " 
            + "phone_number TEXT, "
            + "ip INTEGER"
            + ");";
    
    /**
     * 
     */
    private final Context context;
    
    /**
     * 
     */
    private DatabaseHelper DBHelper;
    
    /**
     * 
     */
    private SQLiteDatabase db;
    
    public DBAdapter(Context ctx) {
        context = ctx;
        DBHelper = new DatabaseHelper(context);
        
        
    }
    
    private static class DatabaseHelper extends SQLiteOpenHelper {
        public DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
            onCreate(db);
        }
        
    }
    
    /**
     * 打开数据库
     * @return
     * @throws SQLException
     */
    public SQLiteDatabase open() throws SQLException
    {
        db = DBHelper.getWritableDatabase();

        Cursor cursor = getAll();
        
        if(cursor.getCount() == 0) {
            
            Log.e("数据库为空", "插入数据");
            /**
             * 先使用假数据初始化数据库
             */
            for(int i = 0; i < 10; i++) {
                insert(i, "", "", 0);
            }  
        } else {
            Log.e("数据库不为空", "读取数据");
        }
        cursor.close();
        
        return db;
    }
    
    /**
     * 关闭数据库
     */
    public void close()
    {
        DBHelper.close();
    }
    
    /**
     * 向数据库中插入数据
     */

    public long insert(int position, String name, String phone_number, int ip) {
        
        ContentValues initialValues = new ContentValues();
        initialValues.put(POSITION, position);
        initialValues.put(NAME, name);
        initialValues.put(PHONE_NUMBER, phone_number);
        initialValues.put(IP, ip);
        
        return db.insert(DATABASE_TABLE, null, initialValues);
    }
    
    /**
     * 删除数据,其实不是真正意义上的删除,而是将name = ""、phone_number = ""、ip = 0
     */
    public boolean delete(int position) {
        
        ContentValues initialValues = new ContentValues();
        initialValues.put(POSITION, position);
        initialValues.put(NAME, "");
        initialValues.put(PHONE_NUMBER, "");
        initialValues.put(IP, 0);
        
        return db.update(DATABASE_TABLE, initialValues, POSITION + "=" + position, null) > 0;    
        
//        return db.delete(DATABASE_TABLE, POSITION + "=" + position, null) > 0;
    }
    
    /**
     * 更改数据
     */
    public boolean update(int position, String name, String phone_number, int ip) {
        
        ContentValues initialValues = new ContentValues();
        //initialValues.put(POSITION, position);
        initialValues.put(NAME, name);
        initialValues.put(PHONE_NUMBER, phone_number);
        initialValues.put(IP, ip);
        
        return db.update(DATABASE_TABLE, initialValues, POSITION + "=" + position, null) > 0;        
    }
    
    public Cursor getAll() {
        
        Cursor cur = db.query(DATABASE_TABLE, 
                null, null, null, null, null, null);
        return cur;    
        
    }
    
    public Cursor get(long rowId) throws SQLException {
        Cursor cur = db.query(true, DATABASE_TABLE, new String[] {POSITION,
                                                                        NAME,
                                                                        PHONE_NUMBER,
                                                                        IP}, 
                                                                        
                                POSITION + "=" + rowId, null, null, null, null, null);
        
        if(cur != null) {
            cur.moveToFirst();
        }
        
        return cur;
    }
    
}

2、Activity中调用:1 public DBAdapter m_DBAdapter;

m_DBAdapter = new DBAdapter(this);
m_DBAdapter.open();

文章出处

http://www.cnblogs.com/Jackeyzhang/archive/2011/06/02/2068459.html

SQlite-数据库的访问实例(转)

标签:

原文地址:http://www.cnblogs.com/zhangshuli-1989/p/zhangshuli_SQlite_150302183.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!