1. Are you ready for the Galaxy S20? Here is everything we know so far!

no such table SQLITE

Discussion in 'Android Development' started by dplusk, Mar 17, 2021.

  1. dplusk

    dplusk Lurker
    Thread Starter

    Hi, I'm a noob in programming android apps. When using SQLite Database I got following problem:
    Logcat tells me: "no such table: table1", when I run both methods. Where is my mistake?
    I use an existing database, placed in assets/databases folder.
    MainActivity
    Code (Java):
    1. package com.dplusk.example_sql;
    2.  
    3. import androidx.appcompat.app.AppCompatActivity;
    4.  
    5. import android.os.Bundle;
    6. import android.view.View;
    7. import android.widget.Toast;
    8.  
    9. public class MainActivity extends AppCompatActivity {
    10.  
    11.     DataBaseHelper dataBaseHelper;
    12.     @Override
    13.     protected void onCreate(Bundle savedInstanceState) {
    14.         super.onCreate(savedInstanceState);
    15.         setContentView(R.layout.activity_main);
    16.     }
    17.  
    18.     public void onClickSearch(View view) {
    19.  
    20.         dataBaseHelper = new DataBaseHelper(MainActivity.this);
    21.         try {
    22.             String post = dataBaseHelper.getPost("table1","Moser");
    23.             Toast.makeText(this,String.format("%s",post),Toast.LENGTH_SHORT).show();
    24.         }catch (Exception e){
    25.             Toast.makeText(this,"Error",Toast.LENGTH_SHORT).show();
    26.         }
    27.     }
    28.  
    29.     public void onClickUpdate(View view) {
    30.         //Toast.makeText(MainActivity.this,"Saving successful",Toast.LENGTH_LONG).show();
    31.     }
    32.  
    33.     public void onClickRows(View view) {
    34.         dataBaseHelper = new DataBaseHelper(MainActivity.this);
    35.         try {
    36.             long count = dataBaseHelper.numberOfRows("table1");
    37.             String message = String.format("%s",count);
    38.             Toast.makeText(MainActivity.this,message,Toast.LENGTH_LONG).show();
    39.         }catch (Exception e){
    40.             Toast.makeText(this,"Error",Toast.LENGTH_SHORT).show();
    41.         }
    42.     }
    43. }
    DataBaseHelper
    Code (Java):
    1. package com.dplusk.example_sql;
    2.  
    3. import android.content.Context;
    4. import android.database.Cursor;
    5. import android.database.sqlite.SQLiteDatabase;
    6. import android.database.sqlite.SQLiteOpenHelper;
    7.  
    8. import androidx.annotation.Nullable;
    9.  
    10. public class DataBaseHelper extends SQLiteOpenHelper {
    11.  
    12.     public DataBaseHelper(@Nullable Context context) {
    13.         super(context, "database.db", null, 1);
    14.     }
    15.  
    16.     @Override
    17.     public void onCreate(SQLiteDatabase db) {
    18.     }
    19.  
    20.     @Override
    21.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    22.     }
    23.  
    24.     public String getPost(String table, String nameIn){
    25.         String queryString = "SELECT post FROM "+ table +" WHERE name = "+nameIn;
    26.         SQLiteDatabase db = this.getReadableDatabase();
    27.         Cursor cursor = db.rawQuery(queryString,null);
    28.         StringBuffer buffer = new StringBuffer();
    29.         while(cursor.moveToNext()){
    30.             String post = cursor.getString(0);
    31.             buffer.append(""+post);
    32.         }
    33.         cursor.close();
    34.         db.close();
    35.         return buffer.toString();
    36.     }
    37.  
    38.     public long numberOfRows(String table){
    39.         String queryString = "SELECT * FROM " + table;
    40.         SQLiteDatabase db = this.getReadableDatabase();
    41.         Cursor cursor = db.rawQuery(queryString,null);
    42.         long count = cursor.getCount();
    43.         cursor.close();
    44.         db.close();
    45.         return count;
    46.     }
    47. }
    Please help me.
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...

Share This Page

Loading...