Problem, "My application has stopped" - "SQLiteException: no such table"

1

I have created an App to consult the address of a person by placing its name in an EditText, for the database I used the BD Browser program for SQlite, create a folder called assets and place the .db file there. The application opens in the emulator, but by pressing the button to search I get the error "Unfortunaly, My application has stopped" and it closes. Thanks for any help

Other information: The emulator is Genymotion, use a generic device 5.0.0 API 21.

The logcat errors are the following:

12-13 14:30:59.555 2211-2211/? E/libprocessgroup: failed to make and chown         
/acct/uid_10059: Read-only file system
12-13 14:31:02.606 2211-2211/com.example.propietario.myapplication 
E/SQLiteLog: (1) no such table: Table1
12-13 14:31:02.607 2211-2211/com.example.propietario.myapplication 
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.propietario.myapplication, PID: 2211
android.database.sqlite.SQLiteException: no such table: Table1 (code 1): , 
while compiling: Select Address from Table1 where Name =''
    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.example.propietario.myapplication.DataBaseAccess.getAddress(DataBaseAccess.java:48)
    at com.example.propietario.myapplication.MainActivity$1.onClick(MainActivity.java:37)
    at android.view.View.performClick(View.java:4756)
    at android.view.View$PerformClick.run(View.java:19749)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5221)
    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:899)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

The Codes are as follows:

MainActivity.java:

package com.example.propietario.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

public EditText name;
public Button query_button;
public TextView result_address;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    name=findViewById(R.id.name);
    query_button=findViewById(R.id.query_button);
    result_address=findViewById(R.id.result);

    //onclicklistener
    query_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //instance of database access

            DataBaseAccess dataBaseAccess=DataBaseAccess.getInstance(getApplicationContext());
            dataBaseAccess.open();

            //getting string

            String n=name.getText().toString();
            String address = dataBaseAccess.getAddress(n);


            //set to textview

            result_address.setText(address);

            dataBaseAccess.close();

            //database conection closed

        }
    });
}
}

DatabaseOpenHelper.java:

package com.example.propietario.myapplication;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseOpenHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="MyExternalDatabase.db";
private static final int  DATABASE_VERSION=1;

//constructor

public DataBaseOpenHelper(Context context){
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {

}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}
}

DataBaseAccess.java:

package com.example.propietario.myapplication;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseAccess {
private SQLiteOpenHelper openHelper;
private SQLiteDatabase db;
private static DataBaseAccess instance;
Cursor c = null;

//private constructor

private DataBaseAccess(Context context){
    this.openHelper=new DataBaseOpenHelper(context);

}

//to return
public static DataBaseAccess getInstance(Context context){
    if (instance==null){
        instance=new DataBaseAccess(context);

    }
    return instance;
}

//to open database

public void open(){
    this.db=openHelper.getWritableDatabase();

}

//close database conetcion

public void close(){
    if(db!=null){
        this.db.close();
    }
}

//method to query

public String getAddress(String name){
    c=db.rawQuery("Select Address from Table1 where Name ='"+name+"'", new String[]{});
    StringBuffer buffer = new StringBuffer();
    while(c.moveToNext()){
        String address = c.getString(0);
        buffer.append(""+address);
    }
    return buffer.toString();
}

}

Add the lines you indicated, but I'm not sure if I added them correctly because it still does not work, these are the codes:

DatabaseOpenHelper.java:

package com.example.diego.databasetest2;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;


public class DatabaseOpenHelper extends SQLiteAssetHelper {
private static final String 
DATABASE_NAME="MyExternaldatabase.db";
private static final int DATABASE_VERSION=1;

public void onCreate(SQLiteDatabase sqLiteDatabase){
    sqLiteDatabase.execSQL(DataBase.CREATE_TABLE);
}

//constructor

public DatabaseOpenHelper(Context context){
    super(context, DATABASE_NAME,null, DATABASE_VERSION);

}


}

In order to create and call the database, should I create another class?

    
asked by Diego Vente 13.12.2018 в 21:02
source

1 answer

0

The table you are trying to access has not been created:

  

SQLiteException: no such table: Table1 (code 1)

you must create in the method onCreate() of your class DataBaseOpenHelper :

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {

}

You must create your script to create the table and call it from the onCreate() method, example:

public static final String CREATE_TABLE = "CREATE TABLE Table1 ("
        + "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
        + "name TEXT);";
...
...

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE);
}
  • It is important that when you implement the creation of the table, you eliminate the cache of the application or the application so that the correct structure of the database can be created again with the table.
answered by 14.12.2018 в 00:06