gpt4 book ai didi

java - SQLite 表返回空日期

转载 作者:太空宇宙 更新时间:2023-11-04 14:04:21 25 4
gpt4 key购买 nike

我正在开发一个 Android Activity ,该 Activity 读取用户输入的出生日期并使用该值实例化一个对象。然后该日期存储在 SQLite 表中。问题是当我稍后查询表中的日期时得到 NULL 日期值。

对象的构造函数正确接收日期。但是,稍后查询时会返回 NULL。

如果您能指出错误,那就太好了。

读取日期、实例化对象并存储在 SQLite 中的方法。正在从 EditText dob 读取该值

    b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

final EditText name = (EditText) findViewById(R.id.nameinput);
final EditText dob = (EditText) findViewById(R.id.dobinput);
final EditText mobile_no = (EditText) findViewById(R.id.mnuminput);
final EditText alt_no = (EditText) findViewById(R.id.altnuminput);
final EditText email_id = (EditText) findViewById(R.id.emailinput);
final EditText home_adr = (EditText) findViewById(R.id.homeadrinput);
final EditText off_adr = (EditText) findViewById(R.id.offadrinput);
final EditText notes = (EditText) findViewById(R.id.notesinput);
final TextView flag = (TextView) findViewById(R.id.insertflaglabel);

String name_converted = name.getText().toString();
String mobile_no_converted = mobile_no.getText().toString();
String alt_no_converted = alt_no.getText().toString();
String email_id_converted = email_id.getText().toString();
String home_adr_converted = home_adr.getText().toString();
String off_adr_converted = off_adr.getText().toString();
String notes_converted = notes.getText().toString();

DateFormat df = DateFormat.getDateInstance();
Date dob_converted = new Date(DateFormat.MEDIUM);

try {
dob_converted = df.parse(dob.getText().toString());

} catch (ParseException e) {
e.printStackTrace();
}

Log.d("add_client", "Date read is " +dob_converted);
client client_new = new client(name_converted, dob_converted, mobile_no_converted, alt_no_converted, home_adr_converted, off_adr_converted, email_id_converted, notes_converted);

Context context = getBaseContext();
database_handler insert_client = new database_handler(context);
Log.d("add_client", "Date instantiated is " +client_new.getDOB());
insert_client.addClient(client_new);
flag.setText("Client Details Saved!");
}

});
}

使用 .getDOB() 查询 SQLite 出生日期的方法

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_clientdetails);

Bundle extras = getIntent().getExtras();
Integer tab_id = 0;

if (extras != null) {
tab_id = (int) extras.getInt("client_id");
}

TextView namev = (TextView) findViewById(R.id.namevalue);
TextView mobnum = (TextView) findViewById(R.id.mnumvalue);
TextView date_ob = (TextView) findViewById(R.id.dobvalue);
TextView altnum = (TextView) findViewById(R.id.altnumvalue);
TextView off_addr = (TextView) findViewById(R.id.offadrvalue);
TextView home_addr = (TextView) findViewById(R.id.homeadrvalue);
TextView email_addr = (TextView) findViewById(R.id.emailvalue);
TextView notes_client = (TextView) findViewById(R.id.notesvalue);

Context c_displayclient = getBaseContext();
database_handler dhandle = new database_handler(c_displayclient);

String TAG = "display_clientdetails";

Log.d(TAG, "Value of ID is " +tab_id);
final client c_details = dhandle.getClient(tab_id);
namev.setText(c_details.getName());
mobnum.setText(c_details.getMobile_number());
altnum.setText(c_details.getAlt_num());


DateFormat datef = DateFormat.getDateInstance(DateFormat.MEDIUM);

Log.d("display_clientdetails", "Date of Birth is " +c_details.getDOB());


String text_date = datef.format(c_details.getDOB());
date_ob.setText(text_date);

实例化代码

    // constructor
