Sqlite经理无法打开我的Android数据库

时间:2014-05-20 03:04:04

标签: android database sqlite sqlitemanager

当我尝试使用sqlite数据库打开我的数据库文件时,它会显示以下错误:

[2014-05-19 23:50:51] Failed to open database file db_penpal.db
[2014-05-19 23:50:51] null

这是否意味着我的数据库包含一些错误? 如果是,我该如何解决?

提前致谢。

public class Database_handler extends SQLiteOpenHelper {

    private static final int database_version = 1;
    public static final String database_name = "db_e_pal.db";

    public Database_handler(Context context) {
        super(context, database_name, null, database_version);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        //Creation de la table user
        String create_t_user
                = "CREATE TABLE IF NOT EXISTS user ("
                + "ID INTEGER PRIMARY KEY AUTOINCREMENT,"
                + "username varchar(20) NOT NULL,"
                + "password varchar(20) NOT NULL,"
                + "connected INTEGER(0) NOT NULL);";
        String create_t_friend_request
                = "CREATE TABLE IF NOT EXISTS friend_request ("
                + "ID INTEGER PRIMARY KEY AUTOINCREMENT,"
                + "username varchar(20) NOT NULL,"
                + "ID_last_message INTEGER NOT NULL);";

        String create_t_friend
                = "CREATE TABLE IF NOT EXISTS friend ("
                + "ID INTEGER PRIMARY KEY AUTOINCREMENT,"
                + "username varchar(20) NOT NULL," 
                + "connected INTEGER(0) NOT NULL);";

        String create_t_message
                = "CREATE TABLE IF NOT EXISTS message ("
                + "ID INTEGER PRIMARY KEY AUTOINCREMENT,"
                + "ID_friend INTEGER NOT NULL,"
                + "Sent bit(1) NOT NULL,"
                + "Body text NOT NULL,"
                + "DATE datetime NOT NULL);";
        //execution des requetes
        db.execSQL(create_t_user);
        db.execSQL(create_t_friend_request);
        db.execSQL(create_t_friend);
        db.execSQL(create_t_message);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Supprimer les anciennes tables si elles existent
        // Requetes pou les suppressions
        String drop_t_user = "DROP TABLE IF EXISTS user";
        String drop_t_message = "DROP TABLE IF EXISTS message";
        String drop_t_friend = "DROP TABLE IF EXISTS friend";
        String drop_t_friend_request = "DROP TABLE IF EXISTS friend_request";
        // Suppression des tables
        db.execSQL(drop_t_user);
        db.execSQL(drop_t_message);
        db.execSQL(drop_t_friend);

        db.execSQL(drop_t_friend_request);
        // Create tables again
        onCreate(db);
    }

    //Les operation faites sur la bd "les CRUD"
    //Vider la base de donnée 
    public void empty() {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete("user", null, null);
        db.delete("message", null, null);
        db.delete("friend", null, null);
        db.delete("friend_request", null, null);
        db.close();
    }
}

这是日志:

05-20 04:35:10.757: D/dalvikvm(3907): GC_EXTERNAL_ALLOC freed 458K, 55% free 3317K/7303K, 
external 1070K/1570K, paused 72ms
05-20 04:35:37.117: D/dalvikvm(3907): GC_CONCURRENT freed 435K, 47% free 3887K/7303K, 
external 1530K/2027K, paused 4ms+5ms
05-20 04:35:37.117: D/Cursor(3907): Database path: db_e_pal.db
05-20 04:35:37.117: D/Cursor(3907): Table name   : null
05-20 04:35:37.117: D/Cursor(3907): Database path: db_e_pal.db
05-20 04:35:37.117: D/Cursor(3907): Table name   : null

1 个答案:

答案 0 :(得分:-1)

public class DbHelper extends SQLiteOpenHelper {

