2

我在 Android 上使用 SQLite 时遇到问题。我能够解析 XML 文件,然后创建一个数据库来存储内容。创建,插入工作正常。我可以在文件资源管理器中看到 .db 文件

我最后一次调用 checkDataBase() 返回 false ?!为什么 ?

我正在使用模拟器上的 Android 2.3。

难道我做错了什么 ?

活动:

public class MTGDBActivity extends Activity{

    String currentDBPath = "data/data/rudy.jaumain.mtgdb/databases/MTGCards";
    MTGContainerData mtgcd;
    MTGDatabase mtgdb;

    Button buttonEditions, buttonCollection, buttonDecks;
    View v;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
                this.mtgcd = new MTGContainerData(this);
                if (!this.checkDataBase()) {
                    System.out.println("FILE DOESN'T EXIST");
                    this.mtgdb = new MTGDatabase(this);
                    this.mtgdb.open();
                    this.mtgcd.loadCards(this.mtgdb);
                    System.out.println("CARDS LOADED");
                    this.mtgdb.close();
                }
                else{
                    System.out.println("FILE DOES EXIST");
                }

        } catch (Exception e) {
            System.out.println("FAIL");
        }

        System.out.println(this.checkDataBase());
        this.setContentView(R.layout.main);
        this.initialize();
    }

    private boolean checkDataBase() {
        SQLiteDatabase checkDB = null;
        try {
            checkDB = SQLiteDatabase.openDatabase(currentDBPath, null,
                    SQLiteDatabase.OPEN_READONLY);
            checkDB.close();
        } catch (SQLiteException e) {
            System.out.println("DATABASE DOES NOT EXIST");
        }
        return checkDB != null ? true : false;
    }


    public void initialize(){
        try{

            v = (View)this.findViewById(R.id.mainLayout);
            v.setBackgroundColor(Color.WHITE);

            this.buttonEditions = (Button)findViewById(R.id.buttonEdition);
            this.buttonEditions.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {

                    System.out.println("CLICKED");
                        Intent i = new Intent(v.getContext(), MTGDBEditionsActivity.class);
                        startActivityForResult(i,0);
                }
            });

            this.buttonCollection = (Button)findViewById(R.id.buttonCollection);
            this.buttonCollection.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {

                    System.out.println("CLICKED");
                        Intent i = new Intent(v.getContext(), MTGDBCollectionActivity.class);
                        startActivityForResult(i,0);
                }
            });

            this.buttonDecks = (Button)findViewById(R.id.buttonDecks);
            this.buttonDecks.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {

                    System.out.println("CLICKED");
                        Intent i = new Intent(v.getContext(), MTGDBDecksActivity.class);
                        startActivityForResult(i,0);
                }
            });

        }
        catch(Exception e1){
            e1.printStackTrace();
        }

    }
}

数据库:

