什么是现有SQLite数据库的完整Android数据库帮助器类?

前端之家收集整理的这篇文章主要介绍了什么是现有SQLite数据库的完整Android数据库帮助器类?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图使用现有的sqlite数据库部署一个应用程序.

我已经阅读并尝试在线实现几个示例,但是我发现它们总是缺少一些代码,也不会编辑或者像广告一样工作.

有没有人有一个完整的Android数据库助手类在Android上部署现有的sqlite数据库

这是我想出来的,希望能帮助那些有麻烦的人.
package com.MyPackage;

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

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;
import android.util.Log;

public class AnyDBAdapter {

    private static final String TAG = "AnyDBAdapter";
    private DatabaseHelper mDbHelper;
    private static sqliteDatabase mDb;

    //make sure this matches the 
    //package com.MyPackage;
    //at the top of this file
    private static String DB_PATH = "/data/data/com.MyPackage/databases/";

    //make sure this matches your database name in your assets folder
    // my database file does not have an extension on it 
    // if yours does
    // add the extention
    private static final String DATABASE_NAME = "data";

    //Im using an sqlite3 database,I have no clue if this makes a difference or not
    private static final int DATABASE_VERSION = 3;

    private final Context adapterContext;

    public AnyDBAdapter(Context context) {
        this.adapterContext = context;
    }

    public AnyDBAdapter open() throws sqlException {
        mDbHelper = new DatabaseHelper(adapterContext);

        try {
            mDbHelper.createDataBase();
        } catch (IOException ioe) {
            throw new Error("Unable to create database");
        }

        try {
            mDbHelper.openDataBase();
        } catch (sqlException sqle) {
            throw sqle;
        }
        return this;
    }
    //Usage from outside
    // AnyDBAdapter dba = new AnyDBAdapter(contextObject); //in my case contextObject is a Map
    // dba.open();
    // Cursor c = dba.ExampleSelect("Rawr!");
    // contextObject.startManagingCursor(c);
    // String s1 = "",s2 = "";
    // if(c.moveToFirst())
    // do {
    //  s1 = c.getString(0);
    //  s2 = c.getString(1);
    //  } while (c.moveToNext());
    // dba.close();
    public Cursor ExampleSelect(string myVariable)
    {
        String query = "SELECT locale,? FROM android_Metadata";
        return mDb.rawQuery(query,new String[]{myVariable});
    }

    //Usage
    // AnyDBAdatper dba = new AnyDBAdapter(contextObjecT);
    // dba.open();
    // dba.ExampleCommand("en-CA","en-GB");
    // dba.close();
    public void ExampleCommand(String myVariable1,String myVariable2)
    {
        String command = "INSERT INTO android_Metadata (locale) SELECT ? UNION ALL SELECT ?";
        mDb.execsql(command,new String[]{ myVariable1,myVariable2});
    }

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

    private static class DatabaseHelper extends sqliteOpenHelper {

        Context helperContext;

        DatabaseHelper(Context context) {
            super(context,DATABASE_NAME,null,DATABASE_VERSION);
            helperContext = context;
        }

        @Override
        public void onCreate(sqliteDatabase db) {
        }

        @Override
        public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
            Log.w(TAG,"Upgrading database!!!!!");
            //db.execsql("");
            onCreate(db);
        }

        public void createDataBase() throws IOException {
            boolean dbExist = checkDataBase();
            if (dbExist) {
            } else {

                //make sure your database has this table already created in it
                //this does not actually work here
                /*
                 * db.execsql("CREATE TABLE IF NOT EXISTS \"android_Metadata\" (\"locale\" TEXT DEFAULT 'en_US')"
                 * );
                 * db.execsql("INSERT INTO \"android_Metadata\" VALUES ('en_US')"
                 * );
                 */
                this.getReadableDatabase();
                try {
                    copyDataBase();
                } catch (IOException e) {
                    throw new Error("Error copying database");
                }
            }
        }

        public sqliteDatabase getDatabase() {
            String myPath = DB_PATH + DATABASE_NAME;
            return sqliteDatabase.openDatabase(myPath,sqliteDatabase.OPEN_READONLY);
        }

        private boolean checkDataBase() {
            sqliteDatabase checkDB = null;
            try {
                String myPath = DB_PATH + DATABASE_NAME;
                checkDB = sqliteDatabase.openDatabase(myPath,sqliteDatabase.OPEN_READONLY);
            } catch (sqliteException e) {
            }
            if (checkDB != null) {
                checkDB.close();
            }
            return checkDB != null ? true : false;
        }

        private void copyDataBase() throws IOException {

            // Open your local db as the input stream
            InputStream myInput = helperContext.getAssets().open(DATABASE_NAME);

            // Path to the just created empty db
            String outFileName = DB_PATH + DATABASE_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,length);
            }

            // Close the streams
            myOutput.flush();
            myOutput.close();
            myInput.close();
        }

        public void openDataBase() throws sqlException {
            // Open the database
            String myPath = DB_PATH + DATABASE_NAME;
            mDb = sqliteDatabase.openDatabase(myPath,sqliteDatabase.OPEN_READWRITE);
        }

        @Override
        public synchronized void close() {

            if (mDb != null)
                mDb.close();

            super.close();

        }
    }

}
原文链接:https://www.f2er.com/sqlite/197814.html

猜你在找的Sqlite相关文章