sql-为什么会得到CursorIndexOutOfBoundsException?

问题描述

为什么会得到CursorIndexOutOfBoundsException?

我把全部代码都贴上,cursor好像不对。

package com.tanukiproductions.battleforchristmas;

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 SQLiteTable {

public static final String KEY_ROWID = "_id";
public static final String KEY_LEVEL = "level";
public static final String KEY_HEALTH = "health";
public static final String KEY_NAME = "name";
public static final String KEY_CRIT = "crit";
public static final String KEY_CRIT_RANGE = "crit_range";
public static final String KEY_CRIT_INC = "crit_increment";
public static final String KEY_HIT_RANGE = "hit_range";
public static final String KEY_HIT_INC = "hit_increment";
public static final String KEY_CHAR_IMG = "character_image";
public static final String KEY_TOTAL_XP = "total_xp";
public static final String KEY_XP = "xp";
public static final String KEY_XP_NEEDED = "xp_needed";
public static final String KEY_COINS = "coins";
public static final String KEY_SMALL_POTS = "small_pots";
public static final String KEY_LARGE_POTS = "large_pots";

private static DbHelper ourHelper;
private final Context ourContext;
private static SQLiteDatabase ourDatabase;

private static final String DB_NAME = "battle_for_christams";
private static final String DB_TBL = "stats";
private static final int DATABASE_VERSION = 1;

private static class DbHelper extends SQLiteOpenHelper{

    public DbHelper(Context context) {
        super(context, DB_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + DB_TBL + " (" +
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_LEVEL + " INTEGER NOT NULL, " +
                KEY_HEALTH + " INTEGER NOT NULL, " +
                KEY_NAME + " TEXT NOT NULL, " +
                KEY_CRIT + " INTEGER NOT NULL, " +
                KEY_CRIT_RANGE + " INTEGER NOT NULL, " +
                KEY_CRIT_INC + " INTEGER NOT NULL, " +
                KEY_HIT_RANGE + " INTEGER NOT NULL, " +
                KEY_HIT_INC + " INTEGER NOT NULL, " +
                KEY_CHAR_IMG + " INTEGER NOT NULL, " +
                KEY_TOTAL_XP + " INTEGER NOT NULL, " +
                KEY_XP + " INTEGER NOT NULL, " +
                KEY_XP_NEEDED + " INTEGER NOT NULL, " +
                KEY_COINS + " INTEGER NOT NULL, " +
                KEY_SMALL_POTS + " INTEGER NOT NULL, " +
                KEY_LARGE_POTS + " INTEGER NOT NULL)"
            );

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, "User1");
        values.put( KEY_LEVEL, 1);
        values.put(KEY_HEALTH, 10);
        values.put(KEY_CRIT, 5);
        values.put(KEY_CRIT_RANGE, 2);
        values.put(KEY_CRIT_INC, 2);
        values.put(KEY_HIT_RANGE, 2);
        values.put(KEY_HIT_INC, 0);
        values.put(KEY_TOTAL_XP, 0);
        values.put(KEY_XP, 0);
        values.put(KEY_XP_NEEDED, 5);
        values.put(KEY_COINS, 5);
        values.put(KEY_SMALL_POTS, 0);
        values.put(KEY_LARGE_POTS, 0);
        values.put(KEY_CHAR_IMG, 1);
        db.insert(DB_TBL,  null, values);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXIST " + DB_TBL);
        onCreate(db);
    }

}

public SQLiteTable(Context c){
    ourContext = c;
    ourHelper = new DbHelper(ourContext);
}

public static void open() throws SQLException{
    ourDatabase = ourHelper.getWritableDatabase();
}

public static void close() throws SQLException{
    ourHelper.close();
}

public int getLevel() {
    open();
    Cursor cursor = ourDatabase.query(DB_TBL, new String[] {KEY_LEVEL}, null, null, null, null, null);
    int level = cursor.getInt(0);
    cursor.close();
    close();
    return level;
}

