从Android电子存储器读取SQLite数据库

从Android电子存储器读取SQLite数据库

本文介绍了从Android电子存储器读取SQLite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个Java应用程序的SQLite数据库,并将它推到我的机器人。我想读这个数据库(可能写入到后来太)。最让我发现到目前为止创建了Android中的DB的教程...

I have created an sqlite database on a Java application and have pushed it to my android. I want to read this database (probably write into it later too). Most of the tutorials I have found so far create the DB within the android...

我不认为我需要覆盖的onCreate()和的onUpdate()SQLiteOpenHelper类的方法,是吧?此外,在下面的教程中,他创建的表,因为我已经有我的数据库中的一部分

I don't think I need to override the onCreate() and onUpdate() methods of SQLiteOpenHelper class do I ? Also the part that in the below tutorial he has created the tables, since I already have my database

任何教程,样本code u人可能知道的?

Any tutorial, sample code u guys might know of ?

public class DatabaseHandler extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "contactsManager";

    // Contacts table name
    private static final String TABLE_CONTACTS = "contacts";

    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_PH_NO = "phone_number";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
                + KEY_PH_NO + " TEXT" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

        // Create tables again
        onCreate(db);
    }

我只需要打开DB我在记忆中,执行一个SQL查询agianst我的数据库并得到结果。

I Simply need to open the DB I have in the memory, execute an sql query agianst my DB and get the result.

推荐答案

您必须在设备上拷贝,从资产到您的应用程序存储区中的DB(或其他可访问的位置),然后才能使用它。你不能用它直接出了.apk文件的。

You must copy the db from assets to your apps storage area (or another accessible location) on the device before you can use it. You cannot use it directly out of the .apk file.

如何去做一个例子:

public class DBAdapter {

// DB info
public static final String MAIN_DATABASE_NAME = "yourDB";
public static String MAIN_DB_PATH = "/data/data/your.package.name/databases/";
public static final int MAIN_DATABASE_VERSION = 1;

// database control
private DatabaseHelper mDbHelper;
private static SQLiteDatabase mDb;
private static Context mCtx;

private static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context, String dbname, int dbversion) {
        super(context, dbname, null, dbversion);
        if (checkDataBase(dbname)) {
            openDataBase(dbname);
        } else {
            try {
                this.getReadableDatabase();
                copyDataBase(dbname);
                this.close();
                openDataBase(dbname);
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
            Toast.makeText(context,
                    "Initial " + dbname + " database has been created",
                    Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    }

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

public DBAdapter(Context ctx) {
    DBAdapter.mCtx = ctx;
}

public DBAdapter open(String dbname, int dbversion) throws SQLException {
    mDbHelper = new DatabaseHelper(mCtx, dbname, dbversion);
    mDb = mDbHelper.getWritableDatabase();
    return this;
}

public void close() {
    mDbHelper.close();
}

private static void copyDataBase(String dbname) throws IOException {
    InputStream myInput = mCtx.getAssets().open(dbname);
    String outFileName = MAIN_DB_PATH + dbname;
    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();
}

private static boolean checkDataBase(String dbname) {
    SQLiteDatabase checkDB = null;
    boolean exist = false;
    try {
        String db = MAIN_DB_PATH + dbname;
        checkDB = SQLiteDatabase.openDatabase(db, null,
                SQLiteDatabase.OPEN_READONLY);
    } catch (SQLiteException e) {
        Log.v("db log", "database does't exist");
    }
    if (checkDB != null) {
        exist = true;
        checkDB.close();
    }
    return exist;
}

private static void openDataBase(String dbname) throws SQLException {
    String dbPath = MAIN_DB_PATH + dbname;
    mDb = SQLiteDatabase.openDatabase(dbPath, null,
            SQLiteDatabase.OPEN_READWRITE);
}
}

这篇关于从Android电子存储器读取SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 14:52