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



我是SQL新手。我有一个mac地址的数据库,我使用Sqliteman浏览器创建。它是一个简单的数据库,只有一个名为"access_points"的表。名为macad.db的数据库位于我的assets文件夹中。我有一个应用程序,从edittextview插入新的mac地址到表access_points。我的DB适配器有以下初始化:

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适配器中的insertmac方法。我如何参考我的表"access_points"macad.db存储在资产文件夹?请将下列db.insert()语句中的问号替换为您的答案。

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

还有什么方法可以确保,如果mac地址已经存在于access_points表中,它不会再次放在表中?提前感谢任何解决方案。我的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地址到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();
            }
          });

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

如果,则继续使用与使用SQLIte管理器创建数据库时相同的表名。

如果NO然后通过这个SO问题如何与数据库一起发布Android应用程序?

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);
    }

相关内容

  • 没有找到相关文章

最新更新