无法从SQlite数据库中获取数据

时间:2017-07-31 09:53:15

标签: android sqlite crud android-cursor

我是Android的SQLite数据库新手我想从数据库中获取特定数据,但我无法做到这一点它给了我以下错误,我添加了_id列但是它仍然提供相同的例外,这可能是我如何解决这个问题的原因?有什么我想念的吗?

java.lang.IllegalArgumentException: column '_id' does not exist at 
android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:333)
                                                                        at android.support.v4.widget.CursorAdapter.init(CursorAdapter.java:186)
                                                                        at android.support.v4.widget.CursorAdapter.<init>(CursorAdapter.java:163)
                                                                        at android.support.v4.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:99)
                                                                        at android.support.v4.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:94)
                                                                        at com.iz.PG.SummaryFragment.onCreateView(SummaryFragment.java:57)
                                                                        at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
                                                                        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
                                                                        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                                                                        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
                                                                        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
                                                                        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
                                                                        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
                                                                        at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
                                                                        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
                                                                        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
                                                                        at android.os.Handler.handleCallback(Handler.java:815)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                        at android.os.Looper.loop(Looper.java:207)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5769)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

我的代码如下 - DBHelper.java

    public class DBHelper extends SQLiteOpenHelper {

    // Table Name
    public static final String TABLE_NAME = "detailinfo";

    // Table columns
    public static final String id = "_id";
    public static final String param1 = "param1";
    public static final String param2 = "param2";
    public static final String param3 = "param3";
    public static final String param4 = "param4";

    // Database Information
    static final String DB_NAME = "test.db";

    // database version
    static final int DB_VERSION = 1;

    // create query
    private static final String CREATE_TABLE = "create table " + TABLE_NAME + "( "+id+" INTEGER PRIMARY KEY AUTOINCREMENT," + param1+ " TEXT, " + param2 + " TEXT,"+param3+" TEXT,"+param4+" TEXT);";

    public DBHelper(Context context) {
        super(context,DB_NAME ,null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String query = "drop table if exists"+TABLE_NAME;
        db.execSQL(query);
        onCreate(db);
    }
}

DBManager.java

    public class DBManager {

    private static final String TAG = "DBManager";

    private DBHelper dbHelper;
    private Context context;
    private SQLiteDatabase database;

    public DBManager(Context c) {
        context = c;
    }

    public DBManager open() throws SQLException {
        dbHelper = new TransactionDBHelper(context);
        database = dbHelper.getWritableDatabase();
        return this;
    }

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

    public void insert(TxnSummary txnSummary) {

        ContentValues contentValue = new ContentValues();        
        contentValue.put(DBHelper.param1, "1234");
        contentValue.put(DBHelper.param2, "test");
        contentValue.put(DBHelper.param3, "testdata1");
        contentValue.put(DBHelper.param4,"testdata2");

        long count = database.insert(DBHelper.TABLE_NAME, null, contentValue);
        Log.d(TAG,"Data is inserted in database count is : "+count);
    }

    public Cursor fetch() {

        String query = "SELECT "+DBHelper.param1+","
                                +DBHelper.param2+","
                                +DBHelper.param3+","
                                +DBHelper.param4
                                +" FROM "+DBHelper.TABLE_NAME;
        Cursor cursor = database.rawQuery(query,null);
        if (cursor != null) {
            cursor.moveToFirst();
        }
        return cursor;
    }
}

SummaryFragment.java

    public class SumarryFragment extends Fragment {

      final String[] from = new String[] {DBHelper.param1, Helper.param2,
DBHelper.param3,DBHelper.param4};
      final int[] to = new int[] { R.id.textViewtxnList1,
                                        R.id.textViewtxnList2,
                                            R.id.textViewtxnList3,
                                                R.id.textViewtxnList4 };

      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            View view = inflater.inflate(R.layout.fragment_txnsumarry, container, false);

            context = getContext();

            ListView txnListView = (ListView) view.findViewById(R.id.summarylistview);        

            DBManager dbManager = new DBManager(getActivity());
            dbManager.open();
            cursor = dbManager.fetch();
            simpleCursorAdapter = new SimpleCursorAdapter(getActivity(),R.layout.fragment_summary,cursor,from,to,0);
            simpleCursorAdapter.notifyDataSetChanged();
            txnListView.setAdapter(simpleCursorAdapter);

            return view;
        }
    }

我的观点是

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/textViewList1"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dp"
            android:text="PARAM1" />
        <TextView
            android:id="@+id/textViewList2"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="PARAM2" />
        <TextView
            android:id="@+id/textViewList3"
            android:layout_width="75dp"
            android:layout_height="wrap_content"
            android:text="PARAM3" />
        <TextView
            android:id="@+id/textViewList4"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="PARAM4" />
    </LinearLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

您必须在2中更改数据库版本,或者只是删除并创建新数据库。

答案 1 :(得分:0)

首先,您必须确保数据库实际包含该列(增加数据库版本)。

然后,您还必须确保查询实际返回您要读取的所有列:

    String query = "SELECT "+DBHelper.param1+","
                            +DBHelper.param2+","
                            +DBHelper.param3+","
                            +DBHelper.param4
                            +" FROM "+DBHelper.TABLE_NAME;