Reputation: 29
My database has a table named "tbl", but the error says: table "tbl" not found
DataBase.java => as bellow
public class DataBase extends SQLiteOpenHelper {
private Context context;
public DataBase(@Nullable Context context) {
super(context, info_db.Data_Name, null, info_db.DataBase_Version);
this.context = context;
IsDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private void IsDatabase() {
File check = new File(info_db.Package);
if (!check.exists()) {
check.mkdir();
}
check = context.getDatabasePath(info_db.Data_Name);
if (!check.exists()) {
try {
copyDataBase();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void copyDataBase() throws IOException {
InputStream myInput = context.getAssets().open(info_db.DataBase_Source);
String outFileName = info_db.Package + info_db.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 List<info_Data> fetchmaindata() {
SQLiteDatabase db = this.getReadableDatabase();
List<info_Data> data = new ArrayList<>();
String query ="SELECT "+info_db.Data_Name+","+info_db.Data_Body+","+info_db.Data_Fav+","+info_db.Data_Image+" FROM "+info_db.Table_Name;
Cursor cursor = db.rawQuery(query,null);
if (cursor.moveToFirst()){
do {
info_Data info = new info_Data();
info.setName(cursor.getString(cursor.getColumnIndex(info_db.Data_Name)));
info.setBody(cursor.getString(cursor.getColumnIndex(info_db.Data_Body)));
info.setFav(cursor.getInt(cursor.getColumnIndex(info_db.Data_Fav)));
info.setImage(cursor.getString(cursor.getColumnIndex(info_db.Data_Image)));
data.add(info);
}while (cursor.moveToNext());
}
cursor.close();
db.close();
return data;
}
}
FragMain.java => as bellow
public class FragMain extends Fragment {
public FragMain() {
// Required empty public constructor
}
private RecyclerView recyclerView;
private AdapterMain adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
DataBase db = new DataBase(getActivity());
List<info_Data> data = db.fetchmaindata();
db.close();
View view = inflater.inflate(R.layout.fragment_frag_main, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.main_re);
adapter = new AdapterMain(getActivity(), postdata(data));
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return view;
}
private List<info> postdata(List<info_Data> db) {
List<info> data = new ArrayList<>();
//String name[] = {"بخش هشتم", "بخش هفتم", "بخش ششم", "بخش پنجم", "بخش چهارم", "بخش سوم", "بخش دوم"};
//int pic[] = {R.drawable.img, R.drawable.img, R.drawable.img, R.drawable.img, R.drawable.img, R.drawable.img, R.drawable.img};
for (int i = 0; i < db.size(); i++) {
info cur = new info();
cur.title = db.get(i).getName();
String uri ="@drawable/"+db.get(i).getImage();
int iconid = getResources().getIdentifier(uri, null, getActivity().getPackageName());
cur.iconid = iconid;
cur.body = db.get(i).getBody();
data.add(cur);
}
return data;
}
}
and the error in logcat => as bellow
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.af.sawalebast/com.af.sawalebast.MainActivity}: android.view.InflateException: Binary XML file line #26: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
at android.app.ActivityThread.access$800(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.view.InflateException: Binary XML file line #26: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.af.sawalebast.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2280)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
at android.app.ActivityThread.access$800(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.database.sqlite.SQLiteException: no such table: tbl (code 1): , while compiling: SELECT name,body,fav,image FROM tbl
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
**at com.af.sawalebast.DataBase.fetchmaindata(DataBase.java:74)
at com.af.sawalebast.FragMain.onCreateView(FragMain.java:33)**
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
at androidx.fragment.app.FragmentManagerImpl.ensureInflatedFragmentView(FragmentManagerImpl.java:1138)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:851)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1133)
at androidx.fragment.app.FragmentManagerImpl.addFragment(FragmentManagerImpl.java:1393)
at androidx.fragment.app.FragmentManagerImpl.onCreateView(FragmentManagerImpl.java:3205)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:134)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:357)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:336)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.af.sawalebast.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2280)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
at android.app.ActivityThread.access$800(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
THANKS TO ALL
Upvotes: 1
Views: 170
Reputation: 56953
The most likely cuase of this is that the database file copied into the assets has no such table.
I'd suggest checking, using the tool used to create the database file, to see that the table name tbl exists. If not create the table and save the file and then close the tool (restart the tool anddo again to double check) and when positive that the table has been created replace the asset file and try again after uninstalling the App or dleeting the App's data.
What you could do as an interim measure is to use the following code :-
DataBase db = new DataBase(getActivity()); //<<<<< EXISTING CODE
Cursor csr = db.getWritableDatabase().query("sqlite_master",null,"name NOT like 'sqlite_%'",null,null,null);
DatabaseUtils.dumpCursor(csr);
csr.close();
List<info_Data> data = db.fetchmaindata(); //<<<<< EXISTING CODE
db.close(); //<<<<< EXISTING CODE
This will extract a list of all the database entities (tables indexes etc) that are not SQLite generated and dump the extracted data to the log. Thus you could see what tables exist in the database copied from the asset.
Upvotes: 1