没有这样的表例外的SQLite [英] no such table exception SQLite

查看:175
本文介绍了没有这样的表例外的SQLite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code是如下内容:

My code is now as follows:

主要应用的Java:

    String TAG = "DATABASES";    
    try {

        String destPath = "/data/data/" + getPackageName()
                + "/databases/data.db";

        File f = new File(destPath);
        if(!f.exists()){
            Log.v(TAG,"Dest DB doesn't exist");
            InputStream in = getAssets().open("airports.db");
            OutputStream out = new FileOutputStream(destPath);

            byte[] buffer = new byte[1024];
            int length;
            while ((length = in.read(buffer)) > 0) {
                out.write(buffer, 0, length);
            }
            in.close();
            out.close();
        } else {
            Log.v(TAG,"File exists: " + destPath);  

        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        Log.v(TAG,"ioexeption");
        e.printStackTrace();
    }

    DBManager dbManager =  new DBManager(this);
    Log.v(TAG,"Database is there with version: "+dbManager.getReadableDatabase().getVersion());
    //String sql = "select * from airports where IATA='GRO' ";
    String sql = "select count(*) from airports";


    SQLiteDatabase db = dbManager.getReadableDatabase();
    Cursor cursor = db.rawQuery(sql, null);
    Log.v(TAG,"Query Result:"+cursor);


    cursor.close();
    db.close();
    dbManager.close();

我的DBManager.java:

package com.jammo.mywidget4;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBManager extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String TAG = "DATABASES";

public DBManager(Context context) {
    super(context, "data.db", null, DATABASE_VERSION);

}

@Override
public void onCreate(SQLiteDatabase db) {
    Log.v(TAG,"On create Called:"+db.getPath());
}

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

现在的执行运行在DB开始的main.java ok了,但是失败于它试图执行rawQuery()的下一行

Now the execution runs ok in the main.java on initiation of "db", however fails on the next line where it tries to execute rawQuery()

FYI从机场COUNT(*)在我的sqlite的数据库管理器GUI运行返回1650行。

FYI "select count(*) from airports" run on my Sqlite DB Manager GUI returns 1650 rows.

错误日志说:

03-04 21:54:24.200: E/AndroidRuntime(11513): FATAL EXCEPTION: main
03-04 21:54:24.200: E/AndroidRuntime(11513): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jammo.mywidget4/com.jammo.mywidget4.SectorInfo}: android.database.sqlite.SQLiteException: no such table: airports: , while compiling: select count(*) from airports

本地/assets/airports.db似乎是被发现并复制到/data/data/mypackage/databases/data.db为文件中找到

Local /assets/airports.db "seems" to be being detected and copied to /data/data/mypackage/databases/data.db as "file is found"

非常感谢
Ĵ

推荐答案

尝试没有使用DBManager类。

Try it without using your DBManager class.

注释掉这一行...

DBManager dbManager =  new DBManager(this);

...然后通过更换这行打开数据库明确...

...then open the database explicitly by replacing this line...

SQLiteDatabase db = dbManager.getReadableDatabase();

... ...用

...with...

SQLiteDatabase db = SQLiteDatabase.openDatabase(destPath, null, SQLiteDatabase.OPEN_READONLY);

然后清除应用数据,从而将数据库从你的资产重新复制目录

编辑:虽然我的建议的工作,它实际上只是一种变通方法,你的问题的根源是这样的...

Even though my suggestion worked, it's actually just a workaround and the root of your problem was this...

String destPath = "/data/data/" + getPackageName() + "/databases/data.db";

您应该避免使用与Android硬codeD路径 - 他们可能有很好的许多设备,但没有保证,他们将与所有设备,甚至与Android所有版本的

You should avoid using hard-coded paths with Android - they may work fine for many devices but there is no guarantee they'll work with all devices or even with all versions of Android.

问题是你创建/复制你的数据库,以这条道路,但DBManager类是presumably创建位于文件系统不同的路径上某处一个空数据库。

The problem was you were creating / copying your database to that path but the DBManager class was presumably creating an empty database located somewhere on a different path in the file-system.

要解决这个问题对所有设备和Android版本,复制数据库之前,我会用...

To fix this for all devices and Android versions, before copying the database, I would use...

SQLiteDatabase.openOrCreateDatabase("data.db", null)

...创建一个虚拟(空)数据库。然后,我会打电话...

...to create a 'dummy' (empty) database. Then I'd call...

getDatabasePath("data.db")

...去 data.db 的绝对路径。然后,您可以复制操作过程中覆盖数据库,从此你可以使用一个类扩展 SQLiteOpenHelper 键,它会找到正确的数据库。

...to get the absolute path to data.db. You can then overwrite that database during the copy operation and from then on you can use a class which extends SQLiteOpenHelper and it will find the correct database.

这篇关于没有这样的表例外的SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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