public int getHealth() {
    open();
    Cursor cursor = ourDatabase.query(DB_TBL, new String[] {KEY_HEALTH}, null, null, null, null, null);
    int health = cursor.getInt(0);
    cursor.close();
    close();
    return health;
}

public int getCrit() {
    // TODO Auto-generated method stub
    open();
    Cursor cursor = ourDatabase.query(DB_TBL, new String[] {KEY_CRIT}, null, null, null, null, null);
    int crit = cursor.getInt(0);
    cursor.close();
    close();
    return crit;
}

public int getCritRange() {
    // TODO Auto-generated method stub
    open();
    Cursor cursor = ourDatabase.query(DB_TBL, new String[] {KEY_CRIT_RANGE}, null, null, null, null, null);
    int critRange = cursor.getInt(0);
    cursor.close();
    close();
    return critRange;
}

public int getCritInc() {
    // TODO Auto-generated method stub
    open();
    Cursor cursor = ourDatabase.query(DB_TBL, new String[] {KEY_CRIT_INC}, null, null, null, null, null);
    int critInc = cursor.getInt(0);
    cursor.close();
    close();
    return critInc;
}

public int getHitInc() {
    // TODO Auto-generated method stub
    open();
    Cursor cursor = ourDatabase.query(DB_TBL, new String[] {KEY_HIT_INC}, null, null, null, null, null);
    int hitInc = cursor.getInt(0);
    cursor.close();
    close();
    return hitInc;
}

public int getHitRange() {
    // TODO Auto-generated method stub
    open();
    Cursor cursor = ourDatabase.query(DB_TBL, new String[] {KEY_HIT_RANGE}, null, null, null, null, null);
    int hitRange = cursor.getInt(0);
    cursor.close();
    close();
    return hitRange;
}

public int getXp() {
    open();
    Cursor cursor = ourDatabase.query(DB_TBL, new String[] {KEY_XP}, null, null, null, null, null);
    int xp = cursor.getInt(0);
    cursor.close();
    close();
    return xp;
}

public void setXp(int xp){
    open();
    ContentValues values = new ContentValues();
    int xpNeeded = getXpNeeded();
    int XP = getXp() + xp;
    int totalXp = getTotalXp() + xp;
    if ( XP >= xpNeeded ){
        XP = XP - xpNeeded;
        levelUp();
    }
    values.put(KEY_XP, XP);
    values.put(KEY_TOTAL_XP, totalXp);
    ourDatabase.update(DB_TBL, values, null, null);
    close();

}

public int getXpNeeded() {
    open();
    Cursor cursor = ourDatabase.query(DB_TBL, new String[] {KEY_XP_NEEDED}, null, null, null, null, null);
    int xpNeeded = cursor.getInt(0);
    cursor.close();
    close();
    return xpNeeded;
}

public int getTotalXp() {
    open();
    Cursor cursor = ourDatabase.query(DB_TBL, new String[] {KEY_TOTAL_XP}, null, null, null, null, null);
    int totalXp = cursor.getInt(0);
    cursor.close();
    close();
    return totalXp;
}

public int getCoins() {
    open();
    Cursor cursor = ourDatabase.query(DB_TBL, new String[] {KEY_COINS}, null, null, null, null, null);
    int coins = cursor.getInt(0);
    cursor.close();
    close();
    return coins;
}

public String getName() {
    // TODO Auto-generated method stub
    open();
    Cursor cursor = ourDatabase.query(DB_TBL, new String[] {KEY_NAME}, null, null, null, null, null);
    String name = cursor.getString(0);
    cursor.close();
    close();
    return name;
}

