Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.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_Sqlite_Database Connection_Navicat - Fatal编程技术网

android无法从资产文件夹打开sqlite数据库

android无法从资产文件夹打开sqlite数据库,android,sqlite,database-connection,navicat,Android,Sqlite,Database Connection,Navicat,我有一个.sqllite数据库文件,我试图打开这个数据库并插入一些值。但当我运行应用程序时,我遇到了log cat错误。无法打开数据库 这是我的密码 public class IrocDBController extends SQLiteOpenHelper { String DB_PATH = null; private static String DB_NAME = "CredoDatabase.sqlite"; private static String usm_Users = "Usm

我有一个.sqllite数据库文件,我试图打开这个数据库并插入一些值。但当我运行应用程序时,我遇到了log cat错误。无法打开数据库 这是我的密码

public class IrocDBController extends SQLiteOpenHelper {

String DB_PATH = null;
private static String DB_NAME = "CredoDatabase.sqlite";
private static String usm_Users = "Usm_Users";

private SQLiteDatabase myDataBase;

private final Context myContext;
public IrocDBController(Context context) {

    super(context, DB_NAME, null, 2);
    this.myContext = context;
    DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
}
public void InsertToLoginTable(String username, String password,
        String LoanOfficerId, String BranchId) {
    ContentValues newValues = new ContentValues();
    newValues.put("UserName", username);
    newValues.put("Password", password);
    newValues.put("LoanOfficerId", LoanOfficerId);
    newValues.put("BranchId", BranchId);

    myDataBase.insert(usm_Users, null, newValues);
}


public boolean DublicateValues(int LoanOfficerId) {
    myDataBase = this.getWritableDatabase();
    Cursor cursor = myDataBase.rawQuery("select * from " + usm_Users + " where "
            + "LoanOfficerId" + " == " + LoanOfficerId, null);
    boolean exists = (cursor.getCount() > 0);
    Log.e("aaaaaaaaaaaaaaaaaaaa", String.valueOf(cursor.getCount()));
    cursor.close();
    return exists;
}

public void createDataBase() throws IOException {
    boolean dbExist = checkDataBase();
    if (dbExist) {
        // do nothing - database already exist
    } else {
        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");

        }
    }

}

public String getNameFildeEntry(String username) {
    Cursor cursor = myDataBase.query(usm_Users, null, " UserName=?",
            new String[] { username }, null, null, null);
    if (cursor.getCount() < 1) // UserName Not Exist
    {
        cursor.close();
        return "NOT EXIST";
    }
    cursor.moveToFirst();
    String getUserName = cursor
            .getString(cursor.getColumnIndex("UserName"));

    cursor.close();
    return getUserName;
}

public String getPasswordFildeEntry(String Password) {
    Cursor cursor = myDataBase.query(usm_Users, null, " Password=?",
            new String[] { Password }, null, null, null);
    if (cursor.getCount() < 1) // UserName Not Exist
    {
        cursor.close();
        return "NOT EXIST";
    }
    cursor.moveToFirst();
    String getUserPassword = cursor.getString(cursor
            .getColumnIndex("Password"));

    cursor.close();
    return getUserPassword;
}


private boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE);
    } catch (SQLiteException e) {
    }
    if (checkDB != null) {
        checkDB.close();
    }
    return checkDB != null ? true : false;
}


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);
    }
    myOutput.flush();
    myOutput.close();
    myInput.close();
}

public void openDataBase() throws SQLException {


    String path = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(path, null,
            SQLiteDatabase.OPEN_READWRITE);
}

@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) {

}
}

我使用nativec tu创建sqlite数据库,在我的数据库中创建了一些表等,并将该数据库插入到assets文件夹中 我不知道我做错了什么。 如果有人知道答案,请帮助我 这是我的日志cat错误代码

可能是因为sql文件在您的资产文件夹中。我在使用资产文件夹中的exe时遇到类似问题。当我想使用它时,我必须将它复制到手机存储器中,然后当我知道我不再使用它时将其删除。
这可能是因为手机上的应用程序是一个.apk文件,类似于zip文件,除非使用adroid assets helper,否则无法访问其中的文件。

这已经讨论过很多次了。例如,请参见或使用搜索功能:-