sqlite3_open_v2(“/ data / data / com.food.find / databases / mydatabase”,& handle,2,NULL)在android中失败了吗?

时间:2013-07-04 13:37:56

标签: android sqlite

我正在尝试从资产创建数据库,但是给我错误无法打开数据库 但我再次运行后不给错误是完全运行 当第一次创建是给我错误我把我的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
  

RestaurantDatabase.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;


    }



}

2 个答案:

答案 0 :(得分:5)

这段代码很糟糕。我首先要删除整个内容并重新开始。

首先,如果您要使用您的应用程序打包数据库,请不要为此编写自己的代码。有an existing library, SQLiteAssetHelper,经过测试,调试和支持。更重要的是,它是由某人(Jeff Gilfelt)编写的,具有很多的Android开发经验。

其次,如果您使用SQLiteOpenHelper(或类似SQLiteAssetHelper的子类),并且您在该数据库的任何位置写入openDatabase(),< strong>你做错了。 SQLiteOpenHelper后面的完整和整个 it 为您打开数据库,因此可以根据需要创建或升级该数据库。

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

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

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

答案 1 :(得分:3)

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

private boolean checkDataBase() {

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

}