Android和QUOT;没有这样的表"在某些设备上 [英] Android "no such table" on some devices

查看:229
本文介绍了Android和QUOT;没有这样的表"在某些设备上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有外部数据库的问题。该数据库工作正常的60%左右的设备,但例如在Nexus 7和HTC它的logcat抛出没有这样的表错误。我是pretty混淆它做什么。
但是我非常肯定,该表存在。和它的作品在我家里的设备。

DataBaseHelper类

 公共类DataBaseHelper扩展SQLiteOpenHelper {公共静态最后弦乐KEY_ID =_id;
公共静态最后弦乐KEY_QUOTE =引用;
公共静态最后弦乐KEY_AUTHOR =作者;
公共静态最后弦乐TABLE_QUOTES =引号;
公共静态最后弦乐KEY_FAV =最爱;
私人静态字符串DB_PATH =/data/data/com.radoman.gameofthrones/databases/;私人最终上下文myContext;私人静态字符串DB_NAME =database.db;私人SQLiteDatabase MYDATABASE;公共DataBaseHelper(上下文的背景下){    超(背景下,DB_NAME,NULL,11);
    this.myContext =背景;
}公共无效的CreateDatabase()抛出IOException    布尔dbExist = checkDataBase();    如果(dbExist){
        //什么也不做 - 已存在于数据库
    }其他{        //通过调用此方法与空的数据库将被创建到默认的系统路径
           //你的应用程序,所以我们要能够覆盖该数据库与我们的数据库。
        this.getReadableDatabase();
        this.close();        尝试{
            this.getReadableDatabase();
            copyDataBase();        }赶上(IOException异常五){            抛出新的错误(错误复制数据库);        }
    }}私人布尔checkDataBase(){    文件DBFILE =新的文件(DB_PATH + DB_NAME);
    返回dbFile.exists();
}私人无效copyDataBase()抛出IOException    //打开本地数据库的输入流
    InputStream的myInput = myContext.getAssets()开(DB_NAME)。    //路径刚刚创建的空分贝
    字符串outFileName = DB_PATH + DB_NAME;    //打开空分贝的输出流
    的OutputStream myOutput =新的FileOutputStream(outFileName);    //传递从inputfile中字节到OUTPUTFILE
    字节[]缓冲区=新的字节[1024];
    INT长;
    而((长度= myInput.read(缓冲液))大于0){
        myOutput.write(缓冲液,0,长度);
    }    //关闭流
    myOutput.flush();
    myOutput.close();
    myInput.close();}公共无效的openDatabase()抛出的SQLException {    //打开数据库
    字符串mypath中= DB_PATH + DB_NAME;
    MYDATABASE = SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READWRITE);
}@覆盖
公共同步无效的close(){        如果(MYDATABASE!= NULL)
            myDataBase.close();        super.close();}@覆盖
公共无效的onCreate(SQLiteDatabase DB){    尝试{
        的CreateDatabase();
    }赶上(IOException异常五){
        Log.e(copy_db,错误复制数据库);
        e.printStackTrace();
    }}@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){}// CRUD    公共无效addQuote(行情报价)
    {
        SQLiteDatabase分贝= this.getWritableDatabase();
        ContentValues​​值=新ContentValues​​();
        values​​.put(KEY_QUOTE,quote.getQuote());
        values​​.put(KEY_AUTHOR,quote.getAuthor());        db.insert(TABLE_QUOTES,空,价值);
        db.close();
    }    公开报价为getQuote(INT ID)
    {
        SQLiteDatabase分贝= this.getReadableDatabase();        光标光标= db.query(TABLE_QUOTES,新的String [] {KEY_ID,KEY_QUOTE,KEY_AUTHOR,KEY_FAV},KEY_ID +=?,新的String [] {将String.valueOf(ID)},NULL,NULL,NULL,NULL );
        如果(指针!= NULL)
            cursor.moveToFirst();        引用引用=新的报价(的Integer.parseInt(cursor.getString(0)),
                cursor.getString(1),cursor.getString(2),cursor.getInt(3));        返回报价;
    }    公开名单<&报价GT; getAllQuotes()
    {
            清单<&报价GT; quoteList =新的ArrayList<&报价GT;();
            字符串selectQuery =SELECT * FROM+ TABLE_QUOTES;
            SQLiteDatabase分贝= this.getWritableDatabase();
            光标光标= db.rawQuery(selectQuery,NULL);            如果(cursor.moveToFirst())
            {
                做
                {
                    引用引用=新报价();
                    quote.setID(的Integer.parseInt(cursor.getString(0)));
                    quote.setQuote(cursor.getString(1));
                    quote.setAuthor(cursor.getString(2));
                    quoteList.add(报价);
                }而(cursor.moveToNext());
            }
            返回quoteList;
    }    公众诠释ifFav(INT ID)    {
        SQLiteDatabase分贝= this.getReadableDatabase();        光标光标= db.query(TABLE_QUOTES,新的String [] {KEY_ID,KEY_QUOTE,KEY_AUTHOR,KEY_FAV},KEY_ID +=?,新的String [] {将String.valueOf(ID)},NULL,NULL,NULL,NULL );
        如果(指针!= NULL)
            cursor.moveToFirst();        引用引用=新的报价(的Integer.parseInt(cursor.getString(0)),
                cursor.getString(1),cursor.getString(2),cursor.getInt(3));        如果(quote.getFav()== 1)
        {
            db.close();
            返回1;
        }        其他
        {
            db.close();
            返回0;
        }    }    公共无效addFav(INT ID)
    {
        SQLiteDatabase分贝= this.getWritableDatabase();
        查询字符串=UPDATE报价SET最爱= 1 WHERE _id =+ ID;
        db.execSQL(查询);
        db.close();
    }    公共无效nullFav(INT ID)
    {
        SQLiteDatabase分贝= this.getWritableDatabase();
        查询字符串=UPDATE报价SET最爱= 0 WHERE _id =+ ID;
        db.execSQL(查询);
        db.close();
    }    公开名单<&HashMap的LT;字符串,字符串>> getCharQuote(串字符)
    {        串selectQuery;
        清单<&HashMap的LT;字符串,字符串>> fillMaps =新的ArrayList<&HashMap的LT;字符串,字符串>>();
        如果(character.equals(所有的报价))
        {
            selectQuery =SELECT * FROM+ TABLE_QUOTES;
        }
        否则,如果(character.equals(其他字符))
        {
            selectQuery =SELECT * FROM+ TABLE_QUOTES +
                WHE​​RE作者NOT IN('Syrio福雷尔,艾德·史塔克,丹妮莉丝坦格利安'+
                ,Margaery Tyrell的,劳勃·拜拉席恩,提利昂·兰尼斯特,辑乔拉·莫尔蒙爵士,布兰·史塔克,瑟曦兰尼斯特,詹姆·兰尼斯特');;
        }
        其他
        {             selectQuery =SELECT * FROM+ TABLE_QUOTES +WHERE笔者='+人物+';
        }
        SQLiteDatabase分贝= this.getWritableDatabase();
        光标光标= db.rawQuery(selectQuery,NULL);        如果(cursor.moveToFirst())
        {
            做
            {                HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
                map.put(引用,cursor.getString(2));
                map.put(最爱,cursor.getString(0));
                map.put(ID,cursor.getString(1));
                fillMaps.add(地图);            }而(cursor.moveToNext());
        }
        返回fillMaps;    }    公开名单<&HashMap的LT;字符串,字符串>> getAllFavQuote()
    {        清单<&HashMap的LT;字符串,字符串>> fillMaps =新的ArrayList<&HashMap的LT;字符串,字符串>>();
        字符串selectQuery =SELECT * FROM+ TABLE_QUOTES +WHERE最爱= 1;
        SQLiteDatabase分贝= this.getWritableDatabase();
        光标光标= db.rawQuery(selectQuery,NULL);        如果(cursor.moveToFirst())
        {
            做
            {                HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
                map.put(引用,cursor.getString(2));
                map.put(ID,cursor.getString(1));
                fillMaps.add(地图);            }而(cursor.moveToNext());
        }
        返回fillMaps;    }

}

