0

それでも私はJavaとAndroidの開発にかなり慣れていません。しかし、この部分では、何が悪いのかを見つけることができませんでした。LogCatは、「そのようなテーブルはありません:変換」を返します。

package se.maxallan.birdsound;

    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;
    import android.util.Log;

    public class SpeciesDbAdapter extends SQLiteOpenHelper{
    public static final String DB_SPECIE_ID = "_id";
    public static final String DB_SCF_NAME = "scf_name";

    public static final String DB_T_LANG = "lang";
    public static final String DB_T_TRANS = "translated";

    private static SpeciesDbAdapter mDbHelp;
    private static SQLiteDatabase mDb;

    private static final String DB_NAME = "database.db";
    private static final String DB_TBL_SPECIES = "species";
    private static final String DB_TBL_TRANSLATIONS = "translations";
    //private static final String DB_TBL_SOUNDS = "sounds";
    private static final int DB_VERSION = 1;

    private final Context mCtx;

    static String createSpecies = "CREATE TABLE if not exists "
    + DB_TBL_SPECIES 
    +" ("+DB_SPECIE_ID +" INTEGER PRIMARY KEY AUTOINCREMENT,"
    +DB_SCF_NAME+");"; 

    static String createTranslations = "CREATE TABLE if not exists "
    + DB_TBL_TRANSLATIONS
    +" (tid INTEGER PRIMARY KEY AUTOINCREMENT,"
    +DB_SPECIE_ID+","
    +DB_T_TRANS+","
    +DB_T_LANG+");"; //The error is here?

    public SpeciesDbAdapter(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        this.mCtx = context;
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(createSpecies);
        db.execSQL(createTranslations);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    //Open the database
    public SpeciesDbAdapter open() throws SQLException {
        mDbHelp = new SpeciesDbAdapter(mCtx);
        mDb = mDbHelp.getWritableDatabase();
        return this;
    }
    //Close the connection
    public void close() {
        if (mDbHelp != null) {
            mDbHelp.close();
        }
    }

    public void addSpecie(String scf, String translation, String lang){
        ContentValues specieValues = new ContentValues();
        ContentValues translationsValues = new ContentValues();
        specieValues.put(DB_SCF_NAME, scf);
        int LastInsertedId = (int) mDb.insert(DB_TBL_SPECIES, null, specieValues);
        translationsValues.put(DB_T_LANG, lang);
        translationsValues.put(DB_T_TRANS, translation);
        translationsValues.put(DB_SPECIE_ID, LastInsertedId);
        mDb.insert(DB_TBL_TRANSLATIONS, null, translationsValues);
    }

    public Cursor fetchAllSpecies() {
        Cursor mCursor = mDb.rawQuery("select * from "+ DB_TBL_SPECIES +" s inner join "+ DB_TBL_TRANSLATIONS +" t on s._id=t._id", null);
        Log.d("Database", "Result cursor size " + mCursor.getCount() );
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }
    public void insertSomeSpecies() {
        addSpecie("Cygnus olor", "Knölsvan", "sv");
        addSpecie("Cygnus cygnus", "Sångsvan", "sv");
    }

    }

エラーは、テーブルを作成したか、作成しようとしたかだと思いtranslationsます...

4

2 に答える 2

3

私はあなたがたった1つのテーブルであなたのアプリを少なくとも1つ実行したと推測しています:

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(createSpecies);
}

そして、あなたは追加しました:

    db.execSQL(createTranslations);

OpenHelperは、ここでの変更を自動的にチェックしません。データベースを強制的に再作成する必要があります。これを行う最も簡単な方法は、を増やすことだと思いますDB_VERSION

private static final int DB_VERSION = 2;

これが実行onUpgrade()され、既存のスキーマが削除され、新しいスキーマが作成されます。

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + DB_TBL_SPECIES);
    db.execSQL("DROP TABLE IF EXISTS " + DB_TBL_TRANSLATIONS);
    onCreate(db);
}

次にスキーマを変更するときは、1つの数字(DB_VERSION = 3)を変更するだけで、新しいテーブルが作成されます。

于 2012-09-19T19:02:57.480 に答える
2

ターミナルからデータベースを削除します

adb shell
cd /data/data/com.example.apploicationname/databases
rm *

DB_VERSIONを変更するのではなく、これも役立つと思います

于 2013-11-22T15:48:50.090 に答える