Android 我的SqLite助手类onCreateMethod根本不调用

Android 我的SqLite助手类onCreateMethod根本不调用,android,database,sqlite,android-sqlite,Android,Database,Sqlite,Android Sqlite,我无法使用SqliteOpenHelper创建数据库。我的OnCreate方法根本不运行 我已经尝试过getReadable和getWriteable数据库方法,但没有任何改变 public类SQLiteHandler扩展SQLiteOpenHelper{ 私有静态SQLiteHandler; 私有静态最终字符串数据库\u NAME=“telestaDB”; 私有静态最终int数据库_VERSION=1; 私有静态最终字符串TAG=“SqliteHelper”; 公共SQLiteHandler(

我无法使用SqliteOpenHelper创建数据库。我的OnCreate方法根本不运行

我已经尝试过getReadable和getWriteable数据库方法,但没有任何改变

public类SQLiteHandler扩展SQLiteOpenHelper{
私有静态SQLiteHandler;
私有静态最终字符串数据库\u NAME=“telestaDB”;
私有静态最终int数据库_VERSION=1;
私有静态最终字符串TAG=“SqliteHelper”;
公共SQLiteHandler(上下文){
super(上下文、数据库名称、null、数据库版本);
getReadableDatabase();
getWritableDatabase();
Log.i(标记“Constractor create!!”);
}
公共静态SQLiteHandler getInstance(上下文){
if(sInstance==null){
Log.i(标记“getInstance:newinstance created!!”;
sInstance=newsqlitehandler(context.getApplicationContext());
}
回归承诺;
}
@凌驾
public void onCreate(SQLiteDatabase SQLiteDatabase){
sqLiteDatabase.execSQL(exceptionmodel.CREATE_表);
sqLiteDatabase.execSQL(DownloadModel.CREATE_TABLE);
}
@凌驾
public void onUpgrade(SQLiteDatabase SQLiteDatabase,inti,inti1){
}
/*
*
*@覆盖
public void onCreate(SQLiteDatabase db){
execSQL(exceptionmodel.CREATE_表);
d(标记“onCreate:databaseCreated!!”;
db.execSQL(DownloadModel.CREATE_TABLE);
}
@凌驾
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
execSQL(exceptionmodel.CREATE_表);
Log.d(标记“onUpgrade:databaseCreated!!”;
db.execSQL(DownloadModel.CREATE_TABLE);
}
}
当我尝试创建helper类的对象并尝试将数据插入该对象时,没有发生任何事情。我使用这些行来创建db

SQLiteHandler-sqliteHelper=SQLiteHandler.getInstance(上下文);
sqliteHelper.getWritableDatabase();
我已经看到了一些关于这个问题的其他问题,但没有人帮助

in
oncreate

您没有创建查询的方法

更改->

https://www.javatpoint.com/android-sqlite-tutorial

中的
oncreate

您没有创建查询的方法

更改->


https://www.javatpoint.com/android-sqlite-tutorial

这是我所做的代码,对我来说效果很好 根据你的需要调整它。希望它也能为你工作

public class DatabaseHelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME ="register.db";
public static final String TABLE_NAME ="registeruser";
public static final String COL_1 ="ID";
public static final String COL_2 ="username";
public static final String COL_3 ="password";


public static final String TABLE2_NAME="Student";
public static final String COL_ONE="ID";
public static final String COL_TWO="RegNum";


public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
    //SQLiteDatabase db=this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL("CREATE TABLE registeruser (ID INTEGER PRIMARY  KEY AUTOINCREMENT, username TEXT, password TEXT)");
    sqLiteDatabase.execSQL("CREATE TABLE Student(ID INTEGER PRIMARY KEY AUTOINCREMENT, RegNum TEXT)");


}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL(" DROP TABLE IF EXISTS " + TABLE_NAME);

    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE2_NAME);
    onCreate(sqLiteDatabase);
}

public long addUser(String user, String password){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("username",user);
    contentValues.put("password",password);
    long res = db.insert("registeruser",null,contentValues);
    db.close();
    return  res;
}

public boolean checkUser(String username, String password){
    String[] columns = { COL_1 };
    SQLiteDatabase db = getReadableDatabase();
    String selection = COL_2 + "=?" + " and " + COL_3 + "=?";
    String[] selectionArgs = { username, password };
    Cursor cursor = db.query(TABLE_NAME,columns,selection,selectionArgs,null,null,null);
    int count = cursor.getCount();
    cursor.close();
    db.close();

    if(count>0)
        return  true;
    else
        return  false;
}



public boolean markAttendance(String regnum ){
    SQLiteDatabase db=this.getWritableDatabase();
    ContentValues contentValues=new ContentValues();
    contentValues.put(COL_TWO,regnum);
    long result=db.insert(TABLE2_NAME ,null , contentValues);

    if (result==-1)

        return false;

    else

        return true;

}
public Cursor getAllAttendance(){

    SQLiteDatabase db=this.getWritableDatabase();

    Cursor res= db.rawQuery("select * From " + TABLE2_NAME,null );
    return res;

}

}

这是我所做的代码,对我来说很好 根据你的需要调整它。希望它也能为你工作

public class DatabaseHelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME ="register.db";
public static final String TABLE_NAME ="registeruser";
public static final String COL_1 ="ID";
public static final String COL_2 ="username";
public static final String COL_3 ="password";


public static final String TABLE2_NAME="Student";
public static final String COL_ONE="ID";
public static final String COL_TWO="RegNum";


public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
    //SQLiteDatabase db=this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL("CREATE TABLE registeruser (ID INTEGER PRIMARY  KEY AUTOINCREMENT, username TEXT, password TEXT)");
    sqLiteDatabase.execSQL("CREATE TABLE Student(ID INTEGER PRIMARY KEY AUTOINCREMENT, RegNum TEXT)");


}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL(" DROP TABLE IF EXISTS " + TABLE_NAME);

    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE2_NAME);
    onCreate(sqLiteDatabase);
}

public long addUser(String user, String password){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("username",user);
    contentValues.put("password",password);
    long res = db.insert("registeruser",null,contentValues);
    db.close();
    return  res;
}

public boolean checkUser(String username, String password){
    String[] columns = { COL_1 };
    SQLiteDatabase db = getReadableDatabase();
    String selection = COL_2 + "=?" + " and " + COL_3 + "=?";
    String[] selectionArgs = { username, password };
    Cursor cursor = db.query(TABLE_NAME,columns,selection,selectionArgs,null,null,null);
    int count = cursor.getCount();
    cursor.close();
    db.close();

    if(count>0)
        return  true;
    else
        return  false;
}



public boolean markAttendance(String regnum ){
    SQLiteDatabase db=this.getWritableDatabase();
    ContentValues contentValues=new ContentValues();
    contentValues.put(COL_TWO,regnum);
    long result=db.insert(TABLE2_NAME ,null , contentValues);

    if (result==-1)

        return false;

    else

        return true;

}
public Cursor getAllAttendance(){

    SQLiteDatabase db=this.getWritableDatabase();

    Cursor res= db.rawQuery("select * From " + TABLE2_NAME,null );
    return res;

}

}

好的,我发现我的代码出现了问题,它是基于单例模式的,我不知道为什么,但它确实阻止了我的代码的运行,只需删除它,它的工作很好,我在这里发送代码可能对其他人有所帮助

这里是我的SQLiteHelper类

public类SQLiteHandler扩展SQLiteOpenHelper{
私有静态最终字符串数据库\u NAME=“telestaDB”;
私有静态最终int数据库_VERSION=1;
私有静态最终字符串TAG=“SqliteHelper”;
私有SQLiteHandler(上下文){
super(上下文、数据库名称、null、数据库版本);
getWritableDatabase();
getReadableDatabase();
Log.i(标记“Constractor create!!”);
}
公共无效添加例外(例外模型){
SQLiteDatabase db=getWritableDatabase();
db.beginTransaction();
试一试{
ContentValues=新的ContentValues();
//value.put(exceptionmodel.COLUMN_ID,model.getId());
value.put(exceptionmodel.USER_NAME,model.getUserName());
db.insert(exceptionmodel.TABLE_NAME,null,value);
db.setTransactionSuccessful();
db.endTransaction();
}捕获(例外e){
Log.d(标记“尝试将用户添加到数据库时出错”+e.toString());
}
}
公共void deleteException(字符串模型){
SQLiteDatabase db=getWritableDatabase();
db.beginTransaction();
试一试{
db.delete(exceptionmodel.TABLE_NAME,exceptionmodel.USER_NAME+“=?”,新字符串[]{model});
}捕获(忽略异常){
Log.d(标记“试图从数据库中删除用户时出错”);
}最后{
db.endTransaction();
}
}
公共列表getAllExceptions(){
列表异常=新建ArrayList();
SQLiteDatabase db=getReadableDatabase();
Cursor Cursor=db.rawQuery(ExceptionsModel.SELECT,null);
试一试{
while(cursor.moveToNext()){
ExceptionsModel=新的ExceptionsModel();
/*setId(cursor.getInt(cursor.getColumnIndex(exceptionModel.COLUMN_ID))*/
model.setUserName(cursor.getString(cursor.getColumnIndex(exceptionModel.USER_NAME));
例外情况。添加(模型);
}
}捕获(例外e){
Log.d(标记“尝试从数据库获取异常时出错”+e.toString());
}最后{
if(cursor!=null&!cursor.isClosed()){
cursor.close();
}
}
返回异常;
}
@凌驾
public void onCreate(SQLiteDatabase SQLiteDatabase){
Log.i(标记“getInstance:newinstance created!!”;
sqLiteDatabase.execSQL(exceptionmodel.CREATE_表);
}
@凌驾
public void onUpgrade(SQLiteDatabase SQLiteDatabase,inti,inti1){
}
}
这里是我的模型课

公共类例外模型{
公共静态最终字符串表\u NAME=“user\u exceptions”;
公共静态最终字符串列\u ID=“ID”;
公共静态最终字符串USER\u NAME=“username”;
公共静态最终字符串CREATE_TABLE=“CREATE TABLE IF NOT EXISTS”+TABLE_NAME+”(“
+列_ID+“I