public void levelUp() {
    // TODO Auto-generated method stub
    ContentValues values = new ContentValues();
    int level = getLevel() + 1;
    int health = getHealth() + 5;
    double crit = getCrit() + 0.1;
    int xpNeeded = getXpNeeded() + 5;
    int critInc = getCritInc() + 1;
    int hitInc;
    int hitRange;
    int critRange;
    values.put(KEY_LEVEL, level);
    values.put(KEY_HEALTH, health);
    values.put(KEY_CRIT, crit);
    values.put(KEY_XP_NEEDED, xpNeeded);
    values.put(KEY_CRIT_INC, critInc);
    if(level % 2 == 0){
        hitInc = getHitInc() + 1;
        values.put(KEY_HIT_INC, hitInc);
    }
    if(level % 4 == 0){
        hitRange = getHitRange() + 1;
        critRange = getCritRange() + 1;
        values.put(KEY_HIT_RANGE, hitRange);
        values.put(KEY_CRIT_RANGE, critRange);
    }
    ourDatabase.update(DB_TBL, values, null, null );
}

private int getLargePots() {
    // TODO Auto-generated method stub
    open();
    Cursor cursor = ourDatabase.query(DB_TBL, new String[] {KEY_LARGE_POTS}, null, null, null, null, null);
    int largePots = cursor.getInt(0);
    cursor.close();
    close();
    return largePots;
}

private int getSmallPots() {
    // TODO Auto-generated method stub
    open();
    Cursor cursor = ourDatabase.query(DB_TBL, new String[] {KEY_SMALL_POTS}, null, null, null, null, null);
    int smallPots = cursor.getInt(0);
    cursor.close();
    close();
    return smallPots;
}

public void updateSmallPots() {
    // TODO Auto-generated method stub
    ContentValues values = new ContentValues();
    open();
    int smallPots = getSmallPots() + 1;
    int coins = getCoins() - 1;
    values.put(KEY_SMALL_POTS, smallPots);
    values.put(KEY_COINS, coins);
    ourDatabase.update(DB_TBL, values, null, null);
    close();
}

public void updateLargePots(){
    ContentValues values = new ContentValues();
    open();
    int largePots = getLargePots() + 1;
    int coins = getCoins() - 2;
    values.put(KEY_LARGE_POTS, largePots);
    values.put(KEY_COINS, coins);
    ourDatabase.update(DB_TBL, values, null, null);
    close();
}
}

然后运行获得错误:

01-04 16:12:25.596: E/AndroidRuntime(29517): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
01-04 16:12:25.596: E/AndroidRuntime(29517):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
01-04 16:12:25.596: E/AndroidRuntime(29517):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)

解决方案

代码真长啊,

android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1

你忘了调用cursor.moveToFirst(),还需要验证Cursor是否为空:

// Returns false if the Cursor cannot move to this position, i.e. empty
if(cursor.moveToFirst()) {
    // Read data from the first row
}

解决方案二:

出现这种问题一般是你在执行查询的过程中没有将cursor的指针放到第一个位置,默认查询结束后位置是在-1的位置。

时间: 2024-08-02 17:18:37

sql-为什么会得到CursorIndexOutOfBoundsException?的相关文章

自己动手做一个SQL解释器

