如何在我的Android程序中的SQLite Manager应用程序中使用我创建的SQLite数据库

时间:2013-03-13 16:30:47

标签: android sqlite navicat sql-manager

当我在Android应用程序中处理许多表时,我决定在SQLite管理器中创建数据库(DB)(例如firefox插件或navicat for sqlite)。我完成了创建它们并导出.sql文件!但我不知道如何使用这个文件! 当然我发现了一些关于这个的帖子把DB(在一个root的手机上)放在... data / data / {你的包名} / ..但是我不知道在这一步之后要做什么!! (我知道将db引入Android应用程序的代码!!)

  • 任何人都可以一步一步告诉我如何在我的应用程序中使用我创建的数据库这些方法?? -

  • 这是使用这些SQLite管理器的好方法,或者尽管使用代码在android中创建多表很复杂,你更喜欢它吗?

  • sqlite 2和3之间的主要区别是什么?是否支持不同的Android版本或只支持Sqlite的更新?

编辑:将dB复制到assest文件夹并使用代码进行复制后:


03-16 11:32:20.370: I/SqliteDatabaseCpp(3999): sqlite returned: error code = 14, msg = cannot open file at line 27712 of [8609a15dfa], db=/data/data/com.androidhive.xmlparsing/databases/surveydb.sqlite
03-16 11:32:20.370: I/SqliteDatabaseCpp(3999): sqlite returned: error code = 14, msg = os_unix.c: open() at line 27712 - "" errno=2 path=/data/data/com.androidhive.xmlparsing/databases/surveydb.sqlite, db=/data/data/com.androidhive.xmlparsing/databases/surveydb.sqlite
03-16 11:32:20.370: E/SqliteDatabaseCpp(3999): sqlite3_open_v2("/data/data/com.androidhive.xmlparsing/databases/surveydb.sqlite", &handle, 1, NULL) failed
03-16 11:32:20.380: E/SQLiteDatabase(3999): Failed to open the database. closing it.
03-16 11:32:20.380: E/SQLiteDatabase(3999): android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file
03-16 11:32:20.380: E/SQLiteDatabase(3999):     at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
03-16 11:32:20.380: E/SQLiteDatabase(3999):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:983)
03-16 11:32:20.380: E/SQLiteDatabase(3999):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:956)
03-16 11:32:20.380: E/SQLiteDatabase(3999):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:932)
03-16 11:32:20.380: E/SQLiteDatabase(3999):     at database.AssetDatabaseHelper.checkExist(AssetDatabaseHelper.java:55)
03-16 11:32:20.380: E/SQLiteDatabase(3999):     at database.AssetDatabaseHelper.importIfNotExist(AssetDatabaseHelper.java:81)
03-16 11:32:20.380: E/SQLiteDatabase(3999):     at com.androidhive.xmlparsing.AndroidXMLParsingActivity.onCreate(AndroidXMLParsingActivity.java:78)
03-16 11:32:20.380: E/SQLiteDatabase(3999):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
03-16 11:32:20.380: E/SQLiteDatabase(3999):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1782)
03-16 11:32:20.380: E/SQLiteDatabase(3999):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1834)
03-16 11:32:20.380: E/SQLiteDatabase(3999):     at android.app.ActivityThread.access$500(ActivityThread.java:122)
03-16 11:32:20.380: E/SQLiteDatabase(3999):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1027)
03-16 11:32:20.380: E/SQLiteDatabase(3999):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 11:32:20.380: E/SQLiteDatabase(3999):     at android.os.Looper.loop(Looper.java:132)
03-16 11:32:20.380: E/SQLiteDatabase(3999):     at android.app.ActivityThread.main(ActivityThread.java:4126)
03-16 11:32:20.380: E/SQLiteDatabase(3999):     at java.lang.reflect.Method.invokeNative(Native Method)
03-16 11:32:20.380: E/SQLiteDatabase(3999):     at java.lang.reflect.Method.invoke(Method.java:491)
03-16 11:32:20.380: E/SQLiteDatabase(3999):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
03-16 11:32:20.380: E/SQLiteDatabase(3999):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
03-16 11:32:20.380: E/SQLiteDatabase(3999):     at dalvik.system.NativeStart.main(Native Method)
03-16 11:32:20.380: W/System.err(3999): android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file
03-16 11:32:20.380: W/System.err(3999):     at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
03-16 11:32:20.380: W/System.err(3999):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:983)
03-16 11:32:20.380: W/System.err(3999):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:956)
03-16 11:32:20.380: W/System.err(3999):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:932)
03-16 11:32:20.380: W/System.err(3999):     at database.AssetDatabaseHelper.checkExist(AssetDatabaseHelper.java:55)
03-16 11:32:20.380: W/System.err(3999):     at database.AssetDatabaseHelper.importIfNotExist(AssetDatabaseHelper.java:81)
03-16 11:32:20.380: W/System.err(3999):     at com.androidhive.xmlparsing.AndroidXMLParsingActivity.onCreate(AndroidXMLParsingActivity.java:78)
03-16 11:32:20.380: W/System.err(3999):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
03-16 11:32:20.380: W/System.err(3999):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1782)
03-16 11:32:20.380: W/System.err(3999):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1834)
03-16 11:32:20.380: W/System.err(3999):     at android.app.ActivityThread.access$500(ActivityThread.java:122)
03-16 11:32:20.380: W/System.err(3999):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1027)
03-16 11:32:20.380: W/System.err(3999):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 11:32:20.380: W/System.err(3999):     at android.os.Looper.loop(Looper.java:132)
03-16 11:32:20.380: W/System.err(3999):     at android.app.ActivityThread.main(ActivityThread.java:4126)
03-16 11:32:20.380: W/System.err(3999):     at java.lang.reflect.Method.invokeNative(Native Method)
03-16 11:32:20.380: W/System.err(3999):     at java.lang.reflect.Method.invoke(Method.java:491)
03-16 11:32:20.380: W/System.err(3999):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
03-16 11:32:20.380: W/System.err(3999):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
03-16 11:32:20.380: W/System.err(3999):     at dalvik.system.NativeStart.main(Native Method)

