问题描述
即时通讯开始进入的SQLite数据库,并在Android中,我只是发现了这个问题。有红色的有很多类似的问题,但我还没有找到一个解决办法。
Im starting into SQLite and databases in Android and I just found this issue. Have red a lot of similar questions but I have not found a solution yet.
这是错误code我越来越:
This is the error code I'm getting:
E / AndroidRuntime(529):由:android.database.sqlite.SQLiteException:没有这样的表:ligas_bd:在编制:SELECT * FROM ligas_bd
这是我的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); <---- Error in this line
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
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) { }
我还检查我的手机,根浏览器的数据/数据/ mypackagename /数据库和数据库apears有正常。感谢您的建议。
I also checked in my phone with root explorer data/data/mypackagename/databases and the database apears there as normal.Thanks in advice.
编辑:我的数据库和主表具有相同的名称,你可以在这个PIC SQLite数据库浏览器中看到的 -
My database and the main table have the same name, as you can see in this pic of sqlite database browser-
EDIT2:我忘了说,我不是在创建表,我使用SQLite数据库浏览器之前创建它,我只是想从中读
I forgot to say that I am not creating the table, I created it before using SQLite Database Browser and I am just trying to read from it
EDIT3:不知道怎么回事,但我终于固定它。我刚刚再次启动,这一次下面的本教程。希望它可以帮助!
Don't know how, but I finally fixed it. I just started again, this time following this tutorial.Hope it helps!
推荐答案
这只是一种猜测,但我可以看到三种可能的事情发生在这里...
This is only a guess but I can see three possible things are happening here...
- 电话
this.getReadableDatabase()
在的CreateDatabase()
方法会自动创建一个空数据库。 - 您试图从你的
资产
文件夹失败或数据库被复制到错误的地方复制你的pre-创建的数据库。 - 在步骤1中(上图)创建的空数据库是,当你调用被查询的一个
db.rawQuery(...)
从主$ C $角
- Calling
this.getReadableDatabase()
in yourcreateDatabase()
method is automatically creating an empty database. - Your attempt to copy your pre-created database from your
assets
folder is failing or the database is being copied to the wrong place. - The empty database created in step 1 (above) is the one that is being queried when you call
db.rawQuery(...)
from your main code.
要进一步解释它是usefult看源代码SQLiteOpenHelper
To explain further it is usefult to look at the source for SQLiteOpenHelper
在创建 SQLiteOpenHelper
的实例,它设置的东西,如数据库的名称和版本,但它并没有创建数据库。相反,为 getReadableDatabase()
或 getWritableDatabase()
检查第一次调用查看数据库是否存在,如果它没有它创建它。在创建空数据库后,的onCreate(...)
方法被调用它,在你的情况下,什么也不做。
When you create an instance of SQLiteOpenHelper
it sets things like the database name and version but it does not create the database. Instead, the first call to either getReadableDatabase()
or getWritableDatabase()
checks to see if the database exists and if it doesn't it creates it. After creating the empty database, the onCreate(...)
method is called which, in your case, does nothing.
在以2点(上图) - 你说 DB_PATH
是 /数据/数据/ mypackagename /数据库
。如果真是这样的话,你有没有尾随 /
然后行...
On to point 2 (above) - you say that DB_PATH
is /data/data/mypackagename/databases
. If that is really the case and you have no trailing /
then the line...
String myPath = DB_PATH + DB_NAME;
...将设置 mypath中
到 /数据/数据/ mypackagename / databasesligas_db
。在这种情况下,即使从资产
成功复制,复制的数据库不会是在那里你希望它是。
...will set myPath
to /data/data/mypackagename/databasesligas_db
. In that case, even if the copy from assets
succeeds, the copied database isn't going to be where you expect it to be.
终于在3点(上图),当你调用 db.rawQuery(...)
的实例分贝
指的是由previous调用 getWritableDatabase返回()
。假设副本资产
已失败,或已被复制到不正确的路径,分贝
将指向在在步骤1中创建空数据库。
Finally on point 3 (above) when you call db.rawQuery(...)
the instance that db
points to is returned by the previous call to getWritableDatabase()
. Assuming the copy from assets
has failed or that it has been copied to the incorrect path, db
will be pointing at the empty database created in step 1.
您得到的错误是,该表不存在,这是不是一个错误,指示数据库不存在 - 唯一合乎逻辑的解释是,它是在一个空数据库执行查询,而不是一个具有从资产被复制
。
The error you get is that the table doesn't exist and it isn't an error indicating the database doesn't exist - the only logical explanation is that it's performing a query on an empty database and not the one that has been copied from assets
.
编辑:还有一件事我的意思是说。这是不使用硬codeD路径任何Android的目录是个好主意。例如 - 虽然大多数设备,其中创建的数据库将会是 /数据/数据/&LT的目录中,包名称&gt; /数据库
这不是一个保证。这是很安全的,如果你调用 getDatabasePath(...)
方法,它会返回一个文件
对象,它可以可用于获取路径,使用Android的数据库类数据库目录。
One other thing I meant to mention. It isn't a good idea to use hard-coded paths to any of the Android directories. For example - although on most devices, the directory where databases are created will be /data/data/<package-name>/databases
this is not a guarantee. It is much safer if you call the getDatabasePath(...)
method as it will return a File
object which can be used to get the path to the databases directory used by the Android database classes.
这篇关于Android的SQLite的&QUOT;没有这样的表&QUOT;例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!