如您所见,我想创建一个外部类作为我的活动和数据库之间的适配器,以使其更加安全,只是拒绝使用内部类中的值,我尝试导入值,但导入它们没有意义,而我正试图隐藏它们 你能解决这个问题还是给我另一种方法来使我的代码正常工作
public class DataBaseConnection {
DataBase DataBase;
public DataBaseConnection(Context context) {
DataBase = new DataBase(context);
public void dataInsert (String classnumber , String studentsnumber) {
SQLiteDatabase sqLiteDatabase = dataBase.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(DataBase.classname);
contentValues.put(DataBase.studentsnumber);
}
}
class DataBase extends SQLiteOpenHelper {
private static final String database_name = "DB";
private static final int database_vesion =1;
private static final String UID = "id";
private static final String name = "Name";
private static final String work_mark = "Works Mark";
private static final String test_mark = "Tests mark";
private static final String tableName = "Students";
private static final String classname = "Class name";
private static final String studentsnumber = "Students number";
private Context context;
private static final String tablename1 = "Class";
private static final String CREATE_TABLE1 = "CREATE TABLE " +tablename1+" " +
" ("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT," +
" "+classname+" TEXT VARCHAR(255))," +
" "+studentsnumber+" INTEGER ;";
private static final String CREATE_TABLE = "CREATE TABLE " +tableName+" " +
" ("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT," +
" "+name+" TEXT VARCHAR(255))," +
" "+work_mark+" INTEGER ," +
" "+test_mark+" INTEGER ;";
private static final String DROP_TABLE = "DROP TABLE IF EXISTS " +tableName;
private static final String DROP_TABLE1 = "DROP TABLE IF EXISTS " +tablename1;
public DataBase(Context context) {
super(context, database_name,null, database_vesion);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
db.execSQL(CREATE_TABLE1);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DROP_TABLE);
db.execSQL(DROP_TABLE1);
onCreate(SQLiteDatabase,db);
}
}
}
答案 0 :(得分:0)
您有两个问题,您已将一个函数放置在一个函数内,该语法无效,它与类变量的可见性无关。另外,由于数据库变量是包作用域的,因此也不能真正确定此方法的安全性。要解决您眼前的问题,您需要这样的东西:
public class DataBaseConnection {
private final DataBase dataBase;
public DataBaseConnection(Context context) {
dataBase = new DataBase(context);
}
public void dataInsert (String classnumber , String studentsnumber) {
SQLiteDatabase sqLiteDatabase = dataBase.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(DataBase.classname, classnumber);
contentValues.put(DataBase.studentsnumber, studentsnumber);
sqLiteDatabase.insert( // insert here )
}
.. Rest of class
}