hello please I need your help All of this month I tryed to know how to read data from ready database (sqlite.db) in project
.. I want to make a database and I full it befor I ready it (for example a book) then i want to show this detales in a text view or any
I search very mutch but all of video tutorials just fucosed on make an internal database , and update detale and delete but I want to load an external database that I full it befor and I want to use it inside my app pleas help me how to do that I worked on this code but it is just for internal database
I use this youtube video tutorial
https://www.youtube.com/watch?v=rziyVBKEU50
I added ask for read external storage that is not in app I thinked it's mabe necessary
I tryed this video tutorial but my App is crashed where is my problams?
note : this app we inter a name inside a Edittext it give us the address of this query
package com.sirwansoft.externaldatabase;
import android.content.Context;
import android.database.sqlite.SQLiteOpenHelper;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
public class DatabaseOpenHelper extends SQLiteAssetHelper {
private static final String DATABASE_NAME="data";
private static final int DATABASE_VERSION=1;
//constractor
public DatabaseOpenHelper(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
}
and my database assets file
package com.sirwansoft.externaldatabase;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseAcsess {
private SQLiteOpenHelper openHelper;
private SQLiteDatabase db;
private static DatabaseAcsess instance;
Cursor c =null;
//privite constractor that object creating from outside the class is avoided
private DatabaseAcsess (Context context){
this.openHelper=new DatabaseOpenHelper(context);
}
//return instance of the class
public static DatabaseAcsess getInstance(Context context){
if (instance==null){
instance=new DatabaseAcsess(context);
}return instance;
}
//to open the database
public void open(){
this.db=openHelper.getWritableDatabase();
}
//closing the database connection
public void close(){
if (db!=null){
this.db.close();
}
}
//we will query for address by passing name
public String getAddress(String name){
c=db.rawQuery("select address form Tabale Where Name = '"+name+"'",new String[]{});
StringBuffer buffer =new StringBuffer();
while (c.moveToNext()){
String address = c.getString(0);
buffer.append(""+address);
}return buffer.toString();
}
}
and my MainActivity
package com.sirwansoft.externaldatabase;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText edit_name;
Button btn_query;
TextView text_res;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit_name=findViewById(R.id.edit_name);
btn_query=findViewById(R.id.btn_query);
text_res=findViewById(R.id.text_res);
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
1);
//setOnClick to button
btn_query.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatabaseAcsess databaseAcsess =DatabaseAcsess.getInstance(getApplicationContext());
databaseAcsess.open();
//getting string value from address
String name = edit_name.getText().toString();
String address =databaseAcsess.getAddress(name); //we use the getAddress method to get address
//setting text to result field
text_res.setText(address);
databaseAcsess.close();
//database connection closed
//done
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
}
and this is crash logout
10-13 01:33:21.579 22724-22724/? I/art: Not late-enabling -Xcheck:jni (already on)
10-13 01:33:21.694 22724-22724/com.sirwansoft.externaldatabase W/System: ClassLoader referenced unknown path: /data/app/com.sirwansoft.externaldatabase-1/lib/x86
10-13 01:33:21.815 22724-22724/com.sirwansoft.externaldatabase W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
10-13 01:33:22.212 22724-22724/com.sirwansoft.externaldatabase I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
10-13 01:33:22.212 22724-22724/com.sirwansoft.externaldatabase I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
10-13 01:33:22.516 22724-22766/com.sirwansoft.externaldatabase D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
10-13 01:33:22.687 22724-22766/com.sirwansoft.externaldatabase I/OpenGLRenderer: Initialized EGL, version 1.4
10-13 01:33:22.688 22724-22766/com.sirwansoft.externaldatabase W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
10-13 01:33:22.717 22724-22766/com.sirwansoft.externaldatabase D/EGL_emulation: eglCreateContext: 0xae4a4480: maj 2 min 0 rcv 2
10-13 01:33:22.721 22724-22766/com.sirwansoft.externaldatabase D/EGL_emulation: eglMakeCurrent: 0xae4a4480: ver 2 0 (tinfo 0xae492ee0)
10-13 01:33:22.785 22724-22766/com.sirwansoft.externaldatabase D/EGL_emulation: eglMakeCurrent: 0xae4a4480: ver 2 0 (tinfo 0xae492ee0)
10-13 01:33:23.293 22724-22766/com.sirwansoft.externaldatabase D/EGL_emulation: eglMakeCurrent: 0xae4a4480: ver 2 0 (tinfo 0xae492ee0)
10-13 01:33:23.299 22724-22766/com.sirwansoft.externaldatabase E/Surface: getSlotFromBufferLocked: unknown buffer: 0xaab0aa00
10-13 01:33:31.669 22724-22766/com.sirwansoft.externaldatabase D/EGL_emulation: eglMakeCurrent: 0xae4a4480: ver 2 0 (tinfo 0xae492ee0)
10-13 01:33:37.558 22724-22724/com.sirwansoft.externaldatabase W/SQLiteAssetHelper: copying database from assets...
10-13 01:33:37.558 22724-22724/com.sirwansoft.externaldatabase D/AndroidRuntime: Shutting down VM
10-13 01:33:37.559 22724-22724/com.sirwansoft.externaldatabase E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sirwansoft.externaldatabase, PID: 22724
com.readystatesoftware.sqliteasset.SQLiteAssetHelper$SQLiteAssetException: Missing databases/data file (or .zip, .gz archive) in assets, or target folder not writable
at android.content.res.AssetManager.openAsset(Native Method)
at android.content.res.AssetManager.open(AssetManager.java:313)
at android.content.res.AssetManager.open(AssetManager.java:287)
at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.copyDatabaseFromAssets(SQLiteAssetHelper.java:436)
at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.createOrOpenDatabase(SQLiteAssetHelper.java:400)
at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getWritableDatabase(SQLiteAssetHelper.java:176)
at com.sirwansoft.externaldatabase.DatabaseAcsess.open(DatabaseAcsess.java:34)
at com.sirwansoft.externaldatabase.MainActivity$1.onClick(MainActivity.java:40)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-13 01:38:37.636 22724-22724/com.sirwansoft.externaldatabase I/Process: Sending signal. PID: 22724 SIG: 9
it cannot find database file but why?
com.readystatesoftware.sqliteasset.SQLiteAssetHelper$SQLiteAssetException: Missing databases/data file (or .zip, .gz archive) in assets, or target folder not writable