jeudi 23 juillet 2015

android.database.sqlite.SQLiteException: no such table (code 1)

I'm getting this error of being unable to find the table.

I tried the solutions found through surfing like erasing the database in the emulator, but it didn't work.

I think it is related to reading a database from a different class, but I don't know how to manage this.

p.s. I'm currently using the sample provided by Android Studio 'SlidingTabsBasic'

SlidingTabsBasicFragment.java

public class SlidingTabsBasicFragment extends Fragment {

static final String LOG_TAG = "SlidingTabsBasicFragment";

final String[] genre = {"고전문학", "b", "c", "d", "e","f","g","h","i","j" };

String[] genre1;

final String[] genre2 = {"...","3","4","5"};

SQLiteDatabase db;
MySQLiteOpenHelper helper;


...



class SamplePagerAdapter extends PagerAdapter {

...

public Object instantiateItem(ViewGroup container, int position) {

        helper = new MySQLiteOpenHelper(getActivity(), MainActivity.DATABASE_NAME, null, MainActivity.DATABASE_VERSION);
        db = helper.getWritableDatabase();

        String sql_genre1 = "select name from " + MySQLiteOpenHelper.DATABASE_TABLE_NAME +
                "";

        Cursor cursor_genre1 = db.rawQuery(sql_genre1,null); // where problem occurs

        if (cursor_genre1!=null){
             int count_genre1 = cursor_genre1.getCount();

             String[] temparray_genre1 = new String[count_genre1+1];

            temparray_genre1[0]="...";

            for (int i=1; i<=count_genre1 ; i++){

                temparray_genre1[i] = cursor_genre1.getString(0);
                cursor_genre1.moveToNext();
            }

            genre1 = temparray_genre1;

        }

...

MySQLiteOpenHelper.java

package com.example.android.slidingtabsbasic;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class MySQLiteOpenHelper extends SQLiteOpenHelper {

  final static public String DATABASE_TABLE_NAME = "LDB";

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

@Override
public void onCreate(SQLiteDatabase db) {

    createTable(db);
    insertData(db);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    String sql = "drop table if exists database";
    db.execSQL(sql);
    onCreate(db);

}

public void createTable(SQLiteDatabase db) {

    String sql ="create table " + DATABASE_TABLE_NAME
            + "(name text,"
            + "author text,"
            + "genre text,"
            + "isBookmarked integer)";

    db.execSQL(sql);

}

public void insertData(SQLiteDatabase db) {

    db.beginTransaction();

    insertintoLDB(db,"공무도하가", "작자미상", "고대가요");
    insertintoLDB(db,"구지가","작자미상","고대가요");
    insertintoLDB(db,"해가","작자미상","고대가요");
    insertintoLDB(db,"정읍사","작자미상","고대가요");
    insertintoLDB(db,"황조가","유리왕","고대가요");


    db.endTransaction();

}

public void insertintoLDB(SQLiteDatabase db, String name, String author, String genre){
    String sql = "insert into " + DATABASE_TABLE_NAME + "(name, author, genre, isBookmarked) "
            +"values('"+ name + "','" + author + "','" + genre + "',0)";

    db.execSQL(sql);
}
}

MainActivity.java

public class MainActivity extends SampleActivityBase {

public static final String TAG = "MainActivity";

public static final int DATABASE_VERSION = 2;

public static final String DATABASE_NAME = "LiteratureDatabase.db";

SQLiteDatabase db;
MySQLiteOpenHelper helper;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    createDatabase();







    if (savedInstanceState == null) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        SlidingTabsBasicFragment fragment = new SlidingTabsBasicFragment();
        transaction.replace(R.id.sample_content_fragment, fragment);
        transaction.commit();
    }
}

public void createDatabase() {
    helper = new MySQLiteOpenHelper(this, DATABASE_NAME, null, DATABASE_VERSION);
    db = helper.getWritableDatabase();
}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {

    return super.onOptionsItemSelected(item);
}


}

Logcat

07-23 08:50:24.075  27205-27205/com.example.android.slidingtabsbasic     E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.android.slidingtabsbasic, PID: 27205
android.database.sqlite.SQLiteException: no such table: LDB (code 1): , while compiling: select name from LDB

Aucun commentaire:

Enregistrer un commentaire