我有一个.sqllite数据库文件,我尝试打开这个数据库并插入一些值。但是当我运行应用程序时我有log cat error.can't打开数据库 这是我的代码
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错误代码
答案 0 :(得分:0)
可能是因为sql文件位于资产文件夹中。我有一个类似的问题使用我的资产文件夹中的exe。当我想使用它时,我不得不将它复制到手机存储器中,然后当我知道我不再使用它时将其删除。 这可能是因为手机上的应用程序是一个.apk文件(就像一个zip文件),除非你使用adroid资产助手,否则你无法访问其中的文件。
答案 1 :(得分:0)
已经多次讨论过这个问题。例如,请参阅Reading sqlite file from asset folder或使用搜索功能: - )