0
    Here is the one more problem which i m facing this time the runnin time error am getting is Failed To open the database.. please look at my logcat file and suggest me the solution .

这是错误 m 得到“无法打开数据库'/data/data/com.example.crudoperation/myDB'。12-03 13:24:33.651: E/SQLiteDatabase(798): android.database.sqlite.SQLiteCantOpenDatabaseException:未知错误(代码 14):无法打开”。请查看 DataBaseManager 类中的代码,并为我找到一些解决方法。我已经完美地运行了此代码。但是我尝试将此代码嵌入到其他一些项目中,但在这里我得到了这个错误。

数据库管理器.java

package com.example.crudoperation;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DBAdapter extends SQLiteOpenHelper{

    //The Android's default system path of your application database.
    private static String DB_PATH = "/data/data/com.example.crudoperation/"; 
    private static String DB_NAME = "myDB";
    private SQLiteDatabase myDataBase; 
    private final Context myContext;
    private static String DB_IN = "firstdb";
    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "name";
    public static final String KEY_EMAIL = "email";
    private static final String DATABASE_TABLE = "Contact";

    /**
     * Constructor
     * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
     * @param context
     */
    public DBAdapter(Context context) {
        super(context, DB_NAME, null, 1);
        this.myContext = context;
    }   

  /**
     * Creates a empty database on the system and rewrites it with your own database.
     * */
    public void createDataBase() throws IOException{
        boolean dbExist = checkDataBase();

        if(dbExist){
            //do nothing - database already exist
        }else{
            //By calling this method and empty database will be created into the default system path
               //of your application so we are gonna be able to overwrite that database with our database.
            this.getReadableDatabase();
            try {
                copyDataBase();
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
        }
    }

    /**
     * Check if the database already exist to avoid re-copying the file each time you open the application.
     * @return true if it exists, false if it doesn't
     */
    private boolean checkDataBase(){
        SQLiteDatabase checkDB = null;
        try{
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
        }catch(SQLiteException e){
            //database does't exist yet.
        } 
        if(checkDB != null){
            checkDB.close();
        }

        return checkDB != null ? true : false;
    }

    /**
     * Copies your database from your local assets-folder to the just created empty database in the
     * system folder, from where it can be accessed and handled.
     * This is done by transfering bytestream.
     * */
    private void copyDataBase() throws IOException{ 
        //Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(DB_IN);

        // 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{
        //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();
    }

  //---insert a contact into the database---
    public long insertContact(String name, String email)
    {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_NAME, name);
    initialValues.put(KEY_EMAIL, email);
    return myDataBase.insert(DATABASE_TABLE, null, initialValues);
    }

  //---updates a contact---
    public boolean updateContact(long rowId, String name, String email)
    {
    ContentValues args = new ContentValues();
    args.put(KEY_NAME, name);
    args.put(KEY_EMAIL, email);
    return myDataBase.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
    }

  //---deletes a particular contact---
    public boolean deleteContact(long rowId)
    {
    return myDataBase.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

    //----Retrieving all Contacts From Database ------
    public Cursor getAllContacts()
    {
        return myDataBase.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
                KEY_EMAIL}, null, null, null, null, null);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {

    }

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

    }

        // Add your public helper methods to access and get content from the database.
       // You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
       // to you to create adapters for your views.

}


LogCat: 

