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

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

问题描述

我创建了一个SQLite的文件,并推它资产的文件夹。现在,我可以从文件中读取数据,但我没有任何想法如何写入该数据库文件。我需要的用户名和得分,并在存储到该数据库文件。我搜索的网站上和也是如此,但也有例子为普通的数据库插入。

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.

推荐答案

您不应该创建数据库中的资产的文件夹中,我们只能读取资产的文件夹数据。

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

逸岸,你应该创建数据库的默认内部文件夹即数据/数据​​/ [包名称] /数据库,或者您也可以在SD卡创建数据库,并可以进行读写操作但如果没有的SD卡,当然它不会运行

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.

以下是code在SD卡创建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天全站免登陆