public client(String name,Date dob,String mobile_number,String alternate_number,String office_address, String home_address,String email_id,String notes)
{

this.name = name;
this.dob=dob;
Log.d("client", "dob received in client constructor is " +this.dob);
this.mobile_number = mobile_number;
this.alternate_number=alternate_number;
this.office_address=office_address;
this.home_address=home_address;
this.email_id=email_id;
this.notes=notes;
}

返回出生日期的方法

    // getting Date of Birth
public Date getDOB(){
Log.d("client", "Date returned is " +this.dob);
return this.dob;

}

向数据库表添加新条目的方法

  public void addClient(client client_instance) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(KEY_NAME, client_instance.getName()); // Client Name
values.put(KEY_MO_NO, client_instance.getMobile_number()); // Client Phone
values.put(KEY_DOB, client_instance.getDOB().toString());
values.put(KEY_ALT_NO, client_instance.getAlt_num());
values.put(KEY_OFF_ADR, client_instance.getOff_adr());
values.put(KEY_HOM_ADR, client_instance.getHome_adr());
values.put(KEY_EMAIL, client_instance.getEmail());
values.put(KEY_NOTES, client_instance.getNotes());

db.insert(TABLE_CLIENTS, null, values);
db.close(); // Closing database connection

}

从数据库获取数据的方法

 // Getting single contact
client getClient(int id) {
SQLiteDatabase db = this.getReadableDatabase();
client client_instance = new client();
String TAG ="database_handler";

Cursor cursor = db.query(TABLE_CLIENTS, new String[] {
KEY_NAME, KEY_MO_NO, KEY_DOB, KEY_ALT_NO, KEY_OFF_ADR, KEY_HOM_ADR, KEY_EMAIL, KEY_NOTES}, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);


if(cursor != null) Log.d(TAG, "cursor is not null");
// if(cursor.moveToFirst()) Log.d(TAG, "move to first is true");

if (cursor != null && cursor.moveToFirst()) {

client_instance.setName(cursor.getString(0));
client_instance.setMobileNumber(cursor.getString(1));

//Convert Datefield from String to Date so that the setDOB method can accept it as argument

DateFormat dateform = DateFormat.getDateInstance(DateFormat.FULL);
Date d_of_b = new Date(DateFormat.FULL);

try {
d_of_b = dateform.parse(cursor.getString(2));

} catch (ParseException e) {
e.printStackTrace();
}

//Set Date of Birth
client_instance.setDOB(d_of_b);

client_instance.setAlt_num(cursor.getString(3));
client_instance.setOff_adr(cursor.getString(4));
client_instance.setHome_adr(cursor.getString(5));
client_instance.setEmail(cursor.getString(6));
client_instance.setNotes(cursor.getString(7));
Log.d(TAG, "Email is " +cursor.getString(3));
cursor.close();
}

return client_instance;

}

将出生日期设置到新客户端实例的方法

   public void setDOB(Date dob){
this.dob = dob;
}

最佳答案

  1. 您的问题是在格式化和解析日期时必须使用相同的日期格式。

  2. 您应该使用特定的日期格式,例如“MM/dd/yyyy”

  3. 您的解决方案是

    • 当您格式化日期时(将客户端插入 SQLite 时)

      SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
      String dobAsString = format.format ( dob_Date_object);
      // Store dobAsString in SQLite
    • 当您解析日期时(从 SQLite 读取客户端时)

      SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
      String dobAsString = // ... Read from SQLite

      Date dob = format.parse(dobAsString);
  4. 您不应使用 String 来存储 DATE 数据。您应该使用长数据类型(将日期转换为时间戳 - 长数据类型)。原因见下:

    • 如果您使用字符串:“06/12/2014”大于“03/12/2015”:错误!!!
    • 如果您使用 Long:Long_timestamp("06/12/2014") 小于 Long_timestamp("03/12/2015"):正确

关于java - SQLite 表返回空日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29017896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com