Is it really necessary to create SQLite tables every time the application starts?

In several SQLite tutorials, I noticed that the table was recreated in the onCreate() event of a class that extends SQLiteOpenHelper . I have already created my SQLite database and tables outside of the Android environment (Eclipse IDE) using the Firefox add-on. The database tables are in the expected location:

C:\aXX3&Space\Android\workspace\OnDemandAndAutomatic_Project\assets

It seems strange to me that I have to recreate them programmatically each time (obviously, they continue to exist and save the data, or what's the point)? However, I now have a serious problem with this application, which leads me to my question:

  • Is it really necessary to constantly remind Android of the database?

To my chagrin, I just realized that instead of creating database tables in C:\aXX3&Space\Android\workspace\OnDemandAndAutomatic_Project\assets I put them in C:\aXX3&Space\Android\workspace\OnDemandAndAutomatic\assets , which was an earlier version of the application , but copied the SQLite file to the desired location did not change anything. If this is what caused the problem since my application was looking for the AWOL database, is it not enough to just copy and paste the file? Do I have to do something to officially enter the database in the system/Android/Eclipse directory? Any suggestions?

+4
source share
3 answers

In fact, SQLiteOpenHelper is responsible for creating, updating, and more. If you want to create table programs, then you need to write a query for the creation tables in the onCreate method of the SQLiteOpenHelper class. If you want to update your database after a previously created database, you can write modified table queries using the onUpgrade method only; you will have to change the version of the database.

If you have already created the database from the outside, and if you want to use this database, you need to put this database in the resource folder and copy this file to the database folder, which is located in the /data/data/packagename/databases folder.

here is an example for copying a database from database to database

 private static boolean copyDataBase(Context c) throws IOException { String DB_PATH = "/data/data/" + c.getPackageName() + "/databases/"; AssetManager mg = c.getResources().getAssets(); InputStream myInput = null; try { myInput = mg.open(DATABASE_NAME); } catch (IOException ex) { return false; } if (myInput != null) { String outFileName = DB_PATH + DATABASE_NAME; OutputStream myOutput = new FileOutputStream(outFileName); byte[] buffer = new byte[8000]; int length; while ((length = myInput.read(buffer)) > 0) { myOutput.write(buffer, 0, length); } myOutput.flush(); myOutput.close(); myInput.close(); Log.d(TAG, "Database is copied"); return true; } return false; } 

Here is a method for checking databases and its version for copying

 public void copyDatabase() { try { SharedPreferences preferences = c.getSharedPreferences(c.getPackageName(), Context.MODE_PRIVATE); if (checkDataBase(c)) { if (preferences.getInt("dbversion", 0) != 0) { c.deleteDatabase(DatabaseHelper.DATABASE_NAME); } } getReadableDatabase(); close(); if (copyDataBase(c)) { Editor editor = preferences.edit(); editor.putInt("dbversion", DatabaseHelper.DATABASE_VERSION); editor.commit(); } } catch (Exception e) { e.printStackTrace(); } } 

Here is an example that checks if a database exists or not?

 public static boolean checkDataBase(Context c) { File f = new File("/data/data/" + c.getPackageName() + "/databases/" + DATABASE_NAME); if (!f.exists()) return false; SQLiteDatabase checkDB = null; try { checkDB = SQLiteDatabase .openDatabase("/data/data/" + c.getPackageName() + "/databases/" + DATABASE_NAME, null, SQLiteDatabase.OPEN_READONLY); checkDB.close(); } catch (SQLiteException e) { e.printStackTrace(); } return checkDB != null ? true : false; } 
+4
source

I think you are misunderstanding here. When the SQLiteOpenHelper object is constructed, it checks if the SQLite database exists or not, if not, it will call the onCreate () method (where developers usually implement SQL creation databases)

Update:
@Clay Shannon: I will explain the mechanism of work of Eclipse and Android. Let's say you use Eclipse to program an Android project. The new project will create the project folder in the workspace (in your case it is C: \ aXX3 & Space \ Android \ workspace , and the project folder is OnDemandAndAutomatic_Project ). This folder will contain several subfolders, such as: src, bin, assets, res, ... Each folder has its own role, and you are interested in the resource folder, right? The Assets folder is used to store help files (which you cannot or do not want to put the res folder), such as: html file, sound file, image file, text file ... When Eclipse creates apk from the project, these files are also included in apk. When you install apk on your Android device, apk is copied to the Android system folder and a folder is created to store application data, as Dharmendra mentioned: / data / data / {packagename} / (packagename, for example com.google.app, etc. ., and this path is for your device’s Android OS, not Windows).

In this case, you want to use your database of existing ones, so you need to implement a function that checks if your database exists or not, if you do not copy your db to the database path / data / data / {packagename} / databases / , and call this function when the application starts. And how to do this, the answer has already been given here How to copy an existing database from one application to another . Also here is a link to resource access files if you do not know http://www.wiseandroid.com/post/2010/06/14/Android-Beginners-Intro-to-Resources-and-Assets.aspx

I hope you can solve your problem now. Note : your existing db must be a SQLite database, or the application will not recognize it, even if you copy the correct path.

+5
source

SQLiteOpenHelper has its onCreate() if the database does not exist. It has an onUpgrade() call if the database schema has changed and needs to be updated. So the answer to your question is no .

+2
source

Source: https://habr.com/ru/post/1404045/


All Articles