-1

我正在尝试从 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: 线程退出未捕获异常 (group=0x40018578) 09- 29 01:54:12.069 E/AndroidRuntime(15846): 致命异常: main 09-29 01:54:12.069 E/AndroidRuntime(15846): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.FHS/com. FHS.ActivityDB}:android.database.sqlite.SQLiteException:未知错误:无法将 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): 在 java.lang.reflect.Method.invokeNative(本机方法) 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):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 09-29 01:54:12.069 E/AndroidRuntime(15846):在 com.android.internal.os.ZygoteInit。main(ZygoteInit.java:625) 09-29 01:54:12.069 E/AndroidRuntime(15846): 在 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):在 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): 在 com.FHS.SQLiteHelper.cursorToCard(SQLiteHelper.java:84) 09-29 01:54:12.069 E/AndroidRuntime(15846) ): 在 com.FHS.SQLiteHelper.getCards(SQLiteHelper.java:73) 09-29 01:54:12。069 E/AndroidRuntime(15846): 在 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 15846 uid 10062 发送 setActive(false) 通知1615) 09-29 01:54:12.069 E/AndroidRuntime(15846): ... 11 更多 09-29 01:54:23.589 W/InputManagerService(207): 收到 RemoteException 向 pid 15846 uid 10062 发送 setActive(false) 通知1615) 09-29 01:54:12.069 E/AndroidRuntime(15846): ... 11 更多 09-29 01:54:23.589 W/InputManagerService(207): 收到 RemoteException 向 pid 15846 uid 10062 发送 setActive(false) 通知

4

1 回答 1

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()` 中的第二个参数,而不是第一个:公共游标查询(字符串表,字符串 [] 列,字符串选择,字符串 [] 选择参数,字符串 groupBy,字符串有,字符串 orderBy)
于 2013-09-28T23:09:14.333 回答