Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
376 views
in Technique[技术] by (71.8m points)

android - How to insert data into the database file which is in the assets folder

I have created a SQLite file and putted it on to assets folder. Now I am able to read the data from that file but I don't have any idea how to write to this database file. I need to take the user name and score and store that in to that database file. I searched on the web and on the SO also but there are examples for the normal database insertion.

Can any one tell me how to store the values into database file. I need to store the username and score. Just think this to as Strings and please give me a example for it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should not create database in the assets folder as we can only read data from assets folder.

Infact you should create the database in the default internal folder i.e data/data/[package-name]/databases OR you also can create your database in the sdcard and can perform read-write operation but of course it will not run if there is no sdcard.

The following is the code for creating databse in sdcard:-

    private SQLiteDatabase db;
    private static Context cntxt;

    public static File filename = null;
    public static String DATABASE_FILE_PATH_EXTERNAL = null;

    public DBHelper(Context context) {
            super(context, DATABASE_NAME, null,1);
            cntxt = context;
            try{
                try{
                    filename = Environment.getExternalStorageDirectory();
                }catch(Exception e){
                    Toast.makeText(DbHelper.cntxt, "Please Insert SD card To create Database", Toast.LENGTH_LONG).show();
                    Log.e("Log",e.getMessage(),e.fillInStackTrace());
                }

                DATABASE_FILE_PATH_EXTERNAL = filename.getAbsolutePath()+File.separator+DATABASE_NAME;

                //  db = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH_EXTERNAL, null, SQLiteDatabase.OPEN_READWRITE + SQLiteDatabase.CREATE_IF_NECESSARY);
            }catch(Exception e){

                Log.e("Log",e.getMessage(),e.fillInStackTrace());
            }
        }



        @Override
        public synchronized SQLiteDatabase getWritableDatabase() {
            // TODO Auto-generated method stub
            try{
                db = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH_EXTERNAL, null, SQLiteDatabase.OPEN_READWRITE + SQLiteDatabase.CREATE_IF_NECESSARY);
                try{
                    onCreate(db);
                }catch(Exception e){
                    Log.e("Log",e.getMessage(),e.fillInStackTrace());
                }
                return db;
            }catch(Exception e){
                Log.e("Log",e.getMessage(),e.fillInStackTrace());
                if(db!=null)
                    db.close();
            }
            return db;
        }// End of getWritableDatabase()



**Inserting  and Retrieving data from database:-**


public void insertIntoTable(String[] userName,int[] score)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();

        try {
            db.beginTransaction();
            for (int i = 0; i < beatID.length; i++) {
                cv.put("UserName",userName[i]);
                cv.put("Score",score[i]);

                db.insert("TableName", "UserName",
                        cv);
            }
            db.setTransactionSuccessful();
        } catch (Exception ex) {

        } finally {
            db.endTransaction();
            db.close();
        }
    }


public void getUserNamePswd(){
        Cursor c = null;
        SQLiteDatabase db = null;
        try{
            db = this.getReadableDatabase();     
            c = db.rawQuery("Select UserName,Score from TableName",null);
            c.moveToFirst();
            String[] username = new String[c.getCount()];
            int[] score = new int[c.getCount()];            
            int counter = 0;
            c.moveToFirst();
            while(!c.isAfterLast()){
            username[counter] = c.getString(0);
            score[counter] = c.getInt(1);
            c.moveToNext();
            counter++;
            }


        }catch(Exception e){
            Log.e("Log", e.getMessage(), e.fillInStackTrace());
            return null;      
        }finally{
            c.close();
            db.close();
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...