自己动手做一个SQL解释器在一些小型的应用中,完全没有必要使用大型数据库软件.自己做一个SQL解释器就能用数据库的方式来管理了.这个解释器,能解释常用的SQL命令.你可以自行添加其他功能. <?phpclass DB_text {  var $conn;  var $classname = "db_text";  var $database;  function on_create() {  }  function connect($database_name) {    $th

sql2008-求一个C# SQL做的旅游信息管理系统,不要网页的那种。

问题描述 求一个C# SQL做的旅游信息管理系统,不要网页的那种. 实现简单的景点查询,添加 删除:景点的介绍,修改,以及 管理员及用户个人信息的注册等. 解决方案 http://download.csdn.net/detail/vaeigffsgdfgfdfs/5646183 解决方案二: http://download.csdn.net/detail/wangyindiwyd/3621599 解决方案三: http://download.csdn.net/download/u01049142

sql-关于单表SQL存储过程查询。分页条数,当前页码,总条数的问题

问题描述 关于单表SQL存储过程查询.分页条数,当前页码,总条数的问题 我实现PUB_AREA 表的数据查询, 上面是我写的调试, 指出在下面and处报错,无布尔类型,不知道到底哪里错了- - declare @TotalNum intexec PUB_AREA_SelectAREACITYByHsfSearchss ''101@TotalNum outputselect @TotalNumalter PROCEDURE [dbo].[PUB_AREA_SelectAREACITYByHsfSe

如何用asp把sql server數據轉化為execl文件

server 1.ASP文件: <%@ LANGUAGE="VBSCRIPT" %><%option explicit%><%'EXAMPLE AS:把数据库中一个每天24小时在线人数放到一个EXCEL文件中去'AUTHOR :钢铁工人'EMAIL :hello_hhb@21cn.com'DATE :2001-3-25'TEST :在NT4,SP6,SQL SERVER 7.0,EXCEL2000中测试通过%><HTML><HEAD

在ASP中使用SQL语句之9:表单操作

语句 从某个页面表单中取出信息是ASP编程中常见的问题.但是,遍历通过表单传递的记录会花去多长时间呢?这取决于数据库的大小.简单的GUI界面都可能令循环遍历操作耗费太多的时间. 比方说,假设有个团队成员登录到GUI屏幕输入自己的名字姓氏和名字之间用点号连接:amy.cowen.这个值通过表单提交,她的当前项目列表就从数据库中取了出来并显示在屏幕上.为了快速地取出用户的记录以便显示在屏幕上,你可以编写以下代码. 假设HTML页面上包含以下代码:<FORM ACTION="login_post

.NET和SQL Server中“空值”辨析

server 初学数据库编程我们可能会有一些对"空值"的疑问,比如通过编程新建的一个表中所有数据皆显示为<NULL>,手动添加并删除文字后又变成了空白:一个字符串类型的字段,明明没有填值,却不等于"":用ADO.NET从数据库中取值,每遇到有<NULL>的就出错--这需要我们正确认识.NET和SQL Server中几种不同的"空值".1.真正的空值,也就是"没有输入的值",可以出现在大多数类型的字段中(

SQL Server 2008安装的时提示“重启计算机失败”怎么办?

详细出错信息如下: RebootRequiredCheck 检查是否需要挂起计算机重新启动.挂起重新启动会导致安装程序失败. 失败 需要重新启动计算机.必须重新启动计算机才能安装 SQL Server 出问题后我在网上找的解决方法如下: a .重启机器,再进行安装,如果发现还有该错误,请按下面步骤b.在开始->运行中输入regeditc.到HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\d.在右边窗口右击Pe

SQL连接查询1 内联接查询

在数据库开发方面,通过单表所表现的实现,有时候需要组合查询来找到我们需要的记录集,这时候我们就会用到连接查询. 连接查询主要包括以下几个方面: 内连接 内连接一般是我们最常使用的,也叫自然连接,是用比较运算符比较要联接列的值的联接.它是通过(INNER JOIN或者JOIN)关键字把多表进行连接.我们通过建立两个表来说明问题: StudentID StudentName StudentAge----------- -----------------------------------------

【PL/SQL】TOM 的 show_space()

只适用于非ASSM: create or replace procedure show_space ( p_segname in varchar2, p_owner in varchar2 default user, p_type in varchar2 default 'TABLE' ) as l_free_blks number; l_total_blocks number; l_total_bytes number; l_unused_blocks number; l_unused_byt

h2 删数据 sql优化-h2数据库删除数据速度问题

问题描述 h2数据库删除数据速度问题 想删除h2数据库中某个表部分数据,但该表中有八千万左右数据,如何删除符合要求的一小部分数据呢?比如删除name以abc开头的数据,因为h2数据库我是通过web打开查看的,普通的Sql语句要执行很长很长时间,而且经常报内存不足,各位大神有没有什么优化的方法???求指点呀 解决方案 http://www.lc365.net/blog/b/32424/ 解决方案二: 因为没分了,不过谢谢能回答,对我其他的一些地方有帮助