引用存储在assets文件夹(android)中的数据库文件的表

时间:2011-12-09 07:47:30

标签: android sqlite android-assets

我是SQL新手。我有一个使用Sqliteman浏览器创建的mac-addresses数据库。它是一个简单的数据库,只有一个名为“access_points”的表。名为macad.db的数据库位于我的资产文件夹中。我有一个应用程序,用于从edittextview向表access_points插入新的mac-addresses。我的数据库适配器具有以下初始化:

public static String DATABASE_NAME="macad.db";
public static String DATABASE_PATH= "/data/data/com.example.myproject/databases/";
public static String LOCAL_DATABASE_NAME="macad.db";

我的表(access_points)初始化的值应该是什么。请用你的答案替换问号。

public static String DATABASE_TABLE= ????;

另外我在DB Adapter中的insertmac方法如下所示。我如何引用存储在assets文件夹中的macad.db表“access_points”?请在下面提供的db.insert()语句中用您的答案替换问号。

public long insertmac(String mac) 
    {
        ContentValues macValue = new ContentValues();
        macValue.put("macaddress1", macaddress1);
        return db.insert(????, null, macValue);
    }

还有什么办法可以确保,如果在access_points表中已经存在一个mac-address,它不会再次放入表中?在此先感谢任何解决方案。我的DBAdapter类如下:

public class DbAdapternew {
    private static final String TAG = DbAdapter.class.getName();
    //private DatabaseHelper mDbHelper;
    public static SQLiteDatabase myDb;
    public static String DATABASE_NAME="macad.db";
    public static String DATABASE_TABLE="access_points";
    public static String DATABASE_PATH= "/data/data/com.example.myproject/databases/";
    public static String LOCAL_DATABASE_NAME="macad.db";
    private static final int DATABASE_VERSION = 2;
    private static Context myContext;
    public static final String KEY_MAC = "mac_add"; // column 1
    public static final String KEY_floor = "floor_id";  // column 2
    public static final String KEY_build= "building_id"; //column 3
    public static final String KEY_zone = "zone_id";      // column 4 
    private DatabaseHelper DBHelper;
    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!!!!!");
            onCreate(db);
        }

        public void createDataBase() throws IOException {
            Log.i(TAG,"DB NAME : "+DATABASE_NAME);
            Log.i(TAG,"DB PATH : "+DATABASE_PATH);
            Log.i(TAG,"LOCAL DB NAME : "+LOCAL_DATABASE_NAME);


            boolean dbExist = checkDataBase();
            if (dbExist) {
            } else {
                this.getReadableDatabase();
                try {
                    copyDataBase();
                } catch (IOException e) {
                    //throw new Error("Error copying database");
                }
            }
        }

        public SQLiteDatabase getDatabase() {
            String myPath = DATABASE_PATH + DATABASE_NAME;
            return SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READONLY);
        }

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

        private void copyDataBase() throws IOException {

            Log.i(TAG,"LOCAL DB NAME : "+LOCAL_DATABASE_NAME);
            // Open your local db as the input stream
            InputStream myInput = myContext.getAssets().open(LOCAL_DATABASE_NAME);

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

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

        public void openDataBase() throws SQLException {
            // Open the database
            String myPath = DATABASE_PATH + DATABASE_NAME;
            myDb = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READWRITE);
        }

        @Override
        public synchronized void close() {

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

            super.close();

        }
    }


    public DbAdapternew(Context ctx) {
        this.myContext = ctx;
        DBHelper = new DatabaseHelper(ctx);
    }

    public DbAdapternew open() throws SQLException {
        myDb = DBHelper.getWritableDatabase();

        return this;

    }

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

    public Cursor fetchAllData(String table) {

        try {
            myDb.rawQuery("SELECT * FROM " + table, null);
            Log.i(TAG, "Row Collected");
        } catch (SQLiteException e) {
            myDb.execSQL("CREATE TABLE IF NOT EXISTS " + table
                    + " (mac_add VARCHAR)");
            Log.i(TAG, "TABLE Not Found");
        }
        return myDb.rawQuery("SELECT * FROM " + table, null);
    }

    public Cursor fetchData(String sqlQuery) {

        try {
            myDb.rawQuery(sqlQuery, null);
            Log.i(TAG, "Query Executed");
        } catch (SQLiteException e) {
            Log.i(TAG, e.toString());
        }
        return myDb.rawQuery(sqlQuery, null);
    }

    public void executeQuery(String sql) {
        myDb.execSQL(sql);
    }
     public long insertrow(String editmac) 
        {
            ContentValues initialValues = new ContentValues();
            initialValues.put(KEY_MAC, editmac.toString());  //for column 1 in macad.db
            initialValues.put(KEY_floor, "uf");            //for column 2 
            initialValues.put(KEY_build, "ub");            //for column 3
            initialValues.put(KEY_zone, "uz");             //for column 4
            return myDb.insert(DATABASE_TABLE, null, initialValues);
        }

}

在我的活动中有一个按下更新按钮,这将导致将edittextview中键入的mac-address放入macad.db。代码是:

updzonedb.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                db.open();
                try
                {

                  db.insertrow(editmac.getText().toString());   


                 }
                catch (Exception ex)
                {

                  }
                db.close();
            }
          });

1 个答案:

答案 0 :(得分:0)

您是否已将数据库.db文件从资产复制到data/data/<package_name>databases目录?

如果,那么在使用SQLIte Manager创建时,继续使用数据库中的相同表名。

如果,请仔细阅读此问题How to ship an Android application with a database?

public static String DATABASE_TABLE= "access_points";

public long insertmac(String mac) 
    {
        ContentValues macValue = new ContentValues();
        macValue.put("macaddress1", macaddress1);
        return db.insert(DATABASE_TABLE, null, macValue);
    }