从android中的数据库SQLite中重新获取数据时出错

时间:2015-05-12 13:16:58

标签: android sqlite

我的数据库帮助程序类是

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataHandlerDairy { 

public static final String DATE = "date";
public static final String DAIRY = "dairy";
private static final String DATA_BASE_NAME = "mydairy";
private static final String TABLE_NAME = "table_dairy";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_CREATE="create table table_dairy (date text primary key," + "dairy text not null);";

DataBaseHelper1 dbhelper1;
Context ct;
SQLiteDatabase db;

public DataHandlerDairy(Context ct)
{
    this.ct=ct;
    dbhelper1 = new DataBaseHelper1(ct);

}

private static class DataBaseHelper1 extends SQLiteOpenHelper
{

public DataBaseHelper1(Context ct)
{
    super(ct,DATA_BASE_NAME,null, DATABASE_VERSION);
}


    @Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    try
    {
        db.execSQL(TABLE_CREATE);
    }
    catch(SQLException e)
    {
        e.printStackTrace();
    }
    }



@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS table_dairy ");
    onCreate(db);
}

}

public DataHandlerDairy open()
{
db = dbhelper1.getWritableDatabase();
return this;
}

public void close()
{
    dbhelper1.close();
}

public long insertData(String date,String dairy)
{
    ContentValues content = new ContentValues();
    content.put(DATE, date);
    content.put(DAIRY, dairy);
    return db.insertOrThrow(TABLE_NAME, null, content);
}

public Cursor returnData(String date1) throws SQLException
{
    Cursor c = db.query(true, TABLE_NAME, new String[] { DATE, DAIRY}, DATE + "=" + date1, null, null, null, null, null);
    if(c!=null)
    {
        c.moveToFirst();
    }
    return c;
}



}

当我尝试从数据库中检索数据时,我收到错误。 我在以下代码中使用了数据库助手类。

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor; 
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class Read_Dairy extends Activity {


String date1;
TextView tv1,tv2;
ImageButton imgb1;
Button bt1;
DataHandlerDairy handler3;


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



    tv1=(TextView) findViewById(R.id.readme);
    imgb1=(ImageButton) findViewById(R.id.tick);
    bt1=(Button) findViewById(R.id.ready);
    tv2=(TextView) findViewById(R.id.love);

    Bundle bundle = getIntent().getExtras();
    date1 = bundle.getString("kanna");




    bt1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String getdata=" ";
            tv2.setText(date1);
            String abcd=tv2.getText().toString();

            handler3 = new DataHandlerDairy(getBaseContext());
            handler3.open();
            Cursor c = handler3.returnData(abcd);
            if(c.moveToFirst())
            {
                do
                {
                getdata=c.getString(1);

                }while(c.moveToNext());             
                }
            handler3.close();

            if(getdata==null)
            {
                tv1.setText("no data exists for this date");
            }
            else
            {
            tv1.setText(getdata);
            }

        }

    });


    imgb1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent ks = new Intent(Read_Dairy.this,PickYourDate.class);
            startActivity(ks);

        }
    });

    Typeface font = Typeface.createFromAsset(getAssets(), "Strato-linked.ttf");
    tv1.setTypeface(font);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_read__dairy, menu);
    return true;
}

}

我在使用它时遇到错误,我的StackTrace是

 FATAL EXCEPTION: main
 Process: simple.smile.my_dairy, PID: 4423
 android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
    at simple.smile.my_dairy.Read_Dairy$1.onClick(Read_Dairy.java:71)
    at android.view.View.performClick(View.java:4832)
    at android.view.View$PerformClick.run(View.java:19839)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:211)
    at android.app.ActivityThread.main(ActivityThread.java:5315)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:736)

请告诉我错误的位置以及纠正方法。

1 个答案:

答案 0 :(得分:1)

getdata=c.getString(1)不是一个好方法。而是使用getdata=c.getString(c.getColumnIndex(DataHandlerDairy.DAIRY))