【原创】SQLiteOpenHelper 示例

本文介绍了一个用于管理短信数据的SQLite数据库助手类DbHelper。该工具为Android应用提供了一个简便的方法来创建和升级短信数据库,包括sms表及其字段如_id、address、person等。
摘要由CSDN通过智能技术生成

package com.firewings.smstools;  
  
import android.content.Context;  
import android.database.sqlite.SQLiteDatabase;  
import android.database.sqlite.SQLiteOpenHelper;  
  
public class DbHelper extends SQLiteOpenHelper {  
      
    private static DbHelper sSingleton = null;  
  
    public static final String DATABASE_NAME = "sms.db";  
    public static final int DATABASE_VERSION = 1;  
  
    public interface Tables {  
        public static final String SMS = "sms";  
    }  
  
    public interface SmsColumns {  
        public static final String _ID = "_id";  
        public static final String ADDRESS = "address";  
        public static final String PERSON = "person";  
        public static final String DATE = "date";  
        public static final String TYPE = "type";  
        public static final String BODY = "body";  
        public static final String SEND = "send";  
    }  
      
    public static synchronized DbHelper getInstance(Context context) {  
        if (sSingleton == null) {  
            sSingleton = new DbHelper(context);  
        }  
        return sSingleton;  
    }  
  
    public DbHelper(Context context) {  
        super(context, DATABASE_NAME, null, DATABASE_VERSION);  
    }  
  
    @Override  
    public void onCreate(SQLiteDatabase db) {  
        db.execSQL("CREATE TABLE " + Tables.SMS + " (" +  
                SmsColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +  
                SmsColumns.ADDRESS + " TEXT NOT NULL," +  
                SmsColumns.PERSON + " TEXT," +  
                SmsColumns.DATE + " TEXT NOT NULL," +  
                SmsColumns.TYPE + " TEXT NOT NULL," +  
                SmsColumns.BODY + " TEXT NOT NULL," +  
                SmsColumns.SEND + " INTEGER NOT NULL DEFAULT 0" +  
        ");");  
    }  
  
    @Override  
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
    }  
}  
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值