我只是想知道你是否可以帮我解决我遇到过的问题 我创建了一个SQLite数据库,我希望根据是否在特定日期服用药物来创建一个饼图。
创建表格的代码
// sql query to create table for daily routine
String sql6 = "CREATE TABLE "
+ TABLE_DAILY_ROUTINE + " ( "
+ COL_DAILY_ROUTINE_DATE + " VARCHAR, "
+ COL_DAILY_ROUTINE_TIME_ID + " INTEGER,"
+ COL_DAILY_ROUTINE_TIME_HOUR + " INTEGER , "
+ COL_DAILY_ROUTINE_TIME_MIN + " INTEGER , "
+ COL_DAILY_ROUTINE_MED_TAKEN + " TEXT ,"
+ COL_DAILY_ROUTINE_MED_QUANTITY + " INTEGER, "
+ COL_DAILY_ROUTINE_MED_ID + " VARCHAR, "
+ "PRIMARY KEY ( " + COL_DAILY_ROUTINE_DATE + ", " + COL_DAILY_ROUTINE_TIME_ID + "),"
+ "FOREIGN KEY ( " + COL_DAILY_ROUTINE_MED_ID + ") REFERENCES " + TABLE_MEDICATION + "("
+ COL_MEDICATION_ID + ") ON DELETE CASCADE);";
饼图活动
public class PieChart extends Activity {
SQLiteDatabase ourDatabase;
Database db;
DatabaseHelper dbHelper;
String date;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pie_chart_layout);
db = new Database(this);
db.open();
dbHelper = new DatabaseHelper(this);
ourDatabase = dbHelper.getWritableDatabase();
String sql = "SELECT DAILY_ROUTINE_MED_TAKEN, COUNT(DAILY_ROUTINE_MED_TAKEN) FROM DAILY_ROUTINE\n" +
"WHERE DAILY_ROUTINE_DATE = ' + date + ' GROUP BY DAILY_ROUTINE_MED_TAKEN " ;
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
calendar.add(Calendar.DATE, 1);
date = sdf.format(calendar.getTime());
Cursor c = ourDatabase.rawQuery(sql, null);
int count = c.getCount();
int[] colors={Color.RED, Color.GREEN};
double[] values = new double[count];
String[] categoryNames = new String[count];
for (int i = 0; i < count; i++) {
c.moveToNext();
categoryNames[i] = c.getString(0);
values[i] = c.getDouble(1);
}
// Instantiating CategorySeries to plot Pie Chart
CategorySeries distributionSeries = new CategorySeries(" Android version distribution as on October 1, 2012");
for (int i = 0; i < categoryNames.length; i++) {
// Adding a slice with its values and name to the Pie Chart
distributionSeries.add(categoryNames[i], values[i]);
}
// Instantiating a renderer for the Pie Chart
DefaultRenderer defaultRenderer = new DefaultRenderer();
for (int i = 0; i < categoryNames.length; i++) {
SimpleSeriesRenderer seriesRenderer = new SimpleSeriesRenderer();
seriesRenderer.setColor(colors[i]);
seriesRenderer.setDisplayChartValues(true);
defaultRenderer.addSeriesRenderer(seriesRenderer);
}
defaultRenderer.setChartTitle("Patient Daily Adherance to Medication");
defaultRenderer.setChartTitleTextSize(60);
defaultRenderer.setZoomButtonsVisible(false);
defaultRenderer.setLegendTextSize(70);
defaultRenderer.setLabelsTextSize(80);
Intent intent = ChartFactory.getPieChartIntent(getApplicationContext(), distributionSeries, defaultRenderer, "AChartEnginePieChartDemo");
// Start Activity
startActivity(intent);
c.close();
ourDatabase.close();
}
}
数据库是正确的,当我硬编码日期以替换字符串日期时,我会看到一个饼图 字符串日期是前面的一天,因为这是日期在数据库中的时间。这不是问题。
我没有得到任何错误 标题显示,但没有饼图,你可以帮我解决问题,问题是什么 我确定它与初始for循环有关,但不能指责它。
答案 0 :(得分:0)
我认为你需要移动一些代码行,并且还要连接date
变量的值。
现在,您正在尝试查询列明确等于' + date + '
字符串的位置。
// What is 'date' here? null, most likely...
String sql = "SELECT DAILY_ROUTINE_MED_TAKEN, COUNT(DAILY_ROUTINE_MED_TAKEN) FROM DAILY_ROUTINE\n" +
"WHERE DAILY_ROUTINE_DATE = ' + date + ' GROUP BY DAILY_ROUTINE_MED_TAKEN " ;
// Here's where you do get the value of the date string
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
calendar.add(Calendar.DATE, 1);
date = sdf.format(calendar.getTime());
// move the sql down here
我还建议尝试使用SqliteDatabase类的query
方法而不是execSQL
或rawQuery
,但这是个人偏好