-1

I need to use an internal database already populated in my project, I already searched the internet and tried two ways, neither of them came to work, the second now presents this error:

no such table: test (code 1):, while compiling: SELECT * FROM test

I need to solve this, somebody knows some way to solve this, I need an internal bd already populated, someone knows of some api or how to solve it? I'm developing in the latest version of the android *.

Database class

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

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

DatabaseAccess class

public class DatabaseAccess {
private SQLiteOpenHelper openHelper;
private SQLiteDatabase database;
private static DatabaseAccess instance;


private DatabaseAccess(Context context) {
    this.openHelper = new DatabaseOpenHelper(context);
}

/**
 * Return a singleton instance of DatabaseAccess.
 *
 * @param context the Context
 * @return the instance of DabaseAccess
 */
public static DatabaseAccess getInstance(Context context) {
    if (instance == null) {
        instance = new DatabaseAccess(context);
    }
    return instance;
}

/**
 * Open the database connection.
 */
public void open() {
    this.database = openHelper.getWritableDatabase();
}

/**
 * Close the database connection.
 */
public void close() {
    if (database != null) {
        this.database.close();
    }
}


public List<String> getQuotes() {
    List<String> list = new ArrayList<>();
    Cursor cursor = database.rawQuery("SELECT * FROM teste", null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        list.add(cursor.getString(1));
        cursor.moveToNext();
    }
    cursor.close();
    return list;
 }
}

MainActivity class

public class MainActivity extends AppCompatActivity {
private ListView lv;
private ListaAdapter adapter;
private List<Produtos> lista;
//private DataBaseHelper dataBaseHelper;
private ListView listView;

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

    this.listView = (ListView) findViewById(R.id.lv);
    DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
    databaseAccess.open();
    List<String> quotes = databaseAccess.getQuotes();
    databaseAccess.close();

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, quotes);
    listView.setAdapter(adapter);
 }
}
fin444
  • 725
  • 2
  • 14
  • 27

1 Answers1

1

The error shows that you have a database but that there is no table called test in that database.

It there were no teste.db file then you would get an exception e.g. :-

Caused by: com.readystatesoftware.sqliteasset.SQLiteAssetHelper$SQLiteAssetException: Missing databases/teste.db file (or .zip, .gz archive) in assets, or target folder not writable

So you need to check that the teste.db does actually include a table named test (perhaps you called the table teste). It may be that you didn't save the changes after adding the table.

However, note that the message you have displayed does not correlate with the code that is in the code you call the table teste, as per :-

Cursor cursor = database.rawQuery("SELECT * FROM teste", null);

I've checked and run your code, once you correct the table name issue (i.e. use the name of the table in the database (adding it if that is the issue)), you code works and populates and displays the ListView.

This link shows some utilities that may be of use (logDatabaseInfo could be useful) Are there any methods that assist with resolving common SQLite issues?. The logDatabaseInfo includes a list of the table.

Example

By adding the following method to DatabaseAccess.java

    public void logDBInfo() {
        CommonSQLiteUtilities.logDatabaseInfo(database);
    }

And then changing MainActivity.java to be (see //<<<< for changes made) :-

public class MainActivity extends AppCompatActivity {
    private ListView lv;
    //private ListaAdapter adapter; //<<<< Commented out
    //private List<Produtos> lista; //<<<< Commented out
    //private DataBaseHelper dataBaseHelper;
    private ListView listView;

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

        this.listView = (ListView) findViewById(R.id.lv);
        DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
        databaseAccess.open();
        databaseAccess.logDBInfo(); //<<<< Added
        List<String> quotes = databaseAccess.getQuotes();
        databaseAccess.close();

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, quotes);
        listView.setAdapter(adapter);
    }
}

Example output:-

02-06 20:48:28.145 1715-1715/? W/SQLiteAssetHelper: copying database from assets...
02-06 20:48:28.145 1715-1715/? W/SQLiteAssetHelper: database copy complete
02-06 20:48:28.185 1715-1715/? I/SQLiteAssetHelper: successfully opened database teste.db
02-06 20:48:28.185 1715-1715/? D/SQLITE_CSU: DatabaseList Row 1 Name=main File=/data/data/examples.mjt.sqliteassethelperexample/databases/teste.db
02-06 20:48:28.185 1715-1715/? D/SQLITE_CSU: Database Version = 1
02-06 20:48:28.189 1715-1715/? D/SQLITE_CSU: Table Name = test Created Using = CREATE TABLE `test` (
                                                `_id`   INTEGER,
                                                `col1`  TEXT,
                                                `col2`  TEXT,
                                                PRIMARY KEY(`_id`)
                                             )
02-06 20:48:28.189 1715-1715/? D/SQLITE_CSU: Table = test ColumnName = _id ColumnType = INTEGER Default Value = null PRIMARY KEY SEQUENCE = 1
02-06 20:48:28.189 1715-1715/? D/SQLITE_CSU: Table = test ColumnName = col1 ColumnType = TEXT Default Value = null PRIMARY KEY SEQUENCE = 0
02-06 20:48:28.189 1715-1715/? D/SQLITE_CSU: Table = test ColumnName = col2 ColumnType = TEXT Default Value = null PRIMARY KEY SEQUENCE = 0
02-06 20:48:28.189 1715-1715/? D/SQLITE_CSU: Table Name = android_metadata Created Using = CREATE TABLE android_metadata (locale TEXT)
02-06 20:48:28.189 1715-1715/? D/SQLITE_CSU: Table = android_metadata ColumnName = locale ColumnType = TEXT Default Value = null PRIMARY KEY SEQUENCE = 0

Notes

  • The messages issued from SQLiteAssetHelper have been included (first run, subsequent run you just get the last successfully opened database teste.db)
  • Obviously this is for a database I created
MikeT
  • 51,415
  • 16
  • 49
  • 68