package com.example.meappthree.Dao;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
import com.example.meappthree.Bean.User;
import javax.security.auth.login.LoginException;
public class UserDao {
private SQLiteDatabase db;
public UserDao(Context context) {
//实例化辅助类
//参数一、上下文环境
//参数二、数据库路径
//参数三、游标工厂 , null
//参数四、版本号,能够决定是否会调用升级方法
SQLiteOpenHelper helper = new SQLiteOpenHelper(context, context.getFilesDir().getAbsolutePath() + "MeAppThree2.db", null, 1) {
//创建数据库、创建表
@Override
public void onCreate(SQLiteDatabase db) {
Log.e("TAG", "onCreate: 创建成功");
String sql = "create table UserTb(" +
"_id integer primary key autoincrement," +
"userName varchar(50) not null," +
"steps varchar(20)," +
"userPassword varchar(50) not null," +
"phone varchar(20) not null," +
"idNo varchar(50) not null)";
db.execSQL(sql);
}
//升级
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
};
//获取数据库对象
//1/如果数据库不存在 则先创建再打开
//2、如果数据库存在,但是版本号没变化,则直接打开
//3、如果数据库存在,版本号发生变化,则先执行升级方法,再打开
// db = helper.getReadableDatabase();
db = helper.getWritableDatabase();
}
public void Insert(ContentValues values) {
db.insert("UserTb", null, values);
}
}
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星