我在使用SQLite的应用程序上处理日期时遇到了一些麻烦
这是我的表,其中包括日期数据类型
1.
我从对话框选择器中选择fromdate和todate,当我点击show button时出现错误,如下所示:
Process: com.example.patidar.datedb, PID: 2471 android.database.sqlite.SQLiteException: near "Oct": syntax error (code 1): , while compiling: select * from NewDATE where DATE BETWEEN Tue Oct 10 00:00:00 GMT+05:30 2017 AND Thu Oct 26 00:00:00 GMT+05:30 2017
这是我的数据库助手中的onCreate
方法: -
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String CREATE_TABLE="CREATE TABLE "+ TBNAME +" ( "+ID +" INTEGER PRIMARY KEY AUTOINCREMENT,"+DATE+" date,"+NAME+" TEXT)";
sqLiteDatabase.execSQL(CREATE_TABLE);
}
我在DBHelper
课程中使用以下方法检索日期。在这里我把所有代码
public ArrayList<ADate> retrieveByUseDate(String fromdate,String todate) throws ParseException {
SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sfd.parse(fromdate);
Date date2 = sfd.parse(todate);
SQLiteDatabase database = getReadableDatabase();
ArrayList<ADate> arrayList = new ArrayList<ADate>();
Cursor cursor = database.rawQuery("select * from " + TBNAME + " where DATE BETWEEN " + date1+ " AND " + date2, null);
System.out.println(cursor);
while (cursor.moveToNext()) {
int id = cursor.getInt(0);
String selectdate = cursor.getString(1);
String aname = cursor.getString(2);
ADate date = new ADate(id, selectdate, aname);
arrayList.add(date);
}
return arrayList;
}
我使用Datepicker对话框从活动类
中的日历中检索日期public class RetrieveData extends AppCompatActivity implements View.OnClickListener{
Button btnFromDate,btnToDate,btnshow;
ListView lvRecord;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retrieve_data);
btnFromDate= (Button) findViewById(R.id.btnFromDate);
btnToDate= (Button) findViewById(R.id.btnToDate);
btnshow= (Button) findViewById(R.id.btnShow);
lvRecord= (ListView) findViewById(R.id.lvRecord);
btnFromDate.setOnClickListener(this);
btnToDate.setOnClickListener(this);
btnshow.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btnFromDate:
fromDate();
break;
case R.id.btnToDate:
toDate();
break;
case R.id.btnShow:
try {
dateRecord();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
public void fromDate(){
Calendar c= Calendar.getInstance();
final int yr=c.get(Calendar.YEAR);
final int month=c.get(Calendar.MONTH);
final int day=c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dd=new DatePickerDialog(RetrieveData.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
String date=i+"-"+(i1+1)+"-"+i2;
btnFromDate.setText(date);
}
},yr,month,day);
dd.show();
}
public void toDate(){
Calendar c= Calendar.getInstance();
final int yr=c.get(Calendar.YEAR);
final int month=c.get(Calendar.MONTH);
final int day=c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dd=new DatePickerDialog(RetrieveData.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
String date=i+"-"+(i1+1)+"-"+i2;
btnToDate.setText(date);
}
},yr,month,day);
dd.show();
}
public void dateRecord() throws ParseException {
String fromdate=btnFromDate.getText().toString();
String todate=btnToDate.getText().toString();
DBHelper db=new DBHelper(RetrieveData.this);
ArrayList<ADate> aDateArrayList=db.retrieveByUseDate(fromdate,todate);
ArrayAdapter<ADate> aDateArrayAdapter=new ArrayAdapter<ADate>(RetrieveData.this,android.R.layout.simple_list_item_1,aDateArrayList);
lvRecord.setAdapter(aDateArrayAdapter);
}
答案 0 :(得分:0)
这是一个DatePicker,它获取日期和样式它看起来像这个10-20-2017如果日期只有7或6个字符长它首先添加零“0”然后破折号如果你没有添加破折号所有日期看起来像这样02032017这种格式非常适合存储,但不要查看因此添加破折号我们将发布日期选择器代码和代码以在将日期添加回数据库之前删除破折号
日期选择器此选择器位于其自己的XML文件中,也将发布XML
public void findByDate(){
etPurchaseDate.setText("");
// DATE PICKER Sub Routine the XML file holds the Date Picker see
datepickerview.xml
final Dialog dialog = new
Dialog(DetailsActivity.this,R.style.DatePickerTheme);
/* the style and reference to the XML MUST be included in the lines os
code above and below */
/*
dialog.setContentView(R.layout.datepickerview);
dialog.setTitle("");
DatePicker picker = dialog.findViewById(R.id.datePicker);
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
picker.updateDate(mYear, mMonth, mDay);// Keeps Calendar initial view to
what today is!
picker.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker picker, int year, int
monthOfYear, int dayOfMonth) {
String searchFrom = (String.valueOf(monthOfYear + 1)) +
(String.valueOf(dayOfMonth)) + String.valueOf(year);
StringBuilder str = new StringBuilder(searchFrom);
if (searchFrom.length() == 7) {
str.insert(2, '0');
str.insert(2, '-');
str.insert(5, '-');
etPurchaseDate.setText(str);
} else if (searchFrom.length() == 6) {
str.insert(0, '0');
str.insert(2, '0');
str.insert(2, '-');
str.insert(5, '-');
etPurchaseDate.setText(str);
} else {
etPurchaseDate.setText(searchFrom);
etPurchaseDate.requestFocus();
etPurchaseDate.setSelection(8);
}
etCost.requestFocus();
dialog.dismiss();
}
});
dialog.show();
}
for date picker xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_White"
android:orientation="vertical">
<DatePicker
android:id="@+id/datePicker"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:calendarViewShown="true"
android:headerBackground="@color/color_Purple"
android:theme="@style/DatePickerTheme" />
</LinearLayout>
使用此代码在保存数据之前删除破折号
if(etPurchaseDate.length()==10) {
/* NO spaces \ No = \ NO - \ NO -- \ ONLY
numbers \ NO - at the end*/
String tstr = "^(?!.*\\s)^(?=.*)^(?!-)(?!.*--)[0-9-]+(?
<!-)$";
String astr = etPurchaseDate.getText().toString().trim();
Pattern regex = Pattern.compile(tstr);
Matcher regexMatcher = regex.matcher(astr);
boolean foundMatch = regexMatcher.find();
if (foundMatch == true ) {//IF FOUND REMOVE THE DASH'S AND STORE DATA
/* Routine puts DATE back in DB as 8 Character String REMOVES dashes "-" */
StringBuilder removeTHIS = new
StringBuilder(etPurchaseDate.getText().toString());
String value = removeTHIS.toString().replace("-", "");
etPurchaseDate.setText(value);
etCost.requestFocus();
purchase_date = value;
etCost.requestFocus();
//return;
Toast.makeText(DetailsActivity.this, "Date was Formatted
for Storage", Toast.LENGTH_LONG).show();
}
}