我一直在尝试从资源中读取我的sqlite数据库并在列表视图中填充它,每次我“找不到这样的表”时, 这是我的数据库适配器
DatabaseHelper.Java
public class DatabaseHelper extends SQLiteOpenHelper {
private static String DB_Path="/data/data/com.example.pc.myapp/databases/";
private static String DB_Name= "myDatabase.sqlite";
private final Context mycontext;
private SQLiteDatabase myDatabse;
public DatabaseHelper(Context context) {
super(context, DB_Name,null,1);
this.mycontext=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private boolean chechDatabase(){
SQLiteDatabase checkDB=null;
try {
String myPath=DB_Path+DB_Name;
checkDB=SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READONLY);
} catch (SQLException 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();
myInput.close();
myOutput.close();
}
public void openDatabse() throws SQLException{
String myPath=DB_Path+DB_Name;
myDatabse=SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READONLY);
}
public void ExeSQLData(String sql) throws SQLException{
myDatabse.execSQL(sql);
}
public Cursor queryData(String query) throws SQLException{
return myDatabse.rawQuery(query,null);
}
@Override
public synchronized void close() {
if (myDatabse != null)
myDatabse.close();
super.close();
}
public void checkAndCopyDatabase(){
boolean dbExist=chechDatabase();
if (dbExist){
Log.d("TAG","DataBase Already Exists");
}else {
this.getReadableDatabase();
try {
copyDatabase();
}catch (IOException e){
Log.d("Tag","Error Copying Database");
}
}
}
这是我的主要活动
Mainactivity.java
公共类MainActivity扩展了AppCompatActivity {
ListView listView;
public String TABLE_NAME="dic";
private DatabaseHelper dbHelper;
ArrayList<Word> arrayList=new ArrayList<Word>();
CustomAdapter customAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Load Database
dbHelper=new DatabaseHelper(this);
try {
dbHelper.checkAndCopyDatabase();
dbHelper.openDatabse();
} catch (SQLException e){
}
try {
Cursor cursor = dbHelper.queryData("Select * from "+TABLE_NAME);
if (cursor !=null){
if (cursor.moveToFirst()){
do {
Word word = new Word();
word.setmWord(cursor.getString(1));
word.setmDescription(cursor.getString(2));
arrayList.add(word);
}while (cursor.moveToNext());
}
}
}catch (SQLException e){ }
customAdapter = new CustomAdapter(this,R.layout.custom_list_view,arrayList);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(customAdapter);
customAdapter.notifyDataSetChanged();
}
}
logcat
logcat的
08-17 22:27:20.757 19145-19145/com.example.pc.myapp E/SQLiteLog: (28) failed to open "/data/data/com.example.pc.myapp/databases/myDatabase.sqlite" with flag (131072) and mode_t (0) due to error (2)
08-17 22:27:20.757 19145-19145/com.example.pc.myapp E/SQLiteLog: (14) cannot open file at line 32546 of [9491ba7d73]
08-17 22:27:20.757 19145-19145/com.example.pc.myapp E/SQLiteLog: (14) os_unix.c:32546: (2) open(/data/data/com.example.pc.myapp/databases/myDatabase.sqlite) -
08-17 22:27:20.787 19145-19145/com.example.pc.myapp E/SQLiteDatabase: Failed to open database '/data/data/com.example.pc.myapp/databases/myDatabase.sqlite'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 1294): Could not open database
#################################################################
Error Code : 1294 (SQLITE_CANTOPEN_ENOENT)
Caused By : Specified directory or database file does not exist.
(unknown error (code 1294): Could not open database)
#################################################################
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:318)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:228)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:512)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:206)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:178)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:908)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:878)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:699)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:674)
at com.example.pc.myapp.DatabaseHelper.chechDatabase(DatabaseHelper.java:45)
at com.example.pc.myapp.DatabaseHelper.checkAndCopyDatabase(DatabaseHelper.java:84)
at com.example.pc.myapp.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:6178)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2648)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
08-17 22:27:21.158 19145-19145/com.example.pc.myapp D/Tag: Error Copying Database
08-17 22:27:21.158 19145-19145/com.example.pc.myapp E/SQLiteLog: (1) no such table: dic
08-17 22:27:21.258 19145-19563/com.example.pc.myapp D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: false
08-17 22:27:21.268 19145-19145/com.example.pc.myapp D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
08-17 22:27:21.268 19145-19145/com.example.pc.myapp D/PhoneWindow: *FMB* isFloatingMenuEnabled return false
08-17 22:27:21.488 19145-19563/com.example.pc.myapp I/OpenGLRenderer: Initialized EGL, version 1.4
08-17 22:27:21.498 19145-19563/com.example.pc.myapp D/OpenGLRenderer: Enabling debug mode 0
08-17 22:27:21.808 19145-19145/com.example.pc.myapp I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@3f408cb9 time:154747142
08-17 22:27:29.086 19145-19145/com.example.pc.myapp I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@3f408cb9 time:154754420
08-17 22:28:58.363 19145-19145/com.example.pc.myapp D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
答案 0 :(得分:0)
您访问数据库的方法不合适我使用以下代码
private void importDatabaseFromAssets() {
try {
InputStream myInput = getAssets().open(DatabaseManager.DATABASE_NAME);
String DB_PATH = "/data/data/" + getPackageName() + "/databases/";
String outFileName = DB_PATH + DatabaseManager.DATABASE_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();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
您可以使用以下方法检查该数据库是否存在:
private boolean isDataBaseExist() {
String DB_PATH = "/data/data/" + getPackageName() + "/databases/";
File dbFile = new File(DB_PATH + DatabaseManager.DATABASE_NAME);
return dbFile.exists();
}