android没有这样的表错误

时间:2012-04-24 10:50:18

标签: android sqlite

我在创建数据库类时遇到了这样的表错误。 logcat说没有这样的表。我一直试图用几种方法解决,不幸的是仍然有错误。以下是代码,错误是

  

android.database.sqlite.SQLiteException:没有这样的表:tbl_category :,编译时:SELECT * FROM tbl_category WHERE idx = 1

public class DatabaseManager extends SQLiteOpenHelper {
private SQLiteDatabase db;
private static Context myContext;
public static final String DB_NAME = "ykko.db";

public static final String name = "name";
public static String DB_PATH = "/data/data/com.bit.ykko/databases/";

public DatabaseManager(Context context) {
    super(context, DB_NAME, null, 1);
    DatabaseManager.myContext = context;
}

public void createDataBase() throws IOException {
    boolean dbExit = checkDataBase();

    if (dbExit) {

    } else {
        this.getReadableDatabase().getPath();

        try {

            copyDataBase();

        } catch (IOException e) {
            // throw new RuntimeException(e.getMessage());
            e.printStackTrace();
        }
    }
}

private boolean checkDataBase() {
    SQLiteDatabase checkDb = null;

    try {
        String myPath = DB_PATH + DB_NAME;
        Log.v("Path", myPath);
        checkDb = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READONLY);
    } catch (SQLException e) {

    }

    if (checkDb != null) {
        checkDb.close();
    }
    return checkDb != null ? true : false;
}

public void copyDataBase() throws IOException {

    InputStream databaseInput = null;
    String outFileName = DB_PATH + DB_NAME;
    OutputStream databaseOutput = new FileOutputStream(outFileName);

    byte[] buffer = new byte[1024];
    int length;

    databaseInput = myContext.getAssets().open("ykko.sqlite");
    while ((length = databaseInput.read(buffer)) > 0) {
        databaseOutput.write(buffer, 0, length);
        databaseOutput.flush();
    }
    databaseInput.close();

    databaseOutput.flush();
    databaseOutput.close();
}

public void openDatabase() throws SQLiteException {

    String myPath = DB_PATH + DB_NAME;
    db = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READWRITE);

}

public ArrayList<ChineseInfo> getCatByIdx(int idx) {
    ArrayList<ChineseInfo> list = new ArrayList<ChineseInfo>();

    String query = "SELECT * FROM tbl_category WHERE idx = " + idx;

    Cursor c = this.db.rawQuery(query, null);

    if (c.moveToFirst()) {
        do {
            ChineseInfo ch = new ChineseInfo();
            ch.idx = c.getInt(0);
            ch.name = c.getString(1);
            ch.mm_name = c.getString(2);
            list.add(ch);

        } while (c.moveToNext());
    }
    if (c != null && !c.isClosed()) {
        c.close();
    }
    return list;
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}

}

3 个答案:

答案 0 :(得分:0)

为什么要在内存中创建数据库然后复制粘贴呢?如果你想在SD卡上有一个数据库,只需从头开始在SD卡中创建一个数据库:

File sdpath = Environment.getExternalStorageDirectory();
String dbPath = sdpath.getAbsolutePath() + "/my_dir/mydb.sqlitedb";
SQLiteDatabase database = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);

我希望它有所帮助。

答案 1 :(得分:0)

您可以尝试下面的内容。

   private dbhelper mydbhelper;
   public static SQLiteDatabase myDataBase; 

... 

      public Cursor getData(int Id){
          return myDataBase.query("tbl_category", new String[]{"_id", "column1", "column2", "column3",},
                  "_id" + " = " + Id, null, null, null, null);}

然后拨打下面的活动

            Cursor cursor = mydbhelper.getData(Id);

答案 2 :(得分:0)

此问题已由我自行解决。这个错误只发生在模拟器中。它在真实设备上运行良好。