English 中文(简体)
将数据输入方案和roid
原标题:Populate database data into program android

我实际上打算利用我方位的Browser在我的roid方案中建立了数据库。 我通过一些文章读一读,所有发言者都说,使用道路/数据/数据/数据/结果。 我感到困惑不解,我无法在我的档案中找到问题。 我没有数据夹,或者我是否自行制作?

我将记录放在记录中,以便跟踪方案自动终止的情况,并在数据库开放后终止,并声称我的数据库“有食品表”。 我检查了这一数据,它只是作为我数据库唯一表格的输出和roid数据。

My question is Why my database only have 1 table : android_metadata table only but no Food table? What Steps I have missed out?

This is my database info 2 tables Table 1 : Food (_id,name, price) Table 2 : android_metadata

我实际上遵循这一联系::http://www.reign Design.com/blog/using-your-own-sqlite-database-in-android-applications/

这是我处理数据库的守则。

    package com.restaurant.sesame;

    public class Restaurant {
    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "name";
    public static final String KEY_PRICE = "price";

    private static final String DATABASE_NAME ="Restaurantdb";
    private static final String DATABASE_TABLE ="Food";
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_PATH ="/data/data/com.restaurant.sesame/databases/";

    private static SQLiteDatabase ourDatabase;
    private static Context ourContext = null;
    private DBHelper ourHelper;

    private static class DBHelper extends SQLiteOpenHelper{


        /**
         * Constructor
         * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
         * @param context
         */
        public DBHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }   

      /**
         * Creates a empty database on the system and rewrites it with your own database.
         * */
        public void createDataBase() throws IOException{

            boolean dbExist = checkDataBase();

            if(dbExist){
                //do nothing - database already exist
            }else{

                //By calling this method and empty database will be created into the default system path
                   //of your application so we are gonna be able to overwrite that database with our database.
                this.getReadableDatabase();

                try {

                    copyDataBase();

                } catch (IOException e) {

                    throw new Error("Error copying database");

                }
            }

        }

        /**
         * Check if the database already exist to avoid re-copying the file each time you open the application.
         * @return true if it exists, false if it doesn t
         */
        private boolean checkDataBase(){

            SQLiteDatabase checkDB = null;

            try{
                String myPath = DATABASE_PATH + DATABASE_NAME;
                checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

            }catch(SQLiteException e){

                //database does t exist yet.

            }

            if(checkDB != null){

                checkDB.close();

            }

            return checkDB != null ? true : false;
        }

        /**
         * Copies your database from your local assets-folder to the just created empty database in the
         * system folder, from where it can be accessed and handled.
         * This is done by transfering bytestream.
         * */
        private void copyDataBase() throws IOException{

            //Open your local db as the input stream
            InputStream myInput = ourContext.getAssets().open(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;
            ourDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

        }

        @Override
        public synchronized void close() {

                if(ourDatabase != null)
                    ourDatabase.close();

                super.close();

        }

        @Override
        public void onCreate(SQLiteDatabase db) {

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        }

            // Add your public helper methods to access and get content from the database.
           // You could return cursors by doing "return ourDatabase.query(....)" so it d be easy
           // to you to create adapters for your views.

    }

    public Restaurant(Context c)
    {   
        ourContext = c;
    }

    public Restaurant open() throws SQLException{
        ourHelper = new DBHelper(ourContext);

        try {
            ourHelper.createDataBase();
        } catch (IOException ioe) {
            throw new Error("Unable to create database");
        }

        try {
            ourHelper.openDataBase();
        }catch(SQLException sqle){
            throw sqle;
        }
        return this;
    }

    public void close()
    {
        ourHelper.close();
    }

    public String getData()
    {
        Log.w("my app", "IN getData TOP!!!!");
        String [] columns = new String [] {KEY_ROWID,KEY_NAME,KEY_PRICE};   
        Log.w("my app", "DDDDDDDDD");
      /*  to test what table in my database
        Cursor c1 = ourDatabase.rawQuery("SELECT name FROM sqlite_master WHERE type =  table " , null);
        String result1="";
        int i =0;
        if(c1!= null){
            c1.moveToFirst();
            result1 =result1 + c1.getString(i);
            Log.w("my app", result1);
            i++;
        }
        Log.w("my app", "AAAAAAAAAA");
        return result1;
        */
             Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
        String result ="";
        int iRow = c.getColumnIndex(KEY_ROWID);
        int iName = c.getColumnIndex(KEY_NAME);
        int iPrice = c.getColumnIndex(KEY_PRICE);
        Log.w("my app", "IN getData MIDDLE!!!!");
        for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
        {
            result = result + c.getString(iRow) +"	" +
                              c.getString(iName) + "	" +
                              c.getString(iPrice) + "
";
        }
        Log.w("my app", "IN getData END!!!!");
        return result;

    }
}
问题回答

如果你提供预填的<代码>your_app.db文档,作为数据库(而不是在启动时使用所有INSERT<>/code>的报表,或只使用用户生成的数据),则你需要将数据库作为原始资源列入其中,而这种数据库看看似有多少链接指示你这样做。 自<代码>以来,我猜测你的数据库复印件的工作是正确的。 DBHelper 课程将创建<代码>android_metadata。 如果已经存在,则表。

这一数据库是否只读一读? 如果是的话,你就应当能够从你的原始资源中打开。 你们甚至有机会在这里写这封信,但我不相信,如果允许的话。





相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签