Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I embed an SQLite database into an application?

I think I have some basic understanding problem so maybe someone's able to help :-)

I'm developing an Android application using Eclipse and this application will make use of a database (only reading from the database will be implemented). The database contains around 4,000 entries i.e. creating and populating the database via source code is not an option. Thus I have created the database in advance with all its records.

But how can I "embed" this database file into my application and then access it? The databse will be around 500 kB in file size. Downloading from a remote server is not an option either as this is not allowed.

Thanks, Robert

like image 787
Robert Strauch Avatar asked Apr 11 '11 20:04

Robert Strauch


People also ask

How do I connect SQLite to a Windows application?

Open your Visual Studio and select new project and in Visual C# select "Windows Forms Application" and provide the name as Sqlite and click on OK. Right-click on your application and select "Open folder in your window application" and then go to: BIN -> Debug and extract your application here.

Can I use SQLite in web application?

SQLite will normally work fine as the database backend to a website. But if the website is write-intensive or is so busy that it requires multiple servers, then consider using an enterprise-class client/server database engine instead of SQLite.

Is SQLite an embedded database?

SQLite is an embedded, open-source, lightweight SQL database engine. The C based library is transactional, self-contained, and highly compact. It's also fairly easy to implement. It doesn't require any sort of installation or configuration, and all data is stored locally.


1 Answers

I solved that problem by:

  1. adding file.db into project/assets folder;

  2. writing next class:

    public class LinnaeusDatabase extends SQLiteOpenHelper{      private static String DATABASE_NAME = "Dragonfly.db";     public final static String DATABASE_PATH = "/data/data/com.kan.linnaeus/databases/";     private static final int DATABASE_VERSION = 1;      private SQLiteDatabase dataBase;     private final Context dbContext;      public LinnaeusDatabase(Context context) {         super(context, DBActivity.DatabaseName, null, DATABASE_VERSION);         this.dbContext = context;         DATABASE_NAME = DBActivity.DatabaseName;         // checking database and open it if exists         if (checkDataBase()) {             openDataBase();         } else         {             try {                 this.getReadableDatabase();                 copyDataBase();                 this.close();                 openDataBase();              } catch (IOException e) {                 throw new Error("Error copying database");             }             Toast.makeText(context, "Initial database is created", Toast.LENGTH_LONG).show();         }     }      private void copyDataBase() throws IOException{         InputStream myInput = dbContext.getAssets().open(DATABASE_NAME);         String outFileName = DATABASE_PATH + DATABASE_NAME;         OutputStream myOutput = new FileOutputStream(outFileName);          byte[] buffer = new byte[1024];         int length;         while ((length = myInput.read(buffer))>0){             myOutput.write(buffer, 0, length);         }          myOutput.flush();         myOutput.close();         myInput.close();     }      public void openDataBase() throws SQLException {         String dbPath = DATABASE_PATH + DATABASE_NAME;         dataBase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);     }      private boolean checkDataBase() {         SQLiteDatabase checkDB = null;         boolean exist = false;         try {             String dbPath = DATABASE_PATH + DATABASE_NAME;             checkDB = SQLiteDatabase.openDatabase(dbPath, null,             SQLiteDatabase.OPEN_READONLY);         } catch (SQLiteException e) {             Log.v("db log", "database does't exist");         }          if (checkDB != null) {             exist = true;             checkDB.close();         }         return exist;     } } 
like image 82
Anton Derevyanko Avatar answered Sep 20 '22 17:09

Anton Derevyanko