android 数据库

来源:互联网 发布:易语言ce助手源码 编辑:程序博客网 时间:2024/05/30 05:20
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class StudentDBHelper extends SQLiteOpenHelper {

    private static SQLiteDatabase db = null;
    private static final String TAG = "StudentDB";
    private static final String DATABASE_NAME = "studentss.db";
    private static final int Version = 1;
    public static final String TABLE_NAME = "STUDENT_TABLE";
    private static final String base_info = "create table "
            + TABLE_NAME
            + "(name text not null,sex text,age text,no text,phone text,address text);";

    private StudentDBHelper(Context context) {
        super(context, DATABASE_NAME, null,
                Version);
        Log.i(TAG, "database is create");        
    }

    public static SQLiteDatabase getSQLiteDatabse(Context context) {
        db = new StudentDBHelper(context).getWritableDatabase();        
        return db;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(base_info);
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i(TAG, "onUpgrade database");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
    
}
原创粉丝点击