Android数据库查询无法正常工作

Android数据库查询无法正常工作,android,database,sqlite,Android,Database,Sqlite,我建立了一个数据库,里面有这样的信息 我正试图用这个从数据库中获取组名 public String readNameFromPosition(int position) { Cursor c; c = myDataBase.query("hikingGroups", new String[] { "groupName" }, "position=\'" + position + "\'", null, null, null, null); if

我建立了一个数据库,里面有这样的信息

我正试图用这个从数据库中获取组名

public String readNameFromPosition(int position) {
    Cursor c;
    c = myDataBase.query("hikingGroups", new String[] { "groupName" },
            "position=\'" + position + "\'", null, null, null, null);
    if (c.moveToFirst()) {
        return c.getString(0);
    } else {
        return "" + c.getCount();
    }
}
当position=0时,正确检索groupName,但只要该位置等于任何其他有效数字,c.getCount()就会返回“0”。有什么帮助吗?我可以发布更多的代码,如果需要的话,只要问。readNameFromPosition()也在我的DataBaseHelper类中

更新:我的代码

package bry.Bicker.OjaiHiking;
import java.io.IOException;
import android.app.Activity;
import android.database.SQLException;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.TextView;
import android.widget.Toast;

public class OjaiHikingActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final DataBaseHelper theDb = new DataBaseHelper(getApplicationContext());
    try {

        theDb.createDataBase();

    } catch (IOException ioe) {

        throw new Error("Unable to create database");

    }

    try {

        theDb.openDataBase();

    } catch (SQLException sqle) {

        throw sqle;

    }
    final Gallery hikesGallery = (Gallery) findViewById(R.id.gallery1);
    hikesGallery.setAdapter(new ImageAdapter(this));
    hikesGallery.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            String groupName = "";
            int position = hikesGallery.getSelectedItemPosition();
            Toast.makeText(getApplicationContext(), "" + position,
                    Toast.LENGTH_SHORT).show();
            groupName = theDb.readNameFromPosition(position);
            TextView tV = (TextView) findViewById(R.id.groupName);
            Typeface font = Typeface.createFromAsset(getAssets(),
                    "arialbd.ttf");
            tV.setTypeface(font);
            tV.setText(groupName);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }

    });
    hikesGallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position,
                long id) {

        }
    });
}
}
这样试试


尝试这种方式

您确定没有使用应用程序之外的值(例如在SQLite管理器中)更新数据库,然后运行应用程序,假设新的值将在新的运行中出现吗?您的数据库将不会使用新值重新构建,因为它在设备/仿真器上


尝试重新安装应用程序,然后再次安装(猜测是您第一次启动数据库时只有一个值(对于位置
0
)。

您确定没有使用应用程序之外的值更新数据库(例如在SQLite管理器中)然后运行应用程序,假设新值将出现在新运行中?您的数据库将不会使用新值重新构建,因为它在设备/仿真器上


请尝试重新安装应用程序,然后再次安装(猜测是您第一次启动数据库时仅使用一个值(对于位置
0
)。

我认为DB查询没有返回任何结果。 c、 moveToFirst()必须返回false,因此不应执行c.getString(…)

更好的处理方法是

if (c.moveToFirst()) {
    return c.getString(c.getColumnIndex(KEY_GROPUNAME));
} else {
    return "" + c.getCount();
}

我认为DB查询没有返回任何结果。 c、 moveToFirst()必须返回false,因此不应执行c.getString(…)

更好的处理方法是

if (c.moveToFirst()) {
    return c.getString(c.getColumnIndex(KEY_GROPUNAME));
} else {
    return "" + c.getCount();
}


在执行查询之前是否打开了数据库?我想是的。我将发布我的全部代码,因为到目前为止似乎没有任何帮助。您是否在执行查询之前打开了数据库?我相信是的。我将发布我的全部代码,因为到目前为止似乎没有任何帮助。Android崩溃了。Logcat:android.database.CursorIndexOutOfBoundsException:请求索引0,大小为0,因此在我看来,查询仍然没有检索数据。@BrianEcker已将光标行中的ourDatabase替换为urclass SQLiteDatabase ourDatabase中定义的urdatabase名称。我还编辑了我的答案检查,并让informI将“ourDatabase”替换为我自己的数据库名称。仍然会得到相同的LogCat错误:“android.database.CursorIndexOutOfBoundsException:请求索引0,大小为0”android崩溃。Logcat:android.database.CursorIndexOutOfBoundsException:请求索引0,大小为0,因此在我看来,查询仍然没有检索数据。@BrianEcker已将光标行中的ourDatabase替换为urclass SQLiteDatabase ourDatabase中定义的urdatabase名称。我还编辑了我的答案检查,并让informI将“ourDatabase”替换为我自己的数据库名称。仍然得到相同的LogCat错误:“android.database.CursorIndexOutOfBoundsException:请求索引0,大小为0”啊,嗯,你说得对。我确实是先用一个值开始的。我删除了我的模拟器,并制作了一个新的。现在它开始工作了:D有没有其他方法可以在不删除和重新创建emulator的情况下重新安装应用程序?我以为每次程序运行时都会重新安装它,但我想我错了。@BrianEcker您不必删除并重新创建模拟器,与任何应用程序一样,您将进入
设置
->
应用程序
->
管理应用程序
->
您的应用程序
,然后在那里
清除数据
卸载
。你不必关闭模拟器或其他什么东西。好的,这更有意义。谢谢你帮我解决这个问题:D@BrianEcker我很高兴能帮上忙。另外,建议您使用类似这样的查询,而不是在where子句中插入值:
,“position=?”,新字符串[]{“+position},null,null,null)。好的,我将切换到那个。问号符号一开始让我有点困惑。嗯,你说得对。我确实是先用一个值开始的。我删除了我的模拟器,并制作了一个新的。现在它开始工作了:D有没有其他方法可以在不删除和重新创建emulator的情况下重新安装应用程序?我以为每次程序运行时都会重新安装它,但我想我错了。@BrianEcker您不必删除并重新创建模拟器,与任何应用程序一样,您将进入
设置
->
应用程序
->
管理应用程序
->
您的应用程序
,然后在那里
清除数据
卸载
。你不必关闭模拟器或其他什么东西。好的,这更有意义。谢谢你帮我解决这个问题:D@BrianEcker我很高兴能帮上忙。另外,建议您使用类似这样的查询,而不是在where子句中插入值:
,“position=?”,新字符串[]{“+position},null,null,null)。好的,我将切换到那个。起初,问号符号的事情让我有点困惑。
if (c.moveToFirst()) {
    return c.getString(c.getColumnIndex(KEY_GROPUNAME));
} else {
    return "" + c.getCount();
}