    // The Android's default system path of your application database.
    private static String PACKAGENAME = "com.xyz";
    private static String DB_PATH = "/data/data/" + PACKAGENAME + "/databases/";
    public static String DB_NAME = "abc.db";
    public static int DELETED = 1;
    public static int UPDATED = 2;
    public static int NEW_RECORD = 3;

    private static final String TAG = "DbHelper";
    private SQLiteDatabase myDataBase;

    private final Context myContext;

    /**
     * Constructor Takes and keeps a reference of the passed context in order to
     * access to the application assets and resources.
     * 
     * @param context
     */
    public DbHelper(final 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 final void createDataBase() throws IOException {

        final boolean dbExist = checkDataBase();
        SQLiteDatabase db_Read = null;
        if (dbExist) {
             String create_t_user
                = "CREATE TABLE IF NOT EXISTS user ("
                + "ID INTEGER PRIMARY KEY AUTOINCREMENT,"
                + "username varchar(20) NOT NULL,"
                + "password varchar(20) NOT NULL,"
                + "connected INTEGER(0) NOT NULL);";
        String create_t_friend_request
                = "CREATE TABLE IF NOT EXISTS friend_request ("
                + "ID INTEGER PRIMARY KEY AUTOINCREMENT,"
                + "username varchar(20) NOT NULL,"
                + "ID_last_message INTEGER NOT NULL);";

        String create_t_friend
                = "CREATE TABLE IF NOT EXISTS friend ("
                + "ID INTEGER PRIMARY KEY AUTOINCREMENT,"
                + "username varchar(20) NOT NULL," 
                + "connected INTEGER(0) NOT NULL);";

        String create_t_message
                = "CREATE TABLE IF NOT EXISTS message ("
                + "ID INTEGER PRIMARY KEY AUTOINCREMENT,"
                + "ID_friend INTEGER NOT NULL,"
                + "Sent bit(1) NOT NULL,"
                + "Body text NOT NULL,"
                + "DATE datetime NOT NULL);";
        //execution des requetes
        db_Read .execSQL(create_t_user);
        db_Read .execSQL(create_t_friend_request);
        db_Read .execSQL(create_t_friend);
        db_Read .execSQL(create_t_message);
        } 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.
            // 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.
            // db_Read = this.getReadableDatabase(DB_Internal);
            db_Read = this.getReadableDatabase();
            db_Read.close();

            copyDataBase();

        }
    }

    /**
     * Restore whole database without any data
     * 
     * @throws IOException
     */
    public final void RestoreDatabase() throws IOException {
        SQLiteDatabase db_Read = this.getReadableDatabase();
        db_Read.close();

        copyDataBase();
        Log.i(TAG, "Database REstored");
    }

    /**
     * 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() {
        final File dbFile = new File(DB_PATH + DB_NAME);
        return dbFile.exists();
    }

    /**
     * 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.
     * 
     * @throws IOException
     * */
    private void copyDataBase() throws IOException {

        // Open your local db as the input stream
        final InputStream myInput = myContext.getAssets().open(DB_NAME);

        // Path to the just created empty db
        final String outFileName = DB_PATH + DB_NAME;

        // Open the empty db as the output stream
        final OutputStream myOutput = new FileOutputStream(outFileName);

        // transfer bytes from the inputfile to the outputfile
        final 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 final SQLiteDatabase openDataBase() {
        // Open the database
        final String myPath = DB_PATH + DB_NAME;
        if (myDataBase != null && myDataBase.isOpen()) {
            myDataBase.close();
        }
        return myDataBase = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE);

    }

    public final synchronized void closeDatabase() {
        if (myDataBase != null)
            myDataBase.close();
        super.close();
    }

    @Override
    public void onCreate(final SQLiteDatabase arg0) {
    }

    @Override
    public void onUpgrade(final SQLiteDatabase arg0, final int arg1,
            final int arg2) {
    }

}

在你的代码中你没有把代码放到create database(从data / data / databases / ddd.db复制db)所以你必须得到这种类型的错误..

请尝试这个。我希望它对你有用..