public class MTGDatabase{

private static final String TABLE_CARDS = "table_cards";
private static final String COL_ID = "ID";
private static final int NUM_COL_ID = 0;
private static final String COL_NAME = "NAME";
private static final int NUM_COL_NAME = 1;
private static final String COL_EDITION = "EDITION";
private static final int NUM_COL_EDITION = 2;
private static final String COL_RARITY = "RARITY";
private static final int NUM_COL_RARITY = 3;
private static final String COL_MANACOST = "MANACOST";
private static final int NUM_COL_MANACOST = 4;
private static final String COL_NUMBER = "NUMBER";
private static final int NUM_COL_NUMBER = 5;
private static final String COL_COLOR = "COLOR";
private static final int NUM_COL_COLOR = 6;

public int VERSION_BDD = 1;
private SQLiteDatabase sqldb;
private MTGDatabaseAdapter mtgbdAdapter;

public MTGDatabase(Context c) {
    mtgbdAdapter = new MTGDatabaseAdapter(c, "MTGCards.db", null, VERSION_BDD);
}

public void open(){
    sqldb = mtgbdAdapter.getWritableDatabase();
}

public void close(){
    sqldb.close();
}

public SQLiteDatabase getDatabase(){
    return sqldb;
}

public long insertCard(MTGCard card){
    ContentValues values = new ContentValues();
    values.put(COL_NAME, card.getName());
    values.put(COL_EDITION, card.getEdition());
    values.put(COL_RARITY, card.getRarity());
    values.put(COL_MANACOST, card.getManacost());
    values.put(COL_NUMBER, card.getNumber());
    values.put(COL_COLOR, card.getColor());
    return sqldb.insert(TABLE_CARDS, null, values);
}

public int updateCard(int id, MTGCard card){
    ContentValues values = new ContentValues();
    values.put(COL_NAME, card.getName());
    values.put(COL_EDITION, card.getEdition());
    values.put(COL_RARITY, card.getRarity());
    values.put(COL_MANACOST, card.getManacost());
    values.put(COL_NUMBER, card.getNumber());
    values.put(COL_COLOR, card.getColor());
    return sqldb.update(TABLE_CARDS, values, COL_ID + " = " +id, null);
}

public int removeCardWithID(int id){
    return sqldb.delete(TABLE_CARDS, COL_ID + " = " +id, null);
}

public MTGCard getCardWithName(String name){
    Cursor c = sqldb.query(TABLE_CARDS, new String[] {COL_ID, COL_NAME, COL_EDITION, COL_RARITY, COL_MANACOST, COL_NUMBER, COL_COLOR}, COL_NAME + " LIKE \"" + name +"\"", null, null, null, null);
    return cursorToCard(c);
}


private MTGCard cursorToCard(Cursor c){

    if (c.getCount() == 0)
        return null;

    c.moveToFirst();

    MTGCard card = new MTGCard();
    card.setId(c.getInt(NUM_COL_ID));
    card.setName(c.getString(NUM_COL_NAME));
    card.setEdition(c.getString(NUM_COL_EDITION));
    card.setRarity(c.getString(NUM_COL_RARITY));
    card.setManacost(c.getString(NUM_COL_MANACOST));
    card.setNumber(c.getString(NUM_COL_NUMBER));
    card.setColor(c.getString(NUM_COL_COLOR));

    c.close();

    return card;
}
}

数据库适配器:

public class MTGDatabaseAdapter extends SQLiteOpenHelper {
private static final String TABLE_CARDS = "table_cards";
private static final String COL_ID = "_ID";
private static final String COL_NAME = "NAME";
private static final String COL_EDITION = "EDITION";
private static final String COL_RARITY = "RARITY";
private static final String COL_MANACOST = "MANACOST";
private static final String COL_NUMBER = "NUMBER";
private static final String COL_COLOR = "COLOR";

private static final String CREATE_BDD = "CREATE TABLE " 
    + TABLE_CARDS 
+ " (" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
+ COL_NAME + " TEXT NOT NULL, "
+ COL_EDITION + " TEXT NOT NULL, "
+ COL_RARITY + " TEXT NOT NULL, "
+ COL_MANACOST + " TEXT NOT NULL, "
+ COL_NUMBER + " TEXT NOT NULL, "
+ COL_COLOR + " TEXT NOT NULL);";

public MTGDatabaseAdapter(Context context, String name,
        CursorFactory factory, int version) {
    super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(CREATE_BDD);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE " + TABLE_CARDS + ";");
    onCreate(db);
}
}
4

2 回答 2

5

我建议您使用 SQLiteOpenHelper 类:http: //developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html

如果数据库不存在,此类将创建数据库,为此它将执行onCreate()方法中的代码,如果您扩展 SQLiteOpenHelper 类,您可以覆盖该代码。

您可以通过调用getReadableDatabase()getWritableDatabase()方法来检索数据库实例。

于 2012-04-17T21:53:44.423 回答
2

如果尚未创建数据库,则在您的实例上调用getReadableDatabase()or将创建数据库。为了设置应用程序的数据库,您不应该在实现中需要任何硬编码路径(我不确定您要尝试用“解析 XML”做什么)。getWriteableDatabase()SQLiteOpenHelper

创建 SQLiteOpenHelper 类时,请参阅开发人员站点上的记事本教程以获取参考。

有关如何在应用程序生命周期过程中正确处理您的信息,请参阅这篇文章SQLiteOpenHelper

于 2012-04-17T22:06:31.537 回答