sqlite android no such table,Android SQLite “no such table” exception

I'm starting into SQLite and databases in Android and I just found this issue. I have read a lot of similar questions but I have not found a solution yet.

This is the error I'm getting:

E/AndroidRuntime(529): Caused by: android.database.sqlite.SQLiteException: no such table: ligas_bd: , while compiling: SELECT * FROM ligas_bd

This is my code.

DBHelper myDbHelper = new DBHelper(this);

myDbHelper = new DBHelper(this);

try {

myDbHelper.createDataBase();

} catch (IOException ioe) {

throw new Error("Unable to create database");

}

try {

myDbHelper.open();

SQLiteDatabase db = null;

db = myDbHelper.getWritableDatabase();

String[] args = new String[] {""};

Cursor c = db.rawQuery(" SELECT * FROM ligas_bd", args);

if (c.moveToFirst()) {

do {

String cod = c.getString(0);

String nombre = c.getString(1);

String flag = c.getString(0);

String cod_url = c.getString(0);

ligas.add(new Item_Liga(nombre, flag, cod, cod_url));

} while(c.moveToNext());

}

}catch(SQLException sqle){

throw sqle;

}

DBHelper.java

public class DBHelper extends SQLiteOpenHelper{

private static String DB_NAME = "ligas_bd";

private SQLiteDatabase myDataBase;

private final Context myContext;

public DBHelper(Context context) {

super(context, DB_NAME, null, 1);

this.myContext = context;

}

public void createDataBase() throws IOException{

boolean dbExist = checkDataBase();

if(dbExist){ }

else {

this.getReadableDatabase();

try {

copyDataBase();

} catch (IOException e) {

throw new Error("Error copiando Base de Datos");

}

}

}

private boolean checkDataBase(){

SQLiteDatabase checkDB = null;

try {

String myPath = DB_PATH + DB_NAME;

checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

} catch(SQLiteException e) { }

if(checkDB != null) {

checkDB.close();

}

return checkDB != null ? true : false;

}

private void copyDataBase() throws IOException{

InputStream myInput = myContext.getAssets().open(DB_NAME);

String outFileName = DB_PATH + DB_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 void open() throws SQLException{

try {

createDataBase();

} catch (IOException e) {

throw new Error("Ha sido imposible crear la Base de Datos");

}

String myPath = DB_PATH + DB_NAME;

myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

}

@Override

public synchronized void close() {

if(myDataBase != null)

myDataBase.close();

super.close();

}

@Override

public void onCreate(SQLiteDatabase db) { }

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }

I also checked in my phone with root explorer data/data/mypackagename/databases and the database appears there as normal.

My database and the main table have the same name, as you can see in this pic of SQLite database browser:

0985b78a2cd3ff1ff253a3c86bd5b2de.png

I am not creating the table, I created it before using SQLite Database Browser and I am just trying to read from it

I don't know how, but I finally fixed it. I just started again, this time following this tutorial.

Hope it helps!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值