【问题标题】:No such table: table1 error when copying database from assets没有这样的表:从资产复制数据库时出现 table1 错误
【发布时间】:2017-05-14 00:00:18
【问题描述】:

我有一个存储在名为 myDatabase.db 的资产/数据库中的数据库,我想使用此处的代码将其复制到内部存储:

public class DatabaseOpener extends SQLiteOpenHelper {

private static String DB_PATH;

private static String DB_NAME = "table1.db";

private static int DB_VERSION = 4;

private SQLiteDatabase myDataBase;

private static Context myContext;

private static DatabaseOpener instance;

private DatabaseOpener(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
    this.myContext = context;
}

public static synchronized DatabaseOpener getInstance(Context context) {
    if (instance==null) {
        instance = new DatabaseOpener(context);
    }

    if(Build.VERSION.SDK_INT >= 4.2) {
        DB_PATH = myContext.getApplicationInfo().dataDir + "/databases/";
    } else {
        DB_PATH = "/data/data/" + myContext.getPackageName() + "/databases/";
    }
    return instance;
}

public void createDataBase() throws IOException {
    boolean dbExist = checkDataBase();
    if(dbExist) {

    } else {
        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

private boolean checkDataBase(){
    SQLiteDatabase checkDB = null;
    try{
        String myPath = DB_PATH + DB_NAME;
        File file = new File(myPath);
        if (file.exists() && !file.isDirectory()) {
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
        }
    }catch(SQLiteException e){
        e.printStackTrace();
    }

    if(checkDB != null){
        checkDB.close();
    }

    return checkDB != null ? true : false;
}

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[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;
    File file = new File(myPath);
    if (file.exists() && !file.isDirectory()) {
        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 Browser 中打开它,它就没有表。如果我将它推入模拟器,它会再次工作,但我无法将它推入无根设备。唯一的方法是在路径上创建一个数据库文件并从资产中复制一个。那么,我做错了什么? 另外,这就是我调用 DatabaseOpener 类的方式:

    DatabaseOpener db = DatabaseOpener.getInstance(context);
    try {
        db.createDataBase();
    } catch (IOException e) {
        throw new Error ("Unable to create database");
    }
    try {
        db.openDataBase();
    }catch(SQLException sqle){
        throw sqle;
    }

    Object object = db.getObjectByName(name);

    db.close();
    return object;

编辑:我收到的错误是android.database.sqlite.SQLiteException: no such table: table1 (code 1): , while compiling

【问题讨论】:

  • 在扩展 SQLiteAssetHelper 而不是 SQLiteOpenHelper 时出现同样的错误:android.database.sqlite.SQLiteException: no such table: table1 (code 1): , while compiling。除了更改 DatabaseOpener 的父类之外,我还需要做其他事情吗?

标签: android sqlite android-assets database-create


【解决方案1】:

一旦你卸载了应用程序,android 会删除所有与应用程序相关的本地数据存储,因此 SQLDatabase 将被销毁。

当您重新安装应用程序时,您会在创建表之前尝试访问它,因此会引发错误。确保在操作其数据之前创建表。

你需要写onCreate(SQLiteDatabase db)

db.execSQL(CREATE_TABLE_QUERY);

【讨论】:

    【解决方案2】:

    我用下面的代码解决了这个问题:

    public DatabaseOpener(Context context) throws IOException {
        super(context, DB_NAME, null, DB_VERSION);
        this.myContext = context;
    
        if (Build.VERSION.SDK_INT >= 4.2) {
            DB_PATH = myContext.getApplicationInfo().dataDir + "/databases/";
        } else {
            DB_PATH = "/data/data/" + myContext.getPackageName() + "/databases/";
        }
    
        boolean dbexist = checkDataBase();
        if (dbexist) {
            openDataBase();
        } else {
            System.out.println("Database doesn't exist");
            createDataBase();
        }
    }
    
    public void createDataBase() throws IOException {
        boolean dbexist = checkDataBase();
        if (dbexist) {
            // System.out.println(" Database exists.");
        } else {
            this.getReadableDatabase();
            try {
                copyDataBase();
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
        }
    }
    
    private boolean checkDataBase() {
        boolean checkdb = false;
        try {
            String myPath = DB_PATH + DB_NAME;
            File dbfile = new File(myPath);
            checkdb = dbfile.exists();
        } catch (SQLiteException e) {
            System.out.println("Database doesn't exist");
        }
        return checkdb;
    }
    
    private void copyDataBase() throws IOException {
        // Open your local db as the input stream
        InputStream myinput = myContext.getAssets().open("databases/" + 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(DB_PATH + DB_NAME);
    
        // transfer byte to inputfile to outputfile
        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 {
        // Open the database
        String mypath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE);
    }
    
    @Override
    public synchronized void close() {
        if (myDataBase != null)
            myDataBase.close();
    
        super.close();
    }
    

    【讨论】:

      猜你喜欢
      • 2018-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-13
      • 1970-01-01
      • 2019-09-19
      • 1970-01-01
      相关资源
      最近更新 更多