Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java cursor.getstring()在数据库中获取了错误的字段_Java_Android_Database - Fatal编程技术网

Java cursor.getstring()在数据库中获取了错误的字段

Java cursor.getstring()在数据库中获取了错误的字段,java,android,database,Java,Android,Database,这是我的代码: public void onItemClick(AdapterView<?> listView, View view, int position, long id) { Cursor cursor = (Cursor) listView.getItemAtPosition(position); int _id = cursor.getInt(0); String _recipe = cursor.getString(1); Inten

这是我的代码:

public void onItemClick(AdapterView<?> listView, View view, int position, long id)
{
    Cursor cursor = (Cursor) listView.getItemAtPosition(position);

    int _id = cursor.getInt(0);
    String _recipe = cursor.getString(1);
    Intent intent = new Intent(Luzon1Activity.this,RecipeInstruction.class);
    intent.putExtra("id", _id);
    intent.putExtra("recipe", _recipe);
    startActivity(intent);
}
我的问题在这一部分:
String\u recipe=cursor.getString(1)
。 它总是给我错误的数据。我试图改变号码,但它仍然给了我错误的数据

这是我的数据库:

package com.pinoycookbook;

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.util.Log;

public class Dbadapter
{
    public static final String ROWID = "_id";
    public static final String NAME = "foodname";
    public static final String ORIGIN = "origin";

    public static final String RECIPE = "recipe";

    public static final String CATEGORY = "category";

    private static final String TAG = "Dbadapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    private static final String DATABASE_NAME = "PinoyCookbook.sqlite";
    public static final String SQLITE_TABLE = "Food";
    private static final int DATABASE_VERSION = 1;

    private final Context mCtx;

    private static final String DATABASE_CREATE =
        "CREATE TABLE if not exists " + SQLITE_TABLE + " (" +
                ROWID + " integer PRIMARY KEY autoincrement," +
                NAME + " TEXT," +
                RECIPE + " TEXT," +
                ORIGIN + " TEXT," + 
                CATEGORY+ " TEXT,"+
                " UNIQUE (" + ROWID +"));";

    private static class DatabaseHelper extends SQLiteOpenHelper
    {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            Log.w(TAG, DATABASE_CREATE);
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE);
            onCreate(db);
        }
    }

    public Dbadapter(Context ctx) {
        this.mCtx = ctx;
    }

    public Dbadapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        if (mDbHelper != null) {
            mDbHelper.close();
        }
    }

    public long createData(String foodname, String recipe, String origin,  int i) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(NAME, foodname);
        initialValues.put(RECIPE, recipe);
        initialValues.put(ORIGIN, origin);

        initialValues.put(CATEGORY, i);

        return mDb.insert(SQLITE_TABLE, null, initialValues);
    }

    public boolean deleteAllData() {
        int doneDelete = 0;
        doneDelete = mDb.delete(SQLITE_TABLE, null , null);
        Log.w(TAG, Integer.toString(doneDelete));
        return doneDelete > 0;
    }

    public void insertData() {
        createData("Adobong Manok","adobongmanok","Manila",1);
        createData("Lechon","lechon","Cebu",2);
        createData("Crispy Pata","crispypata","Cebu",2);
        createData("Bulalo","bulalo","Batangas",1);
        createData("Taba ng Talangka Rice","talangkarice","Roxas",2);
        createData("Arroz Caldo","arrozcaldo","Roxas",2);
        createData("Sinigang","sinigang","Manila",1);
    }
}

您可以这样尝试:

cursor.getString(cursor.getColumnIndex("recipe"));
它返回正确的索引,结果是正确的值。

因此我建议您使用该方法,而不是硬编码

String _recipe = cursor.getString(cursor.getColumnIndex(Dbadapter.RECIPE));
这将确保您在该字段中始终正确。若它仍然得到错误的数据,那个么问题就在查询中,而不是在
Cursor

注意:使用包含列名的静态字段始终是最佳做法

更新: 我以前尝试过,但它给了我以下错误: java.lang.IllegalArgumentException:列“recipe”不存在

您需要了解实际的表结构。尝试执行以下语句:

PRAGMA table_info(Dbadapter.SQLITE_TABLE);
docs()怎么说:

该杂注为命名表中的每列返回一行。 结果集中的列包括列名、数据类型、是否 该列可以为NULL,也可以为该列的默认值。 结果集中的“pk”列对于不匹配的列为零 是主键的一部分,是主键中列的索引 作为主键一部分的列的键

例子: 在这里,我为您创建了通过
PRAGMA
获取tableinfo的方法:

public String getTableInfo() {
    StringBuilder b = new StringBuilder("");
    Cursor c = null;
    try {
        db = helper.getReadableDatabase();
        String query = "pragma table_info(" + Dbadapter.SQLITE_TABLE + ")";
        c = db.rawQuery(query, null);
        if (c.moveToFirst()) {
            do {
                b.append("Col:" + c.getString(c.getColumnIndex("name")) + " ");                     
                b.append(c.getString(c.getColumnIndex("type")));
                b.append("\n");
            } while (c.moveToNext());
        }
        return b.toString();
    }
    finally {
        if (c != null) {
            c.close();
        }
        if (db != null) {
            db.close();
        }
    }
}
输出将如下所示:

Column: type text
Column: date text
仅出于想象力,我将为您提供屏幕:


我以前尝试过,但它给了我以下错误:java.lang.IllegalArgumentException:列“recipe”没有exist@KempDomalanta这是不可能的,您是在onCreate()方法中创建它的
RECIPE+“TEXT”,
@kempodomalanta尝试使用
PRAGMA table_info(您的tablename)我将把它放在哪里?对不起,我还在学习,对android还是新手。我以前提取过一个数据库,其中包含配方字段。我不知道为什么它会给我那个错误。谢谢你抽出时间。我试试看。
Column: type text
Column: date text