寻找一些信息和建议,尝试在我的应用中的SQLite数据库中完成Class.forName("org.neo4j.jdbc.Driver")
字段中的冗余数据设置。主要问题是两个属性字段,当数据库中没有数据时,SQLite似乎存储并检索Class.forName("org.neo4j.jdbc.Driver").newInstance()
类型字段的默认值0.0,我想尝试而不是为这两个字段设置任何内容。所以我不确定是否需要在DBHelper类的onCreate()中进行寻址和调整,因为我在创建数据库表时目前将这两个字段都设置为EditText
。任何建议或建议将不胜感激。
这是来自我的REAL
类real not null
DBHelper
SQLiteOpenHelper
方法
/* All the below will define the column names schema for the database. */
private static final String UID = "id";
private static final String JOINT_ID = "Custom_Joint_Id";
private static final String JT_NUM = "Manufacturer_Joint_Number";
private static final String JOINT_HEAT = "Heat";
private static final String JOINT_LENGTH = "Joint_Length";
private static final String MANUFACTURER = "Joint_Manufacturer";
private static final String WALL_THICKNESS = "Joint_Wall_Thickness";
private static final String JT_DIAMETER = "Joint_Size_Diameter";
private static final String GRADE = "Grade";
private static final String COATING_TYPE = "Joint_Coating_Type";
private static final String COATING_THICKNESS = "Joint_Coating_Thickness";
private static final String LOCATION = "Current_Location";
private static final String CUTS = "Cuts";
private static final String NEW_LENGTH = "New_Length";
private static final String CHILD_PIPE_ID = "Child_Pipe_Id";
private static final String NOTES = "Notes";
private static final String DATE = "Date";
这是onCreate()
类中的方法,我在其中检查数据库以查看它是否包含任何数据或为空。如果找到数据,我想获得最新的记录。
@Override
public void onCreate(SQLiteDatabase db) {
try {
/* Creating the db */
db.execSQL("create table if not exists " + TABLE_NAME + " (" + UID + " integer primary key autoincrement, " + JOINT_ID + " text unique not null, " + JT_NUM + " text not null, " + JOINT_HEAT + " text not null, " + JOINT_LENGTH + " real not null, " + MANUFACTURER + " text not null, " + WALL_THICKNESS + " real not null, " + JT_DIAMETER + " real not null, " + GRADE + " text not null, " + COATING_TYPE + " text, " + COATING_THICKNESS + " text, " + LOCATION + " text, " + CUTS + " real null, " + NEW_LENGTH + " real null, " + CHILD_PIPE_ID + " text null, " + NOTES + " text null, " + DATE + " text)");
} catch (SQLException e) {
e.printStackTrace();
} /* end of try/catch here */
} /* onCreate ends here. */
这是应用程序的DBHelper
类,其中进行数据收集,我也使用/*
This method passes and displays redundant pipe data to the TallyActivity2 activity This pipe data
normally stays the same after initial tally has happened
*/
public Pipe displaySomePipeInfo() {
SQLiteDatabase dbDisplayInfo = this.getReadableDatabase();
Cursor cursorDisplayInfo = null;
String selectCriteriaHighest = "select * from " +TABLE_NAME + " where id = (select max(id) from " +TABLE_NAME+")";
try {
cursorDisplayInfo = dbDisplayInfo.rawQuery(selectCriteriaHighest, null);/* Getting the most recent record */
Pipe pipe = new Pipe(); // Creating an instance of the Pipe class
if (cursorDisplayInfo != null && cursorDisplayInfo.moveToFirst() && cursorDisplayInfo.getCount() == 0) {
/*
Empty database
I don't want to do anything but just leave since there is no data in the database.
*/
cursorDisplayInfo.close();
dbDisplayInfo.close();
} else {
if (cursorDisplayInfo != null && cursorDisplayInfo.moveToLast()) {
/*
If there is data in the database, I want to display certain attributes for the most recent
set of data entered to keep from having to type them all in again
*/
pipe.setJointManufacturer(cursorDisplayInfo.getString(cursorDisplayInfo.getColumnIndex(MANUFACTURER)));
pipe.setWallThickness(cursorDisplayInfo.getDouble(cursorDisplayInfo.getColumnIndex(WALL_THICKNESS)));
pipe.setSizeDiameter(cursorDisplayInfo.getDouble(cursorDisplayInfo.getColumnIndex(JT_DIAMETER)));
pipe.setGrade(cursorDisplayInfo.getString(cursorDisplayInfo.getColumnIndex(GRADE)));
pipe.setCoatingType(cursorDisplayInfo.getString(cursorDisplayInfo.getColumnIndex(COATING_TYPE)));
pipe.setCoatingThick(cursorDisplayInfo.getString(cursorDisplayInfo.getColumnIndex(COATING_THICKNESS)));
return pipe;
}
}
} catch (NullPointerException e) {
e.printStackTrace();
} finally {
if (cursorDisplayInfo != null) {
cursorDisplayInfo.close();
}
dbDisplayInfo.close();
}
return null;
}
方法在正确的EditText字段中显示冗余数据。
TallyActivity2
我在同一个redundantPipeInfo()
/*
This method displays redundant pipe data in the EditText fields for certain ones that normally
remain unchanged once initial tally is started. It is called in the onCreate() activity from
this class.
*/
private void redundantPipeInfo(){
// An instance of the DBHelper class
DBHelper dbHelperRedunData = null;
try {
dbHelperRedunData = new DBHelper(TallyActivity2.this);
/* calling the method from the DBHelper class and assigning it to instance of the Pipe class */
mRedundantPipe = dbHelperRedunData.displaySomePipeInfo();
/*
Setting the EditText fields of the redundant data
*/
mEtPipeManufacturer.setText(mRedundantPipe.getJointManufacturer());
mEtPipeWallThick.setText(String.valueOf(mRedundantPipe.getWallThickness()));
mEtPipeGrade.setText(mRedundantPipe.getGrade());
mEtPipeCoatType.setText(mRedundantPipe.getCoatingType());
mEtPipeCoatThick.setText(mRedundantPipe.getCoatingThick());
mEtPipeSizeDiameter.setText(String.valueOf(mRedundantPipe.getSizeDiameter()));
} catch (SQLException | ClassNotFoundException | NoSuchFieldException e) {
e.printStackTrace();
} finally {
if (dbHelperRedunData != null) {
dbHelperRedunData.close();
}
}
}
方法中调用redundantPipeInfo()
方法,以便每次启动此活动时它始终存在且可用,因为这是数据收集所需的位置的地方。
TallyActivity2
我最大的问题是如果有可能,如何实现这一目标。我最大的目标是为这两个字段设置一个空白的EditText,特别是如果它们是空的,而不是SQLite的默认值0.0。谢谢你的帮助。