1

我正在尝试从资产创建数据库但是给我错误无法打开数据库但是在我再次运行后没有给出错误是完成第一次创建时运行给我错误我把我的 logcat 放在这里

07-04 18:28:00.860: E/Database(990): sqlite3_open_v2("/data/data/com.temp.find/databases/tempfind", &handle, 2, NULL) failed
07-04 18:28:00.870: W/System.err(990): android.database.sqlite.SQLiteException: unable to open database file
07-04 18:28:00.880: W/System.err(990):  at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
07-04 18:28:00.880: W/System.err(990):  at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1849)
07-04 18:28:00.880: W/System.err(990):  at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
07-04 18:28:00.880: W/System.err(990):  at com.food.find.RestaurantDatabase.checkDataBase(RestaurantDatabase.java:97)
07-04 18:28:00.890: W/System.err(990):  at com.food.find.RestaurantDatabase.createDataBase(RestaurantDatabase.java:60)
07-04 18:28:00.890: W/System.err(990):  at com.food.find.SplashScreenActivity.onCreate(SplashScreenActivity.java:25)
07-04 18:28:00.890: W/System.err(990):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-04 18:28:00.890: W/System.err(990):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-04 18:28:00.890: W/System.err(990):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-04 18:28:00.890: W/System.err(990):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-04 18:28:00.900: W/System.err(990):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-04 18:28:00.900: W/System.err(990):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-04 18:28:00.900: W/System.err(990):  at android.os.Looper.loop(Looper.java:123)
07-04 18:28:00.900: W/System.err(990):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-04 18:28:00.900: W/System.err(990):  at java.lang.reflect.Method.invokeNative(Native Method)
07-04 18:28:00.900: W/System.err(990):  at java.lang.reflect.Method.invoke(Method.java:507)
07-04 18:28:00.900: W/System.err(990):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-04 18:28:00.900: W/System.err(990):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-04 18:28:00.910: W/System.err(990):  at dalvik.system.NativeStart.main(Native Method)
07-04 18:28:01.030: E/running(990): database opened page size

餐厅数据库.java

public class RestaurantDatabase extends SQLiteOpenHelper{


    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_FOODADDRESS="address";
    private static final String KEY_FOODPHONE = "phonenumber";
    private static final String KEY_LATITUDE="latitude";
    private static final String KEY_LONGITUDE="longitude";



    private static final String TAG_LAT="lat";
    private static final String TAG_LONG="lng";
    private static final String TAG_RESPONSE = "responseData";
    private static final String TAG_TITLENOFORMATING = "titleNoFormatting";
    private static final String TAG_ADDRESSLINES="addressLines";
    private static final String TAG_PHONENUMBERS="phoneNumbers";
    private static final String TAG_NUMBER="number";    
    private static final String TAG_FOOD="food";
    private static String DB_PATH ="/data/data/com.temp.find/databases/";   
    private static String DB_NAME ="tempfind";   
    private SQLiteDatabase myDataBase; 
    private final Context myContext;    

    String query = "";

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

    public void createDataBase() throws IOException
    {

         boolean dbExist = checkDataBase();

         if(dbExist)
         {

         }
         else
         {


         this.getReadableDatabase();

         try
         {

             copyDataBase();

         } 
         catch (IOException e)
         {

             throw new Error("Error copying database");

         }

         }


    }


    private boolean checkDataBase()
    {

        SQLiteDatabase checkDB = null;

        try{
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

        }
        catch(SQLiteException e)
        {   
            e.printStackTrace();
        }

        if(checkDB != null)
        {
            checkDB.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);

        //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 RestaurantDatabase(Context context, String name, CursorFactory factory,int version) 
    {
        super(context, name, factory, version);
        this.myContext=context;
    }


        public void openDataBase() throws SQLException
        {     
                //Open the database
                String myPath = DB_PATH + DB_NAME;
                myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
                Log.e("running", "database opened page size");

        }

        @Override
        public synchronized void close() 
        {

            if(myDataBase != null)
                myDataBase.close(); 
            super.close();

        }



    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

    }

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


    }

    public String addContact(String title,String address,String phone,String latitude,String longitude) 
    {

        String check = null;

        Log.e("Title","--->"+title);
        Log.e("Address","-->"+address);
        Log.e("Number","-->"+phone);
        Log.e("Latitude","-->"+latitude);
        Log.e("Longitude","-->"+longitude);

        ContentValues values = new ContentValues();
        values.put(KEY_NAME,title); // Contact Name
        values.put(KEY_FOODADDRESS,address); // Contact Address
        values.put(KEY_FOODPHONE,phone); // Contact Phone
        values.put(KEY_LATITUDE,latitude); // Contact Latitude
        values.put(KEY_LONGITUDE,longitude); // Contact Longitude

        Log.e("addcontact called","--->");

        // Inserting Row
        myDataBase.insert(TAG_FOOD, null,values);       

        String selectQuery = " SELECT  * FROM " + TAG_FOOD ;

        Cursor cursor = myDataBase.rawQuery(selectQuery, null);

        Log.e("CURSOR RECORD","--->"+cursor.getCount());
        Log.e("INSERT RECORD","--->"+values);


        return check;


    }



}
4

2 回答 2

5

这段代码很可怕。我会首先删除整个事情并重新开始。

首先,如果您要在应用程序中打包数据库,请不要为此编写自己的代码。有一个现有的库,SQLiteAssetHelper经过测试、调试和支持。更重要的是,它是由具有丰富Android 开发经验的人 (Jeff Gilfelt) 编写的。

其次,如果您使用的是SQLiteOpenHelper(或类似的子类SQLiteAssetHelper),并且您还在openDatabase()为该数据库编写任何地方,那么您做错了。背后的全部要点SQLiteOpenHelper让它为您打开数据库,以便它可以在需要时创建或升级该数据库。

第三,如果您正在使用SQLiteOpenHelper(或类似的子类SQLiteAssetHelper),并且您有一个空方法onCreate(),那么您做错了。这里需要有代码来实际创建数据库,无论是您继承(例如,from SQLiteAssetHelper)的代码还是您编写的代码。

第四,如果您正在使用SQLiteOpenHelper(或类似的子类SQLiteAssetHelper),并且您正在调用getReadableDatabase()orgetWriteableDatabase()并且忽略了返回值,那么您做错了。你调用那些方法来打开数据库,给你你SQLiteDatabase的使用。

这段代码还有很多错误,但这应该足以让你更接近一个合理的实现。

于 2013-07-04T14:09:39.520 回答
3

使用此代码更改您的 chekDatabase 这完全有效

私人布尔 checkDataBase() {

文件 dbFile = 新文件(DB_PATH + DB_NAME);返回 dbFile.exists();

}

于 2013-07-06T05:37:30.283 回答