Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中访问SQLite数据库时出错_Android_Sqlite - Fatal编程技术网

在android中访问SQLite数据库时出错

在android中访问SQLite数据库时出错,android,sqlite,Android,Sqlite,我已经尝试了很多关于如何在android项目中显示资产文件夹中的数据的代码,但我无法做到这一点。请帮我摆脱这个问题 谢谢你的帮助 编辑:- 我的适配器类 import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.content.Context;

我已经尝试了很多关于如何在android项目中显示资产文件夹中的数据的代码,但我无法做到这一点。请帮我摆脱这个问题

谢谢你的帮助 编辑:-
我的适配器类

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;

    import android.content.Context;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteException;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.util.Log;

    public class DBAdapter extends SQLiteOpenHelper{
        private static String DB_PATH = "/data/data/com.example.mynwedb/databases/wbty";

        private static String DB_NAME = "wbty";

        private SQLiteDatabase myDataBase;

        private final Context myContext;

        public DBAdapter(Context context) 
        {
            super(context, DB_NAME, null, 1);
            this.myContext = context;
        }

        public void createDataBase() throws IOException{
            boolean dbExist = checkDataBase();

            if(dbExist)
            {
                Log.i("DB....", "database available....");
            }
            else
            {
                this.getWritableDatabase();

                try {

                    copyDataBase();

                } catch (IOException e) {

                    throw new Error("Error copying database");

                }

                Log.i("DB..", "database created.....");
            }   

        }


        public boolean checkDataBase(){

            SQLiteDatabase checkDB = null;

            try{

                String myPath = DB_PATH + DB_NAME;

                checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

            }catch(SQLiteException e){

                Log.e("CheckDb","DB not found");
                //database does't exist yet.

                if(checkDB != null){

                    checkDB.close();

                }
            }
            finally
            {
                if(checkDB != null){

                    checkDB.close();

                } 
                this.close();
            }
            return checkDB != null ? true : false;

        }




        private void copyDataBase() throws IOException{

            InputStream myInput = myContext.getAssets().open(DB_NAME);

            String outFileName = DB_PATH + DB_NAME;

            OutputStream myOutput = new FileOutputStream(outFileName);
            byte[] buffer = new byte[1024];

            int length;

            while ((length = myInput.read(buffer))>0){

                myOutput.write(buffer, 0, length);

            }

            myOutput.flush();

            myOutput.close();

            myInput.close();

        }

        public SQLiteDatabase openDataBase() throws SQLException{

            String myPath = DB_PATH + DB_NAME;

            return myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

        }


        @Override

        public synchronized void close() {

            if(myDataBase != null)

                myDataBase.close();

            super.close();

        }

        @Override

        public void onCreate(SQLiteDatabase db) {

        }

        @Override

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


        }  


        public void getData()
        {
            SQLiteDatabase myDB ;
            Cursor cursor ;
            try {

                myDB=this.openDataBase();                   

                cursor=myDB.rawQuery("SELECT * FROM descriptions",null);


                if (cursor != null ) {
                    if  (cursor.moveToFirst()) {
                        do {

                            // put your code to get data from cursor                      

                        }while (cursor.moveToNext());
                    }

                }



                if(cursor != null)
                {
                    myDB.close();
                    cursor.close();
                }                      
            }catch(SQLException sqle){

                throw sqle;

            }


        }
    } 
和MyActivity类:-

import java.io.IOException;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DBAdapter myDbHelper = new DBAdapter(MainActivity.this); 
        try 
        {
            myDbHelper.createDataBase();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        finally
        {
            myDbHelper.close();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

我已将db文件保存在资产文件夹中。

您会遇到哪种错误?请告诉我们更多关于您的问题,并张贴日志。这不应该是一个答案,但我需要解释一下你做错了什么:

您为outputFile编写了:

       String outFileName = DB_PATH + DB_NAME;
但这将为您提供以下路径:

      "/data/data/com.example.mynwedb/databases/wbtywbty";
因为您的
DB\u路径是

      "/data/data/com.example.mynwedb/databases/wbty";
您的
DB_NAME
是:

      "wbty";
如果我们得到更多的信息,我会尽力帮助你

编辑 在
checkDatabase()中相同:


向我们显示代码的相关部分。要从资产文件夹或使用sqlite数据库获取数据,请在此处粘贴一些代码。不管你做了什么。想从资产文件夹中获取数据谢谢你的回答,实际上我正在获取我的db文件,但是我在访问数据时卡住了。请帮忙…好的,你可以发布日志,看看你得到的是哪种错误吗?我没有在日志中得到任何错误。我正在获取我的db文件,但我无法从中获取数据。好的,很抱歉再次提出这个问题,但我需要了解更多。1.copyDataBase()是否成功?2.您要从哪个数据库获取原始数据库还是复制数据库?3.您的问题是不知道如何获取数据,因为getData();没有函数,没有获取数据的代码?实际上我想从原始数据中获取数据,并且我想显示数据。
     String myPath = DB_PATH + DB_NAME;