android.database.sqlite.SQLiteException:没有这样的表:活动;来自数据库文件

时间:2017-06-01 01:23:45

标签: android database sqlite file android-sqlite

我正在尝试制作一个带有SQLite数据库的Android应用,但当我尝试对表格进行查询时,“activities”会向我显示RuntimeException。我尝试了很多东西,我找到了许多解决方案,但都没有。

我已经从数据库浏览器为SQLite

创建了数据库
  

引起:android.database.sqlite.SQLiteException:没有这样的表:   活动(代码1):在编译时:从活动中选择*

我知道可能这个问题是重复的,但是在其他问题中评论的所有内容对我都不起作用!!!!!

以下是扩展SQLiteOpenHelper

的代码
public class CuencasDbHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 2;


//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.example.kazuelonga.vivecuencabbdd/databases";




private static String DB_NAME = "vivecuenca_database3.db";

private SQLiteDatabase myDataBase;

private final Context myContext;



public CuencasDbHelper(Context context) {
    super(context, DB_NAME, null, DATABASE_VERSION);
    this.myContext = context;
}


/**
 * Creates a empty database on the system and rewrites it with your own database.
 * */
public void createDataBase() throws IOException {

    boolean dbExist = false;

    if(dbExist){
        //do nothing - database already exist
    }else{

        //By calling this method and empty database will be created into the default system path
        //of your application so we are gonna be able to overwrite that database with our database.
        this.getReadableDatabase();
        this.close();

        try {

            copyDataBase();
            Log.v("copyDataBase---", "Successfully");

        } catch (IOException e) {

            e.toString();
            throw new Error("Error copying database");


        }
    }

}

/**
 * Check if the database already exist to avoid re-copying the file each time you open the application.
 * @return true if it exists, false if it doesn't
 */
private boolean checkDataBase(){


    File databasePath = myContext.getDatabasePath(DB_NAME);
    Log.v("SQLite Error", "database does't exist yet.");
    return databasePath.exists();

}

/**
 * Copies your database from your local assets-folder to the just created empty database in the
 * system folder, from where it can be accessed and handled.
 * This is done by transfering bytestream.
 * */
private void copyDataBase() throws IOException{

    //Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;

    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[4096];
    int length;
    while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
    }

    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

}

public void openDataBase() throws SQLException {

    //Open the database
    String myPath = DB_PATH + DB_NAME;
    this.getWritableDatabase();
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

}




@Override
public synchronized void close() {

    if(myDataBase != null)
        myDataBase.close();

    super.close();

}

@Override
public void onCreate(SQLiteDatabase db) {


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + CuencaEntry.TABLE_NAME);
    if(newVersion>oldVersion)
        try {
            copyDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
}

当我尝试使用查询时会发生错误。

 public Cursor getStringDB(){

    return getReadableDatabase()
            .query(
                    CuencaEntry.TABLE_NAME,
                    null,
                    CuencaEntry.TIPO + " LIKE 'pub'",
                    null,
                    null,
                    null,
                    null,
                    null);
}

这是我在MainActivity中调用Helper时的代码

 CuencasDbHelper myDbHelper = new CuencasDbHelper(this);

    try {

        myDbHelper.createDataBase();

    } catch (IOException ioe) {

        throw new Error("Unable to create database");

    }

    try {

        myDbHelper.openDataBase();

    }catch(SQLException sqle){

        throw sqle;

    }

0 个答案:

没有答案