我使用了这段代码:

public class AssetDatabaseHelper extends SQLiteOpenHelper{

 private String dbName;
    private String db_path;
    private Context context;

    /**
     * A helpe r class to import db files.
     * 
     * @param context
     *  base/app context
     * @param dbName
     *            The name of the db in asset folder .
     */
    public AssetDatabaseHelper(Context context, String dbName) {
        super(context, dbName, null, 1);
        this.dbName = dbName;
        this.context = context;
        db_path = "/data/data/" + context.getPackageName() + "/databases/";
    }

    /**
     * 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
     */
    public boolean checkExist() {

        SQLiteDatabase checkDB = null;

        try {
            String myPath = db_path + dbName;
            checkDB = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READONLY);

        } catch (SQLiteException e) {
            e.printStackTrace();


        } catch (Exception ep) {
            ep.printStackTrace();
        }

        if (checkDB != null) {

            checkDB.close();

        }

        return checkDB != null ? true : false;
    }

    /**
     * Creates a empty database on the system and rewrites it with your own
     * database.
     * */
    public void importIfNotExist() throws IOException {

        boolean dbExist = checkExist();

        if (dbExist) {
            // do nothing - database already exist
        } else {
            this.getReadableDatabase();

            try {

                copyDatabase();

            } catch (IOException e) {

                throw new Error("Error copying database");

            }
        }

    }
    /**
     * Creates copies the database from asset to the new database location.
     * */
    private void copyDatabase() throws IOException {
        InputStream is = context.getAssets().open(dbName);

        OutputStream os = new FileOutputStream(db_path + dbName);

        byte[] buffer = new byte[4096];
        int length;
        while ((length = is.read(buffer)) > 0) {
            os.write(buffer, 0, length);
        }
        os.flush();
        os.close();
        is.close();
        this.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    }


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


}

更新2:db sCRIPTS:

CREATE TABLE "**" ("_id" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , "**" TEXT, "****" INTEGER);
CREATE TABLE "***" ("_id" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , "***" INTEGER UNIQUE , "***" TEXT);
CREATE TABLE "**" ("_id" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , "***" INTEGER UNIQUE , "**" INTEGER, "**" INTEGER, "**" TEXT, "impGender" TEXT, "**" TEXT, "***" INTEGER, "***" DATETIME DEFAULT CURRENT_TIMESTAMP);
CREATE TABLE "**" ("_id" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , "**" TEXT);
CREATE TABLE "**" ("_id" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , "***" INTEGER UNIQUE , "***" TEXT, "srQType" INTEGER, "***" INTEGER, "**" BOOL, "**" FLOAT, "**" FLOAT, "**" INTEGER, "**"  UNIQUE );
CREATE TABLE "*" ("_id" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , "**" INTEGER NOT NULL  UNIQUE , "***" INTEGER, "**" INTEGER, "**" INTEGER, "**" INTEGER, "**" TEXT, "**" TEXT);
CREATE TABLE "**" ("_id" INTEGER PRIMARY KEY, "***" TEXT);
CREATE TABLE "**" ("_id" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , "**" TEXT, "srDesc" TEXT, "***" INTEGER, "**" DATETIME, "**" DATETIME, "****" INTEGER, "srCode" INTEGER);
CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US');

最后:

new AssetDatabaseHelper(this , "surveydb.sqlite").importIfNotExist();

问题是什么?

你能帮助我吗?

1 个答案:

答案 0 :(得分:2)

  • Android支持Sqlite 3.
  • 您不需要root电话来使用导入的数据库

使用导入数据库的步骤:

  1. 将数据库转储到/ assets文件夹中。
  2. 添加git hub here
  3. 中的代码
  4. 执行以下代码导入数据库。

        AssetDatabaseHelper dbHelper = new AssetDatabaseHelper(
            getBaseContext(), YOUR_DATABASE_NAME);
    try {
        dbHelper.importIfNotExist();
    } catch (IOException e) {
        e.printStackTrace();
    }