在数据库

时间:2015-05-11 06:32:27

标签: android sqlite

请修复错误我试图在数据库中显示textview中的数据 数据库已创建,但未在textview中显示数据 和代码数据库文件为...

 package com.example.databasetest;


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;
import android.provider.BaseColumns;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper{

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "teacher";
    private static final String TABLE_NAME = "teacher_table";

    private static final String NAME = "teacher_name";
    private static final String FATHER_NAME = "father_name";
    private static final String MOTHER_NAME = "mother_name";

    SQLiteDatabase data=this.getWritableDatabase();

    Context ctx;



    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        ctx=context;
        Log.d("DATABASE OPERATION", "DATABASE CREATED");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        data=db;
          db.execSQL("CREATE TABLE " + TABLE_NAME + " ("
                    + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + NAME + " TEXT,"
                    + FATHER_NAME + " TEXT,"
                    + MOTHER_NAME + " TEXT"
                    + ");");
        Log.d("DATABASE OPERATION", "TABLE CREATED");


    }
     public void open() throws SQLException
     {
         DBHelper db1 = new DBHelper(ctx);
            data = db1.getWritableDatabase();
     }

     public void close()
     {
         data.close();
     }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXCEPTION EXISTS");
        onCreate(db);       
    }
    public void onInsert(DBHelper db,String name,String f_name, String m_name)
    {
        SQLiteDatabase sql= db.getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put("teacher_name",name);
        cv.put("father_name", f_name);
        cv.put("mother_name", m_name);
        sql.insert(TABLE_NAME, null, cv);
        Log.d("DATABASE OPERATION", "ONE ROW INSERTED.....");
    }

    public Cursor getInformation(DBHelper dop)
    {
        SQLiteDatabase SQ=dop.getReadableDatabase();
        String[] coloumns={NAME,FATHER_NAME,MOTHER_NAME};
        Cursor CR=SQ.query(TABLE_NAME, coloumns, null, null, null, null, null);
        return CR;

    }

}

java活动为......

package com.example.databasetest;



import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TextView;

public class ShowDetail extends Activity{

    SQLiteDatabase db;
    TextView NAME,FATHERNAME,MOTHERNAME;
    String name,fathername,mothername;

    Context ctxx=this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_data);
        NAME=(TextView)findViewById(R.id.name);
        FATHERNAME=(TextView)findViewById(R.id.father_name);
        MOTHERNAME=(TextView)findViewById(R.id.mother_name);


        DBHelper DB= new DBHelper(ctxx);
        DB.open();
        Cursor CR=DB.getInformation(DB);

        if (CR.moveToFirst()) {
         do {   

             name=CR.getString(0);
             fathername=CR.getString(1);     
             mothername=CR.getString(2);

          NAME.setText(name);
          FATHERNAME.setText(fathername);
          MOTHERNAME.setText(mothername);

         } while (CR.moveToNext());
      }
      if (CR != null && !CR.isClosed()) {
         CR.close();
      }
      if(DB!=null)
      {
          DB.close();
      }

    }
}

运行此代码后,我发现错误,所以我将活动代码更改为

DBHelper DB= new DBHelper(ctxx);
        DB.open();
        Cursor CR=DB.getInformation(DB);

        if (CR.moveToFirst()) {
         do {   

             name=CR.getString(1);
             fathername=CR.getString(2);     
             mothername=CR.getString(3);

          NAME.setText(name);
          FATHERNAME.setText(fathername);
          MOTHERNAME.setText(mothername);

         } while (CR.moveToNext());
      }
      if (CR != null && !CR.isClosed()) {
         CR.close();
      }
      if(DB!=null)
      {
          DB.close();
      }

    }
}

显示运行时错误为...

    05-11 02:30:06.273: D/DATABASE OPERATION(894): DATABASE CREATED
05-11 02:30:06.333: D/DATABASE OPERATION(894): DATABASE CREATED
05-11 02:30:06.373: D/DATABASE OPERATION(894): ONE ROW INSERTED.....
05-11 02:30:06.683: I/Choreographer(894): Skipped 42 frames!  The application may be doing too much work on its main thread.
05-11 02:30:07.373: D/DATABASE OPERATION(894): DATABASE CREATED
05-11 02:30:07.623: D/DATABASE OPERATION(894): DATABASE CREATED
05-11 02:30:07.723: E/CursorWindow(894): Failed to read row 0, column 3 from a CursorWindow which has 21 rows, 3 columns.
05-11 02:30:07.723: E/CursorWindow(894): Failed to read row 0, column 3 from a CursorWindow which has 21 rows, 3 columns.
05-11 02:30:07.984: D/dalvikvm(894): GC_FOR_ALLOC freed 154K, 9% free 2865K/3136K, paused 136ms, total 213ms
05-11 02:30:07.993: D/AndroidRuntime(894): Shutting down VM
05-11 02:30:07.993: W/dalvikvm(894): threadid=1: thread exiting with uncaught exception (group=0x41465700)
05-11 02:30:08.113: E/AndroidRuntime(894): FATAL EXCEPTION: main
05-11 02:30:08.113: E/AndroidRuntime(894): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.databasetest/com.example.databasetest.ShowDetail}: java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
05-11 02:30:08.113: E/AndroidRuntime(894):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
05-11 02:30:08.113: E/AndroidRuntime(894):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
05-11 02:30:08.113: E/AndroidRuntime(894):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-11 02:30:08.113: E/AndroidRuntime(894):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
05-11 02:30:08.113: E/AndroidRuntime(894):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-11 02:30:08.113: E/AndroidRuntime(894):  at android.os.Looper.loop(Looper.java:137)
05-11 02:30:08.113: E/AndroidRuntime(894):  at android.app.ActivityThread.main(ActivityThread.java:5103)
05-11 02:30:08.113: E/AndroidRuntime(894):  at java.lang.reflect.Method.invokeNative(Native Method)
05-11 02:30:08.113: E/AndroidRuntime(894):  at java.lang.reflect.Method.invoke(Method.java:525)
05-11 02:30:08.113: E/AndroidRuntime(894):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-11 02:30:08.113: E/AndroidRuntime(894):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-11 02:30:08.113: E/AndroidRuntime(894):  at dalvik.system.NativeStart.main(Native Method)
05-11 02:30:08.113: E/AndroidRuntime(894): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
05-11 02:30:08.113: E/AndroidRuntime(894):  at android.database.CursorWindow.nativeGetString(Native Method)
05-11 02:30:08.113: E/AndroidRuntime(894):  at android.database.CursorWindow.getString(CursorWindow.java:434)
05-11 02:30:08.113: E/AndroidRuntime(894):  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
05-11 02:30:08.113: E/AndroidRuntime(894):  at com.example.databasetest.ShowDetail.onCreate(ShowDetail.java:38)
05-11 02:30:08.113: E/AndroidRuntime(894):  at android.app.Activity.performCreate(Activity.java:5133)
05-11 02:30:08.113: E/AndroidRuntime(894):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-11 02:30:08.113: E/AndroidRuntime(894):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
05-11 02:30:08.113: E/AndroidRuntime(894):  ... 11 more

1 个答案:

答案 0 :(得分:0)

列索引出错。为避免这种情况,您应首先使用getColumnIndex(NAME_OF_FIELD)函数获取列索引,然后您可以使用正确的索引访问数据。

int nameFieldColumnIndex = CR.getColumnIndex("teacher_name");
name= CR.getString(nameFieldColumnIndex);