Java 安卓;没有这样的表格”;在某些设备上
我的外部数据库有问题。该数据库在大约60%的设备上运行良好,但例如在Nexus7和HTC上,它会在logcat中抛出“无此类表”错误。我很困惑该怎么处理它。 然而,我非常肯定这个表格是存在的。它可以在我家的设备上工作 DataBaseHelper类Java 安卓;没有这样的表格”;在某些设备上,java,android,eclipse,sqlite,Java,Android,Eclipse,Sqlite,我的外部数据库有问题。该数据库在大约60%的设备上运行良好,但例如在Nexus7和HTC上,它会在logcat中抛出“无此类表”错误。我很困惑该怎么处理它。 然而,我非常肯定这个表格是存在的。它可以在我家的设备上工作 DataBaseHelper类 public class DataBaseHelper extends SQLiteOpenHelper { public static final String KEY_ID = "_id"; public static final String
public class DataBaseHelper extends SQLiteOpenHelper {
public static final String KEY_ID = "_id";
public static final String KEY_QUOTE = "quote";
public static final String KEY_AUTHOR = "author";
public static final String TABLE_QUOTES = "quotes";
public static final String KEY_FAV = "fav";
private static String DB_PATH = "/data/data/com.radoman.gameofthrones/databases/";
private final Context myContext;
private static String DB_NAME = "database.db";
private SQLiteDatabase myDataBase;
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 11 );
this.myContext = context;
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
this.close();
try {
this.getReadableDatabase();
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase(){
File dbFile = new File(DB_PATH + DB_NAME);
return dbFile.exists();
}
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
createDataBase();
} catch (IOException e) {
Log.e("copy_db", "Error copying database");
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
//CRUD
public void addQuote(Quote quote)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_QUOTE, quote.getQuote());
values.put(KEY_AUTHOR, quote.getAuthor());
db.insert(TABLE_QUOTES, null, values);
db.close();
}
public Quote getQuote(int id)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_QUOTES, new String[] {KEY_ID,KEY_QUOTE,KEY_AUTHOR,KEY_FAV}, KEY_ID + "=?", new String[] { String.valueOf(id)}, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Quote quote = new Quote(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2), cursor.getInt(3));
return quote;
}
public List<Quote> getAllQuotes()
{
List<Quote> quoteList = new ArrayList<Quote>();
String selectQuery = "SELECT * FROM " + TABLE_QUOTES;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst())
{
do
{
Quote quote = new Quote();
quote.setID(Integer.parseInt(cursor.getString(0)));
quote.setQuote(cursor.getString(1));
quote.setAuthor(cursor.getString(2));
quoteList.add(quote);
} while(cursor.moveToNext());
}
return quoteList;
}
public int ifFav(int id)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_QUOTES, new String[] {KEY_ID,KEY_QUOTE,KEY_AUTHOR,KEY_FAV}, KEY_ID + "=?", new String[] { String.valueOf(id)}, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Quote quote = new Quote(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2), cursor.getInt(3));
if (quote.getFav() == 1)
{
db.close();
return 1;
}
else
{
db.close();
return 0;
}
}
public void addFav(int id)
{
SQLiteDatabase db = this.getWritableDatabase();
String query = "UPDATE quotes SET fav=1 WHERE _id=" +id;
db.execSQL(query);
db.close();
}
public void nullFav(int id)
{
SQLiteDatabase db = this.getWritableDatabase();
String query = "UPDATE quotes SET fav=0 WHERE _id=" +id;
db.execSQL(query);
db.close();
}
public List<HashMap<String, String>> getCharQuote(String character)
{
String selectQuery;
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
if(character.equals("All quotes"))
{
selectQuery = "SELECT * FROM " + TABLE_QUOTES;
}
else if (character.equals("Other characters"))
{
selectQuery = "SELECT * FROM "+TABLE_QUOTES+
" WHERE author NOT IN('Syrio Forel','Ned Stark','Daenerys Targaryen'" +
",'Margaery Tyrell','Robert Baratheon','Tyrion Lannister','Ser Jorah Mormont','Bran Stark','Cersei Lannister','Jaime Lannister');";
}
else
{
selectQuery = "SELECT * FROM " + TABLE_QUOTES+" WHERE author='"+character+"'";
}
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst())
{
do
{
HashMap<String, String> map = new HashMap<String, String>();
map.put("quote", cursor.getString(2));
map.put("fav", cursor.getString(0));
map.put("id",cursor.getString(1));
fillMaps.add(map);
} while(cursor.moveToNext());
}
return fillMaps;
}
public List<HashMap<String, String>> getAllFavQuote()
{
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT * FROM " + TABLE_QUOTES+" WHERE fav=1";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst())
{
do
{
HashMap<String, String> map = new HashMap<String, String>();
map.put("quote", cursor.getString(2));
map.put("id", cursor.getString(1));
fillMaps.add(map);
} while(cursor.moveToNext());
}
return fillMaps;
}
如果您已经安装了旧版本的应用程序,则在未显示表时,仅安装新版本不会替换旧数据库。从设备上卸载应用程序并安装更新的应用程序应该可以工作。或者执行
onUpgrade
触发器
编辑:
您也可以尝试替换此行:
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
为此:
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
我只是想知道为什么要将DB_名称而不是DB_路径传递给超级构造函数?硬编码的数据库路径似乎是个坏主意。为什么不使用onCreate中提供的SQLiteDatabase对象来创建表呢?我怀疑DB不是您所认为的那样,而是我所怀疑的那样。它应该传递DB_名称。SQLiteDatabase对象是什么意思?全新和首次安装。您也可以尝试使用无本地化的\u拼贴器而不是读写来打开数据库。我想我也有这个问题,检查编辑后的答案。
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);