如何将数据插入到资产文件夹中的数据库文件中 [英] How to insert data into the database file which is in the assets folder

查看:22
本文介绍了如何将数据插入到资产文件夹中的数据库文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 SQLite 文件并将其放到 assets 文件夹中.现在我可以从该文件中读取数据,但我不知道如何写入该数据库文件.我需要获取用户名和分数并将其存储到该数据库文件中.我也在网上和 SO 上进行了搜索,但也有正常数据库插入的示例.

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.

推荐答案

你不应该在 assets 文件夹中创建数据库,因为我们只能从 assets 文件夹中读取数据.

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

事实上,你应该在默认的内部文件夹中创建数据库,即 data/data/[package-name]/databases 或者你也可以在 sdcard 中创建数据库并执行读写操作但是如果没有sdcard当然不会运行.

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.

以下是在sdcard中创建databse的代码:-

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

这篇关于如何将数据插入到资产文件夹中的数据库文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