Sqlite无法将数据库复制到设备

时间:2019-05-27 21:07:13

标签: android

我使用android studio开发了一个简单的应用程序来将数据保存到sqlite (我的资产文件夹下的数据库文件名为customer2.db)

该应用在模拟器上运行良好,但是当我在设备(索尼z2)上运行该应用时,它显示了数据已复制错误

在我的主要活动中,我检查数据库的存在性(如果存在),如果不存在,则在设备上创建它

所以我如何在索尼设备上运行这个简单的应用程序。

感谢您的帮助。谢谢。

我使用以下代码检查数据库:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    databaseHelp = new DatabaseHelp(this);

    File dataFile = getDatabasePath(DatabaseHelp.DATABASE_NAME);
    if(!dataFile.exists()){
        if(databaseHelp.copyDatabase(this)){
            Toast.makeText(this, "Data Copied success", Toast.LENGTH_LONG).show();
        }else {
            Toast.makeText(this, "Data Copied error", Toast.LENGTH_LONG).show();
            return;
        }
    }

//我的DatabaseHelp类是

公共类DatabaseHelp扩展了SQLiteOpenHelper {

public static String DATABASE_NAME = "customers2.db";
public static int DATABASE_VERSION = 1;
public String Location;
public SQLiteDatabase mSQL;
public Context mContext;

public DatabaseHelp(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    Location = "data/user/0/" + context.getPackageName() + "/" + "databases/";
    mContext = context;
}


@Override
public void onCreate(SQLiteDatabase db) {

}

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

}


public ArrayList<customer> getcustomers() {
    openDatabase();
    ArrayList<customer> customer = new ArrayList<>();
    Cursor cursor = mSQL.rawQuery("Select * From cust_file", null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        customer.add(new customer(cursor.getInt(0), cursor.getString(1),
                cursor.getString(2)));
        cursor.moveToNext();
    }
    cursor.close();
    closeDatabase();
    return customer;
}




public void addCustomer(customer customer){
    openDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("cus_no",customer.getcustno());
    contentValues.put("cus_name",customer.getName());
    contentValues.put("cus_tel",customer.getCus_tel());
    mSQL.insert("cust_file",null,contentValues);

    closeDatabase();
}


public void openDatabase() {
    String dataPath = mContext.getDatabasePath(DATABASE_NAME).getPath();
    if (mSQL != null && mSQL.isOpen()) {
        return;
    }
    mSQL = SQLiteDatabase.openDatabase(dataPath, null, SQLiteDatabase.OPEN_READWRITE);
}

public void closeDatabase() {
    if (mSQL != null) {
        mSQL.close();
    }
}


public Boolean copyDatabase(Context context) {
    try {
        InputStream inputStream = context.getAssets().open(DATABASE_NAME);
        String outFileName = Location + DATABASE_NAME;
        OutputStream outputStream = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1024];
        int length = 0;
        while ((length = inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);
        }
        outputStream.flush();
        outputStream.close();
        return true;
    } catch (Exception e) {
        return false;
    }
}

}

0 个答案:

没有答案