0

I am new to SQL.I have a database of mac-addresses which i created using Sqliteman browser. It is a simple database with just one table named "access_points". The database named macad.db is in my assets folder.I have an application to insert new mac-addresses to the table access_points from an edittextview. My DB Adapter has following initializations:

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";

what should be the value for my table's(access_points) initialization. Kindly replace the question marks with your answer.

public static String DATABASE_TABLE= ????;

in addition my insertmac method in DB Adapter is given below. How would i refer to my table "access_points" of macad.db which is stored in assets folder? please replace the question marks with your answer in db.insert() statement provided below.

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

Also is there any way to ensure that, if a mac-address already exists in the access_points table it is not put again in the table?? Thanks in advance for any solution.my DBAdapter class is below:

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

}

In my activity there is an update button pressing which should result in putting the typed mac-address in edittextview into the macad.db. the code for this is:

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

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


                 }
                catch (Exception ex)
                {

                  }
                db.close();
            }
          });
kzs
  • 1,111
  • 5
  • 20
  • 35

1 Answers1

0

Have you copied your database .db file from asset to data/data/<package_name>databases directory?

If yes then go ahead use the same table name which is in your database when you created using SQLIte Manager.

If NO then go through this SO Question How to ship an Android application with a database?

public static String DATABASE_TABLE= "access_points";

and

public long insertmac(String mac) 
    {
        ContentValues macValue = new ContentValues();
        macValue.put("macaddress1", macaddress1);
        return db.insert(DATABASE_TABLE, null, macValue);
    }
Community
  • 1
  • 1
user370305
  • 108,599
  • 23
  • 164
  • 151
  • yes. Thanks. I think your solution is very close to what i want. As i try to put a new mac-address into macad.db though i dont get any error, i dont see the entered mac-address when i open the macad.db with Sqliteman browser afterwards. I have included my SQliteopenhelper class and the code for activity which tries to update macad.db on button click above. – kzs Dec 09 '11 at 11:21
  • my above comment is rather...:). , i guess the updated database is inside my app because i updated by button clicks as i ran the app. Is there any way to see this Database? – kzs Dec 09 '11 at 11:46
  • thanks cannot view data folder..may be because of the permission?i am using fedora.. – kzs Dec 09 '11 at 12:03
  • device...see this link: http://stackoverflow.com/questions/4935388/ddms-file-explorer-cant-access-data-data-htc-desire-hd – kzs Dec 09 '11 at 12:12