Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8276451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:22:47+00:00 2026-06-08T08:22:47+00:00

I have an app that must open a sqlitedatabase from a .db file on

  • 0

I have an app that must open a sqlitedatabase from a .db file on assets folder. This database must be opened each time the app starts.

I’m checking some tutorials for deal with sqlite databases in android and for load sqlite databases from a file. I merge all these tutorials in this code, but it doesn’t works.

My database contains a TABLE called items wich haves some data inside, but when i call a SELECT * sentence from items i got an exception telling me that items table doesn’t exists:

        String databaseName="frases";

    SQLiteManager sqlManager = new SQLiteManager(this); 
    sqlManager.open(databaseName);

    List<String> nombres = new ArrayList<String>();
    Cursor cursor=sqlManager.rawQuery("SELECT * FROM 'items'");
    while (cursor.moveToNext()) {
        nombres.add(cursor.getString(1));
    }

    for (int i=0; i<nombres.size();i++){
        Log.d("DATABASE", "Nombre: "+nombres.get(i));
    }

    sqlManager.close(); //Cerramos la base de datos

And this is my SQLiteManager class:

public class SQLiteManager {
private DemoSQLiteHelper dbHelper;
private SQLiteDatabase db;
private static Context ctx;

public SQLiteManager(Context ctx) {
    this.ctx = ctx;
}

public void open(String databaseName) throws SQLException {
    dbHelper = new DemoSQLiteHelper(ctx, databaseName, null, 1);
    generateSQLiteDB(databaseName);
    db = dbHelper.getWritableDatabase();
}

public void close() {
    dbHelper.close();
}

public void execSQL(String sql){
    db.execSQL(sql);
}

public Cursor rawQuery(String sql){
    Cursor cursor=db.rawQuery(sql, null);
    return cursor;
}

public void clearDB() {
    dbHelper.clearDb(db);
}

public class DemoSQLiteHelper extends SQLiteOpenHelper {

    public DemoSQLiteHelper(Context contexto, String nombre, CursorFactory factory, int version) {
        super(contexto, nombre, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int versionAnterior, int versionNueva) {
    }

    public void clearDb(SQLiteDatabase db){
        onCreate(db);
    }               
}       

private void generateSQLiteDB(String databaseName) {        // 
    SQLiteDatabase db =  dbHelper.getReadableDatabase(); // by calling this line an empty database will be created into the default system path of this app - we will then overwrite this with the database from the server
    db.close();
    OutputStream os = null;
    InputStream is = null;
    try{
        is =  ctx.getAssets().open(databaseName+".db");
        os = new FileOutputStream("/data/data/com.DemoSqlite/databases/"+databaseName+".db");   
        copyFile(os, is);
    }catch (Exception e) {
        Log.e("DB", "Database not found", e);                          
    }finally{
        try{
            if(os != null)
                os.close();     
            if(is != null)
                is.close();
        } catch (IOException e) {Log.e("DB", "Can't close adapters");}
    }
}

private void copyFile(OutputStream os, InputStream is) throws IOException {
    byte[] buffer = new byte[1024];
    int length;
    while((length = is.read(buffer))>0)
        os.write(buffer, 0, length);        
    os.flush();
}   
}

What is wrong in my code?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T08:22:49+00:00Added an answer on June 8, 2026 at 8:22 am

    Replace

    dbHelper = new DemoSQLiteHelper(ctx, databaseName, null, 1);

    with

    dbHelper = new DemoSQLiteHelper(ctx, databaseName + ".db", null, 1);

    in your SQLiteManager.open() method.

    Although you copy database from assets, you pass frases database name to your SQLiteHelper, not frases.db so your helper uses wrong database file.

    You are copying your database from assets everytime you instantiate your SQL manager. That’s wrong approach. Take a look at this example how to move your database from assets https://stackoverflow.com/a/11601770/1300995.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app that must get data from the Sqlite database in order
I have an App that downloads and displays images from URL's. This works fine
Have an app that has listings - think classified ads - and each listing
I have an app that received JSON data from the server. Currently when I
We have an asp.net 3.5 web app that we must start load testing with
I must have missed this somehow... In my app user x owns posts 1,
I have need to implement push Notification in my app.For that when i open
have an app that finds your GPS location successfully, but I need to be
I have an app that switches between full screen and normal screen for a
I have an app that uses the ActionBar with tabs in combination with Fragments.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.