使用游标从sqlite数据库中选择数据

时间:2013-09-28 22:09:29

标签: android sqlite android-cursor

我正在尝试从SQLite数据库中选择数据,但却遇到了麻烦。 首先,我将数据库从assets文件夹加载到data / data / PACKAGE_NAME / databases。它正确加载精细和数据库负载。但是,当我试图用光标读取数据时 - 应用程序停止(这是我的代码,指向我的错误请。谢谢。

public class SQLiteHelper extends SQLiteAssetHelper {

public static final String DB_NAME = "cards";
private static final int DATABASE_VERSION = 1;

public static final String TABLE_CARDS = "cards";

public static final String COLUMN_ID = "_id";
public static final String COLUMN_CARD_NAME = "card_name";
public static final String COLUMN_TYPE = "type";
public static final String COLUMN_RARITY = "rarity";
public static final String COLUMN_CARD_CLASS = "card_class";
public static final String COLUMN_SET = "card_set";
public static final String COLUMN_RACE = "race";
public static final String COLUMN_SKILLS = "skills";
public static final String COLUMN_MANA = "mana";
public static final String COLUMN_ATTACK = "attack";
public static final String COLUMN_HEALTH = "health";
public static final String COLUMN_DAMAGE = "damage";
public static final String COLUMN_DURABILITY = "durability";
public static final String COLUMN_CRAFT = "craft";
public static final String COLUMN_DUST = "dust";
public static final String COLUMN_ARTIST = "artist";
public static final String COLUMN_EXTRA_INFO = "extra_info";
public static final String COLUMN_CARD_SRC = "card_src";


public SQLiteHelper(Context context) {
    super(context, DB_NAME, null, DATABASE_VERSION);
}

public List<FullCard> getCards() {

    SQLiteDatabase db = getReadableDatabase();

    //I want to record all selected data to this list
    List<FullCard> cards = new ArrayList<FullCard>();

    Cursor cursor = db.query(SQLiteHelper.TABLE_CARDS,
            null, null, null, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        FullCard card = cursorToCard(cursor);
        cards.add(card);
        cursor.moveToNext();
    }
    cursor.close();
    return cards;
}
private FullCard cursorToCard(Cursor cursor) {
    FullCard card = new FullCard();
    card.setId(cursor.getInt(0));
    card.setCardName(cursor.getString(1));
    card.setType(cursor.getString(2));
    card.setRarity(cursor.getString(3));
    card.setCardClass(cursor.getString(4));
    card.setSet(cursor.getString(5));
    card.setRace(cursor.getString(6));
    card.setSkills(cursor.getString(7));
    card.setMana(cursor.getString(8));
    card.setAttack(cursor.getString(9));
    card.setHealth(cursor.getString(10));
    card.setDamage(cursor.getString(11));
    card.setDurability(cursor.getString(12));
    card.setCraft(cursor.getString(13));
    card.setDust(cursor.getString(14));
    card.setArtist(cursor.getString(15));
    card.setExtraInfo(cursor.getString(16));
    card.setCard_src(cursor.getString(17));
    return card;
}

}

类的片段,我调用方法getCards()并导致应用程序崩溃。

public class ActivityDB extends ListActivity implements View.OnClickListener{

private SQLiteHelper datasource;
private static Parcelable state;
private static ListView lv;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.activity_db);

    Button button_filters = (Button)findViewById(R.id.button_filters);
    Button button_menu = (Button)findViewById(R.id.button_menu);
    button_filters.setOnClickListener(this);
    button_menu.setOnClickListener(this);

    ListItem listItem;
    ArrayList<ListItem> listItems = new ArrayList<ListItem>();

    datasource = new SQLiteHelper(this); 
    List<FullCard> dbCardList = datasource.getCards(); // Here is that method

    for (FullCard aDbCardList : dbCardList) {
        listItem = new ListItem();
        listItem.setTitle(aDbCardList.getCardName());
        listItem.setPar1(aDbCardList.getMana());
        listItem.setPar2(aDbCardList.getAttack());
        listItem.setPar3(aDbCardList.getHealth());
        listItems.add(listItem);
    }

    setListAdapter(new ListItemAdapter(this,listItems));

}

