Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓SQLite“;没有此类表格”;外部数据库异常_Android_Sql_Sqlite_Import - Fatal编程技术网

Android 安卓SQLite“;没有此类表格”;外部数据库异常

Android 安卓SQLite“;没有此类表格”;外部数据库异常,android,sql,sqlite,import,Android,Sql,Sqlite,Import,我在Android应用程序上导入外部SQLite DB时遵循了一些教程,看起来我做的每件事都“很好”,但我总是遇到同样的错误: Caused by: android.database.sqlite.SQLiteException: no such table: games (code 1): , while compiling: SELECT * FROM games 我在“SQLite数据库浏览器: 我将数据库保存为“testDB.DB”,并将其放在Android Studio项目的“资产

我在Android应用程序上导入外部SQLite DB时遵循了一些教程,看起来我做的每件事都“很好”,但我总是遇到同样的错误:

Caused by: android.database.sqlite.SQLiteException: no such table: games (code 1): , while compiling: SELECT * FROM games
我在“SQLite数据库浏览器:

我将数据库保存为“testDB.DB”,并将其放在Android Studio项目的“资产”文件夹中

代码如下:

public class DatabaseHelper extends SQLiteOpenHelper{

   private static String DB_NAME = "testDB.db";
   SQLiteDatabase mDb;
   private final Context myContext;

   //The Android's default system path
   private static String DB_PATH = "/data/data/com.example.jack.testimport/databases/";

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

   public void createDataBase() throws IOException{
       boolean dbExist = checkDataBase();
       if(dbExist){
           //do nothing - database already exist
       }else{
           mDb = this.getReadableDatabase();
           if (mDb.isOpen()){
               mDb.close();
           }

           try {
               copyDataBase();
           } catch (IOException e) {
               throw new Error("Error copying database");
           }
       }

   }


   public boolean checkDataBase(){
       File dbFile = myContext.getDatabasePath(DB_NAME);
       return dbFile.exists();
   }


   private void copyDataBase() throws IOException{
       InputStream myInput = myContext.getAssets().open(DB_NAME);

       String outFileName = DB_PATH + DB_NAME;

       OutputStream myOutput = new FileOutputStream(outFileName);

       byte[] buffer = new byte[1024];
       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{
       String myPath = DB_PATH + DB_NAME;
       mDb = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
   }

   @Override
   public synchronized void close() {
       if(mDb != null)
           mDb.close();
       super.close();
   }

   @Override
   public void onCreate(SQLiteDatabase db) {
   }

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

   //Query all data:
   public Cursor fetchGames(){
       return mDb.query("games", null,null,null,null,null,null);
   }

   public static String getDbName(){
       return DB_NAME;
   }

}
从我的活动中,我将此方法称为:

int counter=0;
DatabaseHelper myDbHelper = new DatabaseHelper(ImportDatabase.this);

try {
   myDbHelper.createDataBase();
} catch (IOException ioe) {
   throw new Error("Unable to create database");
}
try {
    myDbHelper.openDataBase();
    counter=myDbHelper.fetchGames().getCount();
} catch (SQLException sqle) {
    throw sqle;
}
计数器总是0,我在这个线程上得到错误。 我试图清理/重建项目,删除所有数据并重新安装应用程序,更改路径,我遵循所有相关问题(因此我希望你不要将其报告为重复),等等…我快疯了!我能做些什么来修复此错误


提前谢谢。

我已经在我的设备上测试了你的代码。 但是,

  • 你能用我的createDatabase方法测试下几行代码吗

    public void createDataBase() throws IOException{
        try
        {
            openDataBase();
        }catch(SQLException se)
        {
        }
    
        //if database does exists
        if(mDb != null)
        {
            //close it before open
            mDb.close();
        }
        else
        {
            //if the database does not exists..onCreate method will be called
            this.getReadableDatabase();
            //copy it from the assets directory.
            copyDataBase();
        }
    }
    
  • “com.example.jack.testimport”是AndroidManifest.xml中的包名吗

  • 当数据库位于与包不同的位置(例如'com.example.jack')时,您会收到这样的错误消息

    您的数据库位置必须为

    '/data/data/[package name in your AndroidManifest.xml]/databases/'
    
    我收到了和你类似的信息。

    当我的数据库路径为“com.tobee.myapp.db”并且包位置为“com.tobee.myapp”时,我在上面收到了一条消息


    我希望这会有所帮助。

    @michymallo91,你的代码很好,所以我添加了另一个。我添加了你的第一点。2.这是Manifest.xml package=“com.example.jack.testimport”上的包,但我得到了相同的错误。如果我从myContext.getDatabasePath(DB_NAME.getAbsolutePath()获取路径;我得到data/user/0/com.example.jack.testimport/databases/testDB.db(如果我使用此路径打开文件,我会得到相同的错误…)我做错了什么(谢谢!但是,数据库被复制了,因为在运行复制数据库的方法后,应用程序的大小增加了几MB。你能上传你的数据库文件吗?我重新创建了数据库文件和项目,现在它可以工作了。我使用了相同的代码,所以我不明白为什么现在它可以工作。谢谢你的回答!尝试删除应用程序的数据或卸载我。)关闭应用程序,然后重新运行应用程序。已完成但不更改任何内容OK将代码复制到名为CommonQliteUtilities的新类中,然后添加行
    CommonQliteUtilities.logDatabaseInfo(myDBHelper.getWritableDatabase());
    在行
    myDBHelper.openDataBase()之后;
    (即,在调用fetchgames之前)然后重试。查看日志中的输出。您可能无意中没有在复制之前保存数据库/table name可能是错误的。输出将列出表。我怀疑您的数据库是空的。如果是,请删除这些行
    mDb=this.getReadableDatabase();If(mDb.isOpen()){mDb.close();}
    从createDataBase方法中,删除应用程序的数据并重试。我重新创建了db文件和项目,现在它可以工作了。我使用了相同的代码,所以我不明白为什么现在它可以工作。谢谢你的回答!