0

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

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();
                    }
            }
        });
    }

}

请告诉我我在哪里做错了。

4

2 回答 2

1

尝试使用:

 Cursor c=db_sunshine.rawQuery("SELECT astro_quote FROM "+ TableName +" WHERE d_o_b="+ date_var, null);

我不确定,这些是唯一的问题,但这可能是因为您没有在查询中的 WHERE 之前添加空格字符(我已经在上面添加了一个)。我认为您的 d_o_b 是数字,你不不需要在查询中的 date_var 前后使用“'”。

编辑 :

从您的代码看来,您正在尝试在创建表后立即访问它。因此表中没有数据,因此,您将获得空游标。当您尝试从该游标访问数据时,您会遇到异常。

首先用数据填充表格。然后尝试获取它。

所以基本上你需要做的是-

 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))");

 // call a function to add data to table ( you will have to add data here to the table created just now )

 Cursor c=db_sunshine.rawQuery("SELECT astro_quote FROM "+ TableName +" WHERE d_o_b="+ date_var, null);
 if(c.getCount()>0)
 {
    // cursor contains data ( which can never be a option for your case )
 }             
 else
 {
   // empty cursor
 }
于 2012-04-09T07:39:39.240 回答
1
 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);

于 2012-04-09T05:13:49.957 回答