将字符串传递给 DatabasHelper 并在查询中使用它来搜索两个表之间的平均值
Passing String to DatabasHelper and using it in a query to search for an average between two tables
抱歉,我对此有点陌生。我正在使用 java 构建一个 android 应用程序。我正在尝试将两个字符串传递给我的 DatabaseHelper class 以查询我的数据库。这是我将字符串检索到数据库助手的方法。
private void retrieveDataFromSqlite(){
String selectedYear = String.valueOf(dateViewer.getYear());
String selectedMonth = String.valueOf(dateViewer.getMonth() +1);
String selectedDay = String.valueOf(dateViewer.getDayOfMonth());
String date = (selectedYear) + '-' + (selectedMonth) + '-' + selectedDay;
String email = textViewName.getText().toString();
Data dataSetter = new Data();
dataSetter.setDate(date);
dataSetter.setEmail(email);
String x;
x = databaseHelper.viewData();
textViewSugar.setText(x);
}
这是我的数据库助手。
public String viewData(){
//String x = "No Data For That Date";
SQLiteDatabase db = this.getReadableDatabase();
Data dataGetter = new Data();
String x = dataGetter.getEmail();
//final String MY_QUERY = "SELECT AVG(data) FROM data INNER JOIN user ON data.email = user.user_email";
db.close();
return x;
}
我的 getter 和 setter 也有一个数据 class。
public class 数据 {
private int data_id;
private int userId;
private int data;
private String email;
private String date;
public int getData_id(){ return data_id; }
public void setData_id(int data_id){ this.data_id = data_id;}
public int getUserId(){ return userId;}
public void setUserId(int userId){ this.userId = userId;}
public int getData(){return data;}
public void setData(int data){ this.data = data;}
public String getEmail(){return email;}
public void setEmail(String email){ this.email = email;};
public String getDate(){return date;}
public void setDate(String date){this.date = date;}
}
您正在函数 viewData 中创建一个新的 Data 实例
public String viewData(){
Data dataGetter = new Data();
}
它与 retrieveDataFromSqlite 函数的数据对象没有关系。
无需在 viewData 函数中创建新的数据对象,只需传递数据获取器,然后从中获取值
public String viewData(Data dataGetter){
//String x = "No Data For That Date";
SQLiteDatabase db = this.getReadableDatabase();
String x = dataGetter.getEmail();
//final String MY_QUERY = "SELECT AVG(data) FROM data INNER JOIN user ON data.email = user.user_email";
db.close();
return x;
}
然后从 retrieveDataFromSqlite 函数传递数据,如
x = databaseHelper.viewData(dataSetter);
抱歉,我对此有点陌生。我正在使用 java 构建一个 android 应用程序。我正在尝试将两个字符串传递给我的 DatabaseHelper class 以查询我的数据库。这是我将字符串检索到数据库助手的方法。
private void retrieveDataFromSqlite(){
String selectedYear = String.valueOf(dateViewer.getYear());
String selectedMonth = String.valueOf(dateViewer.getMonth() +1);
String selectedDay = String.valueOf(dateViewer.getDayOfMonth());
String date = (selectedYear) + '-' + (selectedMonth) + '-' + selectedDay;
String email = textViewName.getText().toString();
Data dataSetter = new Data();
dataSetter.setDate(date);
dataSetter.setEmail(email);
String x;
x = databaseHelper.viewData();
textViewSugar.setText(x);
}
这是我的数据库助手。
public String viewData(){
//String x = "No Data For That Date";
SQLiteDatabase db = this.getReadableDatabase();
Data dataGetter = new Data();
String x = dataGetter.getEmail();
//final String MY_QUERY = "SELECT AVG(data) FROM data INNER JOIN user ON data.email = user.user_email";
db.close();
return x;
}
我的 getter 和 setter 也有一个数据 class。 public class 数据 {
private int data_id;
private int userId;
private int data;
private String email;
private String date;
public int getData_id(){ return data_id; }
public void setData_id(int data_id){ this.data_id = data_id;}
public int getUserId(){ return userId;}
public void setUserId(int userId){ this.userId = userId;}
public int getData(){return data;}
public void setData(int data){ this.data = data;}
public String getEmail(){return email;}
public void setEmail(String email){ this.email = email;};
public String getDate(){return date;}
public void setDate(String date){this.date = date;}
}
您正在函数 viewData 中创建一个新的 Data 实例
public String viewData(){
Data dataGetter = new Data();
}
它与 retrieveDataFromSqlite 函数的数据对象没有关系。
无需在 viewData 函数中创建新的数据对象,只需传递数据获取器,然后从中获取值
public String viewData(Data dataGetter){
//String x = "No Data For That Date";
SQLiteDatabase db = this.getReadableDatabase();
String x = dataGetter.getEmail();
//final String MY_QUERY = "SELECT AVG(data) FROM data INNER JOIN user ON data.email = user.user_email";
db.close();
return x;
}
然后从 retrieveDataFromSqlite 函数传递数据,如
x = databaseHelper.viewData(dataSetter);