17

I know that subject was disscussed before. But i cant find solution to my problem so im going to ask it one more time. I have problem creating database. Ive copied implementation of SQLiteOpenHelper:

public class DBCreator extends SQLiteOpenHelper 
{
    private static final String DATABASE_NAME = "servision.db";
    private static final int DATABASE_VERSION = 1;


    private static final String INFO_TABLE ="create table INFO(key text,value text);";
    public static final String GATEWAYS_TABLE = "GATEWAYS";

    // Table name
    public static final String TABLE = "events";

    // Columns
    public static final String TIME = "time";
    public static final String TITLE = "title";

    public DBCreator(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        try
        {
            String gatewaytablescript ="create table" +GATEWAYS_TABLE+"(" +
            "id int primary key, " +
            "host text not null," +
            "port int not null," +
            "username text," +
            "password text,"+
            "useproxy int," +
            "proxyHost text," +
            "proxyPort port," +
            "desc text," +
            "secondaryip text," +
            "secondaryport int," +
            "timezone int," +
            "encryption int," +
            "customkey text" +
            ");";

            Log.d("DBCreator", "Creating " + GATEWAYS_TABLE + "table");
            db.execSQL(gatewaytablescript);

            String infotablescript ="create table " + INFO_TABLE + "(key text,value text);";
            Log.d("DBCreator", "Creating " + INFO_TABLE + "table");
            db.execSQL(infotablescript);

        }
        catch(SQLException ex)
        {
            Log.d("DBCreator", "onCreate exception " +ex.getMessage()); 
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
        Log.d("EventsData", "onUpgrade");
        if (oldVersion >= newVersion)
            return;


    }
}

and i create it from main activity onCreate method like this.

DBCreator db =new DBCreator(this);
db.getReadableDatabase();

in logcat i receive following info:

06-01 17:31:15.523: INFO/global(2470): Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if an 8k buffer is required.
06-01 17:31:15.652: INFO/Database(4160): sqlite returned: error code = 14, msg = cannot open file at source line 25467
06-01 17:31:15.652: ERROR/Database(4160): sqlite3_open_v2("/data/data/com.servision.svclient/databases/servision.db", &handle, 6, NULL) failed
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): Couldn't open servision.db for writing (will try read-only):
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): android.database.sqlite.SQLiteException: unable to open database file
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1921)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:883)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:960)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:953)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:602)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:158)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at com.servision.svclient.Main.onCreate(Main.java:52)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.os.Looper.loop(Looper.java:123)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at java.lang.reflect.Method.invokeNative(Native Method)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at java.lang.reflect.Method.invoke(Method.java:521)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160):     at dalvik.system.NativeStart.main(Native Method)
06-01 17:31:15.738: INFO/Database(4160): sqlite returned: error code = 14, msg = cannot open file at source line 25467
06-01 17:31:15.738: ERROR/Database(4160): sqlite3_open_v2("/data/data/com.servision.svclient/databases/servision.db", &handle, 1, NULL) failed

onCreate of DBCreator is never called. Im running program on Galaxy S in debug mode from Eclipse. Am I missing some permissions? May be context is not good or something?

tshepang
  • 12,111
  • 21
  • 91
  • 136
AlexS
  • 927
  • 4
  • 16
  • 29
  • 1
    Can you try to remove the *.db file extension from your database name (change DATABASE_NAME from servision.db to servision)? – Daniel Novak Jun 01 '11 at 14:57
  • Hi Daniel , thanks for quick answer.No changing the name does not help. – AlexS Jun 01 '11 at 15:07
  • Have you manually checked your permissions to read / write the database? I had some weird market crashes which reported me that in some rare cases my App could not open database for writing. I protected it to handle that special case, but I don't know of any workaround. If you don't have permissions to read/write the file, I don't think there's anything you can do – pandre Jun 01 '11 at 15:16
  • Im pretty sure that i have permissions problem.Ive tried to create file String FILENAME = "hello_file"; String string = "hello world!"; FileOutputStream fos; try { fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); fos.write(string.getBytes()); fos.close(); } catch (Exception e) { e.printStackTrace(); } and got exactly same exception. Why does it happens? I mean i should be able to access storage? None of the tutorials never mention access permissions. – AlexS Jun 02 '11 at 08:31
  • 1
    i solved this problem by manually removing whole application folder from device. – AlexS Jun 02 '11 at 13:14
  • I had same issue can you please tell me what is the solution ? – Dharmendra Aug 22 '11 at 13:10
  • 2
    @AlexS Yes, I'm also interested to know. I don't get what you mean by removing the application folder. Do you mean removing and re-creating the folder? Do you mean you simply created your db somewhere else? Please expand. Thanks in advance! – webmat Nov 29 '11 at 14:47
  • I see the same message in Eclipse, but my app does not use any Sqlite database. Any idea? – Luis A. Florit Apr 21 '14 at 23:07

6 Answers6

7

In relation to the sqlite returned: error code = 14, msg = cannot open file at source line 25467 and sqlite3_open_v2("/data/data/com.servision.svclient/databases/servision.db", &handle, 1, NULL) failed error, this seems to be caused when there's no database and Android tries to create one.

Some reading here

http://androidblogger.blogspot.com/2011/02/instable-android-and-unable-to-open.html

http://www.itsalif.info/content/check-if-database-exist-android-sqlite3openv2-failed

http://code.google.com/p/android/issues/detail?id=949

georgiecasey
  • 21,793
  • 11
  • 65
  • 74
6

check permissions in your manifest add

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
user2307183
  • 77
  • 1
  • 1
4

When I got this error I used this code for my constructor

public DBClass(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    context.openOrCreateDatabase(DATABASE_NAME, context.MODE_PRIVATE, null);
}
Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
Ameen Maheen
  • 2,719
  • 1
  • 28
  • 28
1

I was having an issue with my app, where changing my datebase file with an old datebase after a reinstallation of the app gave me that error. I guess the problem was that the owner of the old DB file was not the same than the owner of the files for the reinstalled app. So it was solved giving the new file full permissions (777) so it could be read by other users too.

At the end, ensure you don't have a file permissions problem.

Tim Kist
  • 1,164
  • 1
  • 14
  • 38
Pek Ifly
  • 89
  • 3
0

I had a similar problem and here is how I solved it:

https://stackoverflow.com/a/22743794/1947094

To sum up:

adb shell
cd /data/data/YOUR.APP.PACKAGE/databases/
ls

This gives you a list of the databases used by your app.

cp your_database.db your_database.db_backup
rm your_database.db
Community
  • 1
  • 1
mimetist
  • 161
  • 6
-1

already bug raised in this link

http://code.google.com/p/android/issues/detail?id=949

use the following api...

context.openOrCreateDatabase("sample.db", MODE_PRIVATE, null);

try this link

http://www.itsalif.info/content/check-if-database-exist-android-sqlite3openv2-failed

satish
  • 27
  • 2