gpt4 book ai didi

java - 如果日期不在 Android 的该表中,如何将日期插入用户输入的表中

转载 作者:行者123 更新时间:2023-12-02 07:52:30 25 4
gpt4 key购买 nike

我正在从用户那里获取日期输入,我想检查该日期是否在表中,然后获取与该日期对应的数据,否则将日期插入该表中。我正在使用以下代码并在选择查询中遇到问题。我的代码如下:

package com.foursquaregame.in;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Astro_talk extends Activity{
EditText dob;
Button submit;
SQLiteDatabase db_sunshine=null;
String Data="";
String Data1="";
String TableName="tb_astro";
String dob_var;
String date_var;
String quote;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.astro_talk);
//quote="Don't worry, Be happy";
dob=(EditText)findViewById(R.id.dob);
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
submit=(Button)findViewById(R.id.submit);
submit.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
java.util.Date dateObject;

System.out.println("submit data");
dob_var=dob.getText().toString();
try {
dateObject = sdf.parse(dob_var);
date_var = new SimpleDateFormat("yyyy/MM/dd").format(dateObject);

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println(dob_var);
try
{
db_sunshine=Astro_talk.this.openOrCreateDatabase("db_hellosunshine", MODE_PRIVATE, null);
db_sunshine.execSQL("CREATE TABLE IF NOT EXISTS "+ TableName + " (d_o_b NUMERIC,astro_quote DATE))");
Cursor c=db_sunshine.rawQuery("SELECT astro_quote FROM "+ TableName +"WHERE d_o_b='"+ date_var +"'" , null);
System.out.println("query executed");
int column1=c.getColumnIndex("d_o_b");
int column2=c.getColumnIndex("astro_quote");
c.moveToFirst();
if(c!=null)
{

{
String date=c.getString(column1);
String astro =c.getString(column2);
Data=Data+date+"\n";
Data1=Data1+astro+"\n";
System.out.println("name"+date+" score"+astro);
}while(c.moveToNext());
}
if(Data.equals(dob_var)){
RelativeLayout rel_astro=(RelativeLayout)findViewById(R.id.rel_astro);
TextView tv_astro=(TextView)findViewById(R.id.tv_astro);
tv_astro.setText(Data);

setContentView(rel_astro);
}
else{
db_sunshine.execSQL("INSERT INTO "+TableName+"(d_o_b,astro_quote)"+" VALUES ('"+dob_var+"','"+quote+"');");
Toast customToast = new Toast(getBaseContext());
customToast = Toast.makeText(getBaseContext(), "Your quote is submit", Toast.LENGTH_SHORT);
customToast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
customToast.show();

Intent newIntent = new Intent(Astro_talk.this,selectoption.class);

startActivity(newIntent);
finishActivity(0);

}


}catch(Exception ex)
{
Log.e("ERROR","ERROR" ,ex);
}
finally
{
if(db_sunshine!=null)
db_sunshine.close();
}
}
});
}

}

请告诉我哪里做错了。

最佳答案

 Cursor c=db_sunshine.rawQuery("SELECT astro_quote FROM "+ TableName +"WHERE d_o_b='"+ date_var +"'" , null);
System.out.println("query executed");
int column1=c.getColumnIndex("d_o_b");
int column2=c.getColumnIndex("astro_quote");
c.moveToFirst();
if(c!=null)
{

{
String date=c.getString(column1);
String astro =c.getString(column2);
Data=Data+date+"\n";
Data1=Data1+astro+"\n";
System.out.println("name"+date+" score"+astro);
}while(c.moveToNext());
}

这里您正在查询一列,但试图获取两列你只能得到一个日期,即 int column1=c.getColumnIndex("astro_quote");

字符串 astro =c.getString(column1);

关于java - 如果日期不在 Android 的该表中,如何将日期插入用户输入的表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10069051/

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