lundi 31 août 2015

database file not opening after merging projects

I am working with Android Studio and java. I created a test project that can copy an existing SQLite database into a program. The project also can append the copied database and read it back. This project works perfectly and does what I need for my main program. I added the test project to my main program by copy pasting each java class to my main project. Now, the program can no longer open the file. I have the same permission in both manifest files, so I should have access to the path and I know the path is OK since it works in the test project. The two changes I made when coping from the test project to the main project is the path name was updated with the main projects name and the putinformation method is called from main in the test program and from a second class in the main program. Below is the code calling putinformation and the entire DatabaseOperations class with the putinformation method. Alternatively, if it was possible to just run the test project from my main project that would be OK as long as I can pass back the results of the database searches.

public class updateDatabase extends Activity {

EditText name,Kal;
String name, kal_value;
Button store, read, copydatabase;
Context ctx = this;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.update_database_screen);
    name = (EditText) findViewById(R.id.nameeditText);
    Kal = (EditText) findViewById(R.id.kaleditText);
    store = (Button) findViewById(R.id.saveButton);
    read = (Button) findViewById(R.id.readButton);
    copydatabase = (Button) findViewById(R.id.copyButton);
    copydatabase.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.d("updateDatabase"," started copydatabase ");
            DataBaseHelper myDbHelper; //= new DataBaseHelper(ctx);
            myDbHelper = new DataBaseHelper(ctx);

            try {
                myDbHelper.createDataBase();
            } catch (IOException ioe) {
                throw new Error("Unable to create database");
            }
            try {
                Log.d("updateDatabase"," started try myDbHelper.openDataBase() ");
                myDbHelper.openDataBase();
            }catch(SQLException sqle){
                sqle.printStackTrace();
            }
        }
    });

    store.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            name = name.getText().toString();
            kal_value = Kal.getText().toString();
            DatabaseOperations DB = new DatabaseOperations(ctx);
            DB.putInformation(DB, name, kal_value);
            Toast.makeText(getBaseContext(), "Successfully instered data", Toast.LENGTH_LONG).show();
            name.setText("");
            .setText("");
        }
    });

.....

public class DatabaseOperations extends SQLiteOpenHelper { public static final int database_version = 1; public String CREATE_QUERY = "CREATE TABLE "+ TableInfo.TABLE_NAME+"("+ TableInfo.NAME+" TEXT, "+ TableInfo.KAL+" TEXT) ;";

public DatabaseOperations(Context context) {
    super(context, TableData.TableInfo.DB_PATH+TableData.TableInfo.DATABASE_NAME, null, database_version);
}

@Override
public void onCreate(SQLiteDatabase sdb) {
    boolean dbExist = DataBaseHelper.checkDataBase();
    if (!dbExist)  sdb.execSQL(CREATE_QUERY);
}
public void putInformation(DatabaseOperations dop, String name, String kcal){
    Log.d("Database Operations", "started putInformation");
    SQLiteDatabase SQ = dop.getWritableDatabase();
    Log.d("Database Operations", "before if statement");
    if (!dbExist)  SQ.execSQL(CREATE_QUERY);
    ContentValues cv = new ContentValues();
    cv.put(TableInfo.NAME,name);
    cv.put(TableInfo.KAL, kal);
    long k = SQ.insert(TableData.TableInfo.TABLE_NAME, null, cv);
    Log.d("Database Operations", "one row inserted");
}

The error code follows:

08-31 16:55:53.581    2428-2428/? D/Database Operations﹕ Database created
08-31 16:55:53.581    2428-2428/? D/Database Operations﹕ started putInformation
08-31 16:55:53.584    2428-2428/? E/SQLiteLog﹕ (14) cannot open file at line 30046 of [9491ba7d73]
08-31 16:55:53.584    2428-2428/? E/SQLiteLog﹕ (14) os_unix.c:30046: (2) open(/data/data/com.taavtech.scale_rev_0/databases/food_try.sqlite) -
08-31 16:55:53.585    2428-2428/? E/SQLiteDatabase﹕ Failed to open database '/data/data/com.taavtech.scale_rev_0/databases/food_try.sqlite'.
    android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
            at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
            at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
            at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
            at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
            at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
            at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
            at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
            at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
            at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
            at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:1142)
            at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:267)
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
            at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
            at com.taavtech.scale_rev_0.DatabaseOperations.putInformation(DatabaseOperations.java:49)
            at com.taavtech.scale_rev_0.updateDatabase$2.onClick(updateDatabase.java:78)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
08-31 16:55:53.585    2428-2428/? D/AndroidRuntime﹕ Shutting down VM
    --------- beginning of crash
08-31 16:55:53.585    2428-2428/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.microchip.mldpterminal2, PID: 2428
    android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
            at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
            at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
            at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
            at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
            at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
            at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
            at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
            at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
            at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
            at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:1142)
            at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:267)
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
            at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
            at com.taavtech.scale_rev_0.DatabaseOperations.putInformation(DatabaseOperations.java:49)
            at com.taavtech.scale_rev_0.updateDatabase$2.onClick(updateDatabase.java:78)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)

Aucun commentaire:

Enregistrer un commentaire