0

I have problem which many have and I think, that I tried all solutions, but I have not found the right solution yet.

My existing database "base.sqlite3" is in "assets" folder, contains three tables. When I want to do query, appears error, that table is not there.

(In code are possible syntax errors, cause I translated code)

public class Sqlite extends SQLiteOpenHelper {

private final Context myContext;
public SQLiteDatabase base;
private static String path ="/data/data/" + "com.example.myexample" + "/databases/";
private static String name = "base.sqlite3";
private static String p = path + name;

public Sqlite(Context context){
    super(context, ime, null, 1);
    this.myContext = context;

    createDatabase();
}   

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

@Override
public synchronized void close() 
{
        if(base != null)
            base.close();
        super.close();
}

public void createDatabase()
{
    boolean exist1 = checkDatabase();
    if(exist1){}

    else
    {
        base = this.getReadableDatabase();
        base.close();
        copyDatabase(); 
    }
}
private boolean checkDatabase()
{
    SQLiteDatabase check = null;
    try
    {
         check = SQLiteDatabase.openDatabase(p, null, SQLiteDatabase.OPEN_READONLY);
    }
    catch(SQLiteException e)
    { }

    if(check != null)
    {
        check.close();
    }
    return check != null ? true : false;
}

private void copyDatabase()
{
        InputStream dat = null;
        try {
            dat = myContext.getAssets().open(name);
        } catch (IOException e) {
            e.printStackTrace();
        }

        OutputStream dat2 = null;
        try {
            dat2 = new FileOutputStream(p);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        byte[] buffer = new byte[1024];
        int length;
        try {
            while ((length = dat.read(buffer))>0)
            {
                dat2.write(buffer, 0, length);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            dat2.flush();
            dat2.close();
            dat.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
}

public void openDatabase()
{
    base = SQLiteDatabase.openDatabase(p, null, SQLiteDatabase.OPEN_READONLY);
}

public Cursor SelectSomething(String sql)
{   
    base = SQLiteDatabase.openDatabase(p, null, SQLiteDatabase.OPEN_READONLY);
    Cursor cursor = base.rawQuery(sql,null);

    return cursor;
}

}

Thank you so much for all help!

Anette
  • 90
  • 1
  • 5
  • did you check if the table is created in the database? if you are on Linux navigate to the directory of the database and type `$ sqlite3 base.sqlite3` then type `.tables` to get the list of all tables there. if it does not exist then your code is not creating the table. you can also query whatever you like with the sqlite3 command on Linux. – toufikovich Apr 16 '14 at 10:20
  • Yes, database containts tables, I check that with SQLite Studio. I am not on Linux, unfortunately. – Anette Apr 16 '14 at 12:29

1 Answers1

0

As already was stated in the comment to this answer, the code from this article is

" old, outdated, dreadful (concatenation to create file paths?), and problematic",

and it appears you are not the first to encounter problems with it.

Also, in the same comment to the same answer, it is proposed to use SQLiteAssetHelper. Consider trying it.

Community
  • 1
  • 1
Drew
  • 3,307
  • 22
  • 33