这是logcat错误:
09-29 01:54:11.989 I / SQLiteAssetHelper(15846):成功打开数据库卡 09-29 01:54:12.049 W / dalvikvm(15846):threadid = 1:线程退出未捕获异常(组= 0x40018578) 09-29 01:54:12.069 E / AndroidRuntime(15846):致命异常:主要 09-29 01:54:12.069 E / AndroidRuntime(15846):java.lang.RuntimeException:无法启动活动ComponentInfo {com.FHS / com.FHS.ActivityDB}:android.database.sqlite.SQLiteException:unknown error:Unable将BLOB转换为字符串 09-29 01:54:12.069 E / AndroidRuntime(15846):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 09-29 01:54:12.069 E / AndroidRuntime(15846):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 09-29 01:54:12.069 E / AndroidRuntime(15846):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117) 09-29 01:54:12.069 E / AndroidRuntime(15846):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:935) 09-29 01:54:12.069 E / AndroidRuntime(15846):在android.os.Handler.dispatchMessage(Handler.java:99) 09-29 01:54:12.069 E / AndroidRuntime(15846):在android.os.Looper.loop(Looper.java:130) 09-29 01:54:12.069 E / AndroidRuntime(15846):在android.app.ActivityThread.main(ActivityThread.java:3687) 09-29 01:54:12.069 E / AndroidRuntime(15846):at java.lang.reflect.Method.invokeNative(Native Method) 09-29 01:54:12.069 E / AndroidRuntime(15846):at java.lang.reflect.Method.invoke(Method.java:507) 09-29 01:54:12.069 E / AndroidRuntime(15846):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:867) 09-29 01:54:12.069 E / AndroidRuntime(15846):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 09-29 01:54:12.069 E / AndroidRuntime(15846):at dalvik.system.NativeStart.main(Native Method) 09-29 01:54:12.069 E / AndroidRuntime(15846):引起:android.database.sqlite.SQLiteException:未知错误:无法将BLOB转换为字符串 09-29 01:54:12.069 E / AndroidRuntime(15846):at android.database.CursorWindow.getString_native(Native Method) 09-29 01:54:12.069 E / AndroidRuntime(15846):在android.database.CursorWindow.getString(CursorWindow.java:361) 09-29 01:54:12.069 E / AndroidRuntime(15846):在android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49) 09-29 01:54:12.069 E / AndroidRuntime(15846):at com.FHS.SQLiteHelper.cursorToCard(SQLiteHelper.java:84) 09-29 01:54:12.069 E / AndroidRuntime(15846):at com.FHS.SQLiteHelper.getCards(SQLiteHelper.java:73) 09-29 01:54:12.069 E / AndroidRuntime(15846):at com.FHS.ActivityDB.onCreate(ActivityDB.java:65) 09-29 01:54:12.069 E / AndroidRuntime(15846):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 09-29 01:54:12.069 E / AndroidRuntime(15846):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 09-29 01:54:12.069 E / AndroidRuntime(15846):... 11更多 09-29 01:54:23.589 W / InputManagerService(207):得到RemoteException向pid发送setActive(false)通知15846 uid 10062

1 个答案:

答案 0 :(得分:0)

抱歉,忘了最后一个答案。再看完之后,我发现自己错了。您的logcat中有一行说:

E/AndroidRuntime(15846): Caused by: android.database.sqlite.SQLiteException: unknown error: Unable to convert BLOB to string 09-29 01:54:12.069 E/AndroidRuntime(15846): at android.database.CursorWindow.getString_native(Native Method) 09-29 01:54:12.069 

您是否已将某些列定义为blob?


<德尔> `SQLiteHelper.TABLE_CARDS`应该是`SQliteDatabase.query()`中的第二个参数,而不是第一个:     public Cursor query(String table,String [] columns,String selection,String [] selectionArgs,String groupBy,String having,String orderBy)