SQLiteOpenHelper的使用

package com.zzxy.chenyb;

1 创建数据库辅助类,继承于SQLiteOpenHelper

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

public class DataBaseUtil extends SQLiteOpenHelper {
 private static final String DATABASENAME="llh.db";
 private static final int DATABASEVERSION=1;//版本号修改时,会执行onUpgrade方法
 private static final String USER="user";
 public DataBaseUtil(Context context){
  super(context, DATABASENAME, null, DATABASEVERSION);
 }
 public DataBaseUtil(Context context, String name, CursorFactory factory,
   int version) {
  super(context, DATABASENAME, factory, DATABASEVERSION);
 }

 @Override
 public void onCreate(SQLiteDatabase db) {
  String sql="create table "+USER+" (" +
    "ID INTEGER PRIMARY KEY" +
    ",NAME VARCHAR(50) NOT NULL" +
    ",PASSWORD VARCHAR(15) NOT NULL)";
  db.execSQL(sql);
  System.out.println("create user successfully...");
 }

 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  String sql="DROP TABLE IF EXISTS "+USER;
  db.execSQL(sql);
  this.onCreate(db);
  System.out.println("drop user successfully...");
 }

}

 

2 编写测试类,验证创建的数据库

SQLiteOpenHelper sqlite=new DataBaseUtil(LoginView.this);
      sqlite.getWritableDatabase();

验证执行完成程序后,到指定的文件夹内寻找生成的llh.db文件。

默认的目录在data/data/项目包名/databases/..

SQLiteOpenHelper的使用_第1张图片

你可能感兴趣的:(sqlite)