SQLiteOpenHelper的使用

前端之家收集整理的这篇文章主要介绍了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,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/..

原文链接:https://www.f2er.com/sqlite/201390.html

猜你在找的Sqlite相关文章