1

我有一个应用程序,它在 sqlite 数据库中有一些措施。

从一个活动(电子邮件活动)中,用户选择将哪些数据发送到医生的电子邮件(过去 1/7/30 天的数据)。

在此处输入图像描述

这是此活动的代码:

import com.oikonomopo.blood.pressure.tracker.dao.BpDAO;

...

public class EmailActivity extends Activity implements OnItemSelectedListener {

    Spinner spinner;
    String[] measures = { "today", "7 days", "30 days" };

    private Calendar theStart;
    private Calendar theEnd;
    SimpleDateFormat dateFormat;
    String start;
    String end;

    //this refers to another class for the manipulate of my table
    private BpDAO dao = null;

    private Button sendEmailBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.email);

        // ////////////////////////////////////
        theEnd = Calendar.getInstance();
        theStart = (Calendar) theEnd.clone();
        dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        // //////////////////////////////////

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                EmailActivity.this, android.R.layout.simple_spinner_item,
                measures);

        sendEmailBtn = (Button) findViewById(R.id.button1);
        spinner = (Spinner) findViewById(R.id.spinner1);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);

        // BpDao - refers to my table - see later
        dao = new BpDAO(this);

        // add a click listener to the button
        sendEmailBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
/////////////////////////////////////////////////////////////////////////////////////////
                Log.d("BPT", "before"); // it works untill here
                Cursor bpLastDaysList = dao.lastXdays_bp(start, end);
                Log.d("BPT", "after"); //doesn't reach the code here!!
/////////////////////////////////////////////////////////////////////////////////////////
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dao.close();
    }

    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        int position = spinner.getSelectedItemPosition();
        switch (position) {

                // last day
        case 0:
            theStart.add(Calendar.DAY_OF_MONTH, -1);

            // date boundaries in TEXT format
            start = dateFormat.format(theStart.getTime());
            end = dateFormat.format(theEnd.getTime());
            //
            break;

        // last 7 days
        case 1:
            theStart.add(Calendar.DAY_OF_MONTH, -7);

            // date boundaries in TEXT format
            start = dateFormat.format(theStart.getTime());
            end = dateFormat.format(theEnd.getTime());
            //
            break;

        // last 30 days
        case 2:
            theStart.add(Calendar.DAY_OF_MONTH, -30);

            // date boundaries in TEXT format
            start = dateFormat.format(theStart.getTime());
            end = dateFormat.format(theEnd.getTime());
            //
            break;
        }
    }

}

但它给了我这个logcat 错误

    FATAL EXCEPTION: main
android.database.sqlite.SQLiteException: near "09": syntax error: , while compiling: SELECT DISTINCT systolic, diastolic, datetime, _id, notes, pulses FROM bp_import WHERE datetime BETWEEN 2012-10-12 09:44 AND 2012-10-12 09:44 ORDER BY datetime DESC
at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
...
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)
at com.oikonomopo.blood.pressure.tracker.dao.BpDAO.lastXdays_bp(BpDAO.java:76)
at com.oikonomopo.blood.pressure.tracker.EmailActivity$1.onClick(EmailActivity.java:70)

android.database.sqlite.SQLiteException:接近“09”:语法错误:,编译时:SELECT DISTINCT systolic、diastolic、datetime、_id、notes、pulses FROM bp_import WHERE datetime BETWEEN 2012-10-12 09:44 AND 2012-10 -12 09:44 按日期时间 DESC 排序


BpDAO 类的函数代码是:

    public class BpDAO {
        private DatabaseHelper_bp dbHelper;
        private SQLiteDatabase database;
        /**
         * Movie table related constants.
         */
        public final static String bp_TABLE = "bp_import";
        public final static String bp_ID = "_id";
        public final static String bp_DT = "datetime";
        public final static String bp_SYS = "systolic";
        public final static String bp_DIA = "diastolic";
        public final static String bp_PUL = "pulses";
        public final static String bp_NOT = "notes";

        public BpDAO(Context context) {
            dbHelper = new DatabaseHelper_bp(context);
            database = dbHelper.getWritableDatabase();
        }

        /**
         * \ Creates a new blood pressure measure
         * 
         * @param datetime
         * @param systolic
         * @param diastolic
         * @param pulses
         * @param notes
         * @return
         */
        public long importBP(String datetime, String systolic, String diastolic,
                String pulses, String notes) {
            ContentValues values = new ContentValues();
            values.put(bp_DT, datetime);
            values.put(bp_SYS, systolic);
            values.put(bp_DIA, diastolic);
            values.put(bp_PUL, pulses);
            values.put(bp_NOT, notes);
            return database.insert(bp_TABLE, null, values);
        }

    // this is working perfect
        /**
         * Fetch all bps sorted DESCending
         * @return
         */
        public Cursor fetchAll_bp() {
            Cursor mCursor = database.query(true, bp_TABLE, new String[] { bp_SYS,
                    bp_DIA, bp_DT, bp_ID, bp_NOT, bp_PUL }, null, null, null, null,
                    bp_DT + " DESC", null);
            if (mCursor != null) {
                mCursor.moveToFirst();
            }
            return mCursor;
        }
    // here is the problem!
        /**
         * Fetch all bps sorted DESCending by date(yyyy-mm-dd hh:mm) of last
         * X[1-7-30] days
         * @return
         */
        public Cursor lastXdays_bp(String start, String end) {
            Cursor mCursor = database.query(true, bp_TABLE, new String[] { bp_SYS,
                    bp_DIA, bp_DT, bp_ID, bp_NOT, bp_PUL }, "datetime BETWEEN "
                    + start + " AND " + end, null, null, null, bp_DT + " DESC",
                    null);
            if (mCursor != null) {
                mCursor.moveToFirst();
            }
            return mCursor;
        }
//
        public void close() {
            database.close();
        }
    }

DatabaseHelper_bp(extends SQLiteOpenHelper) 的代码是:

    public class DatabaseHelper_bp extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "bpDB";
    private static final int DATABASE_VERSION = 1;

    // Database creation sql statement
    private static final String DATABASE_CREATE = "create table bp_import ( _id integer primary key, datetime text not null, systolic text not null, diastolic text not null, pulses text not null, notes text not null);";

    public DatabaseHelper_bp(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Method is called during creation of the database
    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(DATABASE_CREATE);
    }

    // Method is called during an upgrade of the database,
    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion,
            int newVersion) {
        Log.w(DatabaseHelper_bp.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
        database.execSQL("DROP TABLE IF EXISTS bp_import");
        onCreate(database);
    }
}

我将日期时间保存为 sqlite 中的文本。(2012-10-12 09:43)

4

2 回答 2

2
Cursor mCursor = database.query(true, bp_TABLE, new String[] { bp_SYS,
                bp_DIA, bp_DT, bp_ID, bp_NOT, bp_PUL }, "datetime BETWEEN ?
              AND ?" , new String[]{start,end}, null, null, bp_DT + " DESC",
                null);
于 2012-10-12T10:39:40.967 回答
1

日期字符串必须用 SQL 中的引号括起来 - 错误出现在 BpDAO/lastXdays_bp(String start, String end) 方法中。

而且Sardor Dushamov的方法更好

于 2012-10-12T10:36:35.423 回答