和logcat的错误:

 了java.lang.RuntimeException:无法启动活动ComponentInfo {com.radoman.gameofthrones / com.radoman.gameofthrones.FavQuotesActivity}:android.database.sqlite.SQLiteException:没有这样的表:引号(code 1),在编译:SELECT * FROM WHERE报价=最爱1
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
在android.app.ActivityThread.access $ 600(ActivityThread.java:141)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)
在android.os.Handler.dispatchMessage(Handler.java:99)
在android.os.Looper.loop(Looper.java:137)
在android.app.ActivityThread.main(ActivityThread.java:5039)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:511)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
在dalvik.system.NativeStart.main(本机方法)
android.database.sqlite.SQLiteException:引起没有这样的表:引号(code 1),在编译:SELECT * FROM WHERE报价=最爱1
在android.database.sqlite.SQLiteConnection.native prepareStatement(本机方法)
在android.database.sqlite.SQLiteConnection.acquire preparedStatement(SQLiteConnection.java:882)
在android.database.sqlite.SQLiteConnection。prepare(SQLiteConnection.java:493)
在android.database.sqlite.SQLiteSession。prepare(SQLiteSession.java:588)
在android.database.sqlite.SQLiteProgram(SQLiteProgram.java:58)
在android.database.sqlite.SQLiteQuery(SQLiteQuery.java:37)
在android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
在android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
在android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
在com.radoman.gameofthrones.DataBaseHelper.getAllFavQuote(DataBaseHelper.java:290)
在com.radoman.gameofthrones.FavQuotesActivity.onCreate(FavQuotesActivity.java:72)
在android.app.Activity.performCreate(Activity.java:5104)
在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
... 11更多


解决方案

如果您已经安装了您的应用程序,当表没有被presented,只是在安装新版本的旧版本将不会取代旧的数据库。卸载从设备应用程序并安装更新应该工作。或使执行 onUpgrade 触发。

编辑:

您也可以尝试替换此行:

  MYDATABASE = SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READWRITE);

本:

  MYDATABASE = SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.NO_LOCALIZED_COLLATORS);

I am having problems with external database. The database works fine on around 60% of devices, but for example on Nexus 7 and HTC it throws "no such table" error in logcat. I am pretty confused what to do with it. However I AM VERY SURE THAT THE TABLE EXISTS. And it works on my devices at home.

DataBaseHelper Class

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;

    }

}

And logcat error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.radoman.gameofthrones/com.radoman.gameofthrones.FavQuotesActivity}: android.database.sqlite.SQLiteException: no such table: quotes (code 1): , while compiling: SELECT * FROM quotes WHERE fav=1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: no such table: quotes (code 1): , while compiling: SELECT * FROM quotes WHERE fav=1
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
at com.radoman.gameofthrones.DataBaseHelper.getAllFavQuote(DataBaseHelper.java:290)
at com.radoman.gameofthrones.FavQuotesActivity.onCreate(FavQuotesActivity.java:72)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
... 11 more

解决方案

if you have installed older version of your app, when the table is not been presented, just installing the new version won't replace the old database. Uninstall the app from the device and install the newer one should work. Or make implementation of the onUpgrade trigger.

EDIT:

you might also try to replace this line:

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

with this:

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

这篇关于Android和QUOT;没有这样的表&QUOT;在某些设备上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