12-03 13:24:33.623: E/SQLiteLog(798): (14) cannot open file at line 30174 of [00bb9c9ce4]
12-03 13:24:33.623: E/SQLiteLog(798): (14) os_unix.c:30174: (2) open(/data/data/com.example.crudoperation/myDB) - 
12-03 13:24:33.651: E/SQLiteDatabase(798): Failed to open database '/data/data/com.example.crudoperation/myDB'.
12-03 13:24:33.651: E/SQLiteDatabase(798): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
12-03 13:24:33.651: E/SQLiteDatabase(798):  at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
12-03 13:24:33.651: E/SQLiteDatabase(798):  at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
12-03 13:24:33.651: E/SQLiteDatabase(798):  at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
12-03 13:24:33.651: E/SQLiteDatabase(798):  at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
12-03 13:24:33.651: E/SQLiteDatabase(798):  at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
12-03 13:24:33.651: E/SQLiteDatabase(798):  at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
12-03 13:24:33.651: E/SQLiteDatabase(798):  at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
12-03 13:24:33.651: E/SQLiteDatabase(798):  at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
12-03 13:24:33.651: E/SQLiteDatabase(798):  at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
12-03 13:24:33.651: E/SQLiteDatabase(798):  at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
12-03 13:24:33.651: E/SQLiteDatabase(798):  at com.example.crudoperation.DBAdapter.openDataBase(DBAdapter.java:109)
12-03 13:24:33.651: E/SQLiteDatabase(798):  at com.example.crudoperation.InsertActivity.insertRecord(InsertActivity.java:70)
12-03 13:24:33.651: E/SQLiteDatabase(798):  at com.example.crudoperation.InsertActivity$1.onClick(InsertActivity.java:52)
12-03 13:24:33.651: E/SQLiteDatabase(798):  at android.view.View.performClick(View.java:4084)
12-03 13:24:33.651: E/SQLiteDatabase(798):  at android.view.View$PerformClick.run(View.java:16966)
12-03 13:24:33.651: E/SQLiteDatabase(798):  at android.os.Handler.handleCallback(Handler.java:615)
12-03 13:24:33.651: E/SQLiteDatabase(798):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-03 13:24:33.651: E/SQLiteDatabase(798):  at android.os.Looper.loop(Looper.java:137)
12-03 13:24:33.651: E/SQLiteDatabase(798):  at android.app.ActivityThread.main(ActivityThread.java:4745)
12-03 13:24:33.651: E/SQLiteDatabase(798):  at java.lang.reflect.Method.invokeNative(Native Method)
12-03 13:24:33.651: E/SQLiteDatabase(798):  at java.lang.reflect.Method.invoke(Method.java:511)
12-03 13:24:33.651: E/SQLiteDatabase(798):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-03 13:24:33.651: E/SQLiteDatabase(798):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-03 13:24:33.651: E/SQLiteDatabase(798):  at dalvik.system.NativeStart.main(Native Method)
12-03 13:24:33.651: D/AndroidRuntime(798): Shutting down VM
12-03 13:24:33.661: W/dalvikvm(798): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
12-03 13:24:33.692: E/AndroidRuntime(798): FATAL EXCEPTION: main
12-03 13:24:33.692: E/AndroidRuntime(798): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
12-03 13:24:33.692: E/AndroidRuntime(798):  at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
12-03 13:24:33.692: E/AndroidRuntime(798):  at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
12-03 13:24:33.692: E/AndroidRuntime(798):  at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
12-03 13:24:33.692: E/AndroidRuntime(798):  at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
12-03 13:24:33.692: E/AndroidRuntime(798):  at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
12-03 13:24:33.692: E/AndroidRuntime(798):  at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
12-03 13:24:33.692: E/AndroidRuntime(798):  at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
12-03 13:24:33.692: E/AndroidRuntime(798):  at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
12-03 13:24:33.692: E/AndroidRuntime(798):  at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
12-03 13:24:33.692: E/AndroidRuntime(798):  at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
12-03 13:24:33.692: E/AndroidRuntime(798):  at com.example.crudoperation.DBAdapter.openDataBase(DBAdapter.java:109)
12-03 13:24:33.692: E/AndroidRuntime(798):  at com.example.crudoperation.InsertActivity.insertRecord(InsertActivity.java:70)
12-03 13:24:33.692: E/AndroidRuntime(798):  at com.example.crudoperation.InsertActivity$1.onClick(InsertActivity.java:52)
12-03 13:24:33.692: E/AndroidRuntime(798):  at android.view.View.performClick(View.java:4084)
12-03 13:24:33.692: E/AndroidRuntime(798):  at android.view.View$PerformClick.run(View.java:16966)
12-03 13:24:33.692: E/AndroidRuntime(798):  at android.os.Handler.handleCallback(Handler.java:615)
12-03 13:24:33.692: E/AndroidRuntime(798):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-03 13:24:33.692: E/AndroidRuntime(798):  at android.os.Looper.loop(Looper.java:137)
12-03 13:24:33.692: E/AndroidRuntime(798):  at android.app.ActivityThread.main(ActivityThread.java:4745)
12-03 13:24:33.692: E/AndroidRuntime(798):  at java.lang.reflect.Method.invokeNative(Native Method)
12-03 13:24:33.692: E/AndroidRuntime(798):  at java.lang.reflect.Method.invoke(Method.java:511)
12-03 13:24:33.692: E/AndroidRuntime(798):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-03 13:24:33.692: E/AndroidRuntime(798):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-03 13:24:33.692: E/AndroidRuntime(798):  at dalvik.system.NativeStart.main(Native Method)
4

0 回答 0