I've tried the solutions posted in this thread:
SQL Android Error: no such table
... To no avail.
I'm simply trying to open and fetch all the rows from a pre-build SQLite database I've made via command line to my app. It saved the file as "names.db". I went ahead and moved it to a folder in my main project file called "assets" ("C:\...\AndroidStudioProjects\MyProject\app\src\main\assets\names.db"). I've been using this stack solution as a template, more or less:
Here is the current code as per the classes my stack trace is complaining about:
public class MainActivity extends Activity {
private NamesDataSource dataSource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataSource = new NamesDataSource(this);
try {
dataSource.open();
}
catch (SQLException se) {
se.printStackTrace();
}
ListView lv = (ListView) findViewById(R.id.ListView);
List<Names> values = dataSource.getAllNames();
ArrayAdapter<Names> adapter = new ArrayAdapter<Names>(this, android.R.layout.simple_list_item_1, values);
lv.setAdapter(adapter);
}
}
// DBHelper class
public class MySQLiteHelper extends SQLiteOpenHelper {
public static final String TABLE_NAMES = "names";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "name";
private static final String DATABASE_NAME = "names.db";
private static String DB_PATH = "";
private static final int DATABASE_VERSION = 3;
private static final String DATABASE_CREATE = "CREATE TABLE names (_id integer PRIMARY KEY, name text, phone text, email text);";
private final Context mContext;
private SQLiteDatabase mDataBase;
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
this.mContext = context;
}
public void createDataBase() throws IOException {
//If database not exists copy it from the assets
boolean mDataBaseExist = checkDataBase();
if(!mDataBaseExist)
{
this.getReadableDatabase();
try
{
//Copy the database from assets
copyDataBase();
Log.e("MySQLiteHelper", "createDatabase database created");
}
catch (IOException mIOException)
{
throw new Error("ErrorCopyingDataBase");
}
}
}
private boolean checkDataBase() {
File dbFile = new File(DB_PATH + DATABASE_NAME);
//Log.v("dbFile", dbFile + " "+ dbFile.exists());
return dbFile.exists();
}
private void copyDataBase() throws IOException
{
InputStream mInput = mContext.getAssets().open(DATABASE_NAME);
String outFileName = DB_PATH + DATABASE_NAME;
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer))>0)
{
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}
public boolean openDataBase() throws SQLException
{
String mPath = DB_PATH + DATABASE_NAME;
mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
return mDataBase != null;
}
@Override
public void onCreate(SQLiteDatabase database) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Cursor fetchRows() {
String query = "SELECT * FROM " + TABLE_NAMES;
Cursor cursor = mDataBase.rawQuery(query, null);
return cursor;
}
@Override
public synchronized void close()
{
if(mDataBase != null)
mDataBase.close();
super.close();
}
}
// DAO/Adapter Class
public class NamesDataSource {
private SQLiteDatabase db;
private MySQLiteHelper dbHelper;
private String[] allNames = {
MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_NAME
};
public NamesDataSource(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
try {
dbHelper.createDataBase();
}
catch (Exception ioe) {
throw new Error("Unable to create database");
}
try {
dbHelper.openDataBase();
}
catch(Exception se){
se.printStackTrace();
}
}
public void close() {
dbHelper.close();
}
public List<Names> getAllNames() {
List<Names> names = new ArrayList<>();
Cursor cursor = dbHelper.fetchRows();
cursor.moveToFirst();
while(!cursor.isAfterLast()) {
Names row = cursorToName(cursor);
names.add(row);
cursor.moveToNext();
}
cursor.close();
return names;
}
private Names cursorToName(Cursor cursor) {
Names name = new Names();
name.setIds(cursor.getLong(0));
name.setName(cursor.getString(1));
name.setPhone(cursor.getString(2));
name.setEmail(cursor.getString(3));
return name;
}
}
And finally, the error report:
android.database.sqlite.SQLiteException: no such table: names (code 1): , while compiling: SELECT * FROM names
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.user.myproject.MySQLiteHelper.fetchRows(MySQLiteHelper.java:96)
at com.example.user.myproject.NamesDataSource.getAllNames(NamesDataSource.java:49)
at com.example.user.myproject.MainActivity.onCreate(MainActivity.java:44)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
Thanks a ton for anyone willing to take a look at this. Cheers.
Aucun commentaire:
Enregistrer un commentaire