【问题标题】:No such table exists -Sqlite error when writing to database in android不存在这样的表-在android中写入数据库时​​出现Sqlite错误
【发布时间】:2012-03-16 16:17:13
【问题描述】:

我正在尝试在 android 中写入我的 sqlite 数据库中的表,我使用的代码如下:

DBH = new DatabaseHelper(this);
    try {
        DBH.createDataBase();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    DBH.getWritableDatabase().rawQuery("insert into profile (_id, FirstName) values (1, 'Dave')", null);
    DBH.close();

DBH 是我的数据库助手类,它可以很好地创建数据库,我什至可以从数据库中读取数据,所以我知道它在那里。我试图写入的表是配置文件,我确信它确实存在,但每次运行时我都会收到此错误:

sqlite returned: error code = 1, msg = no such table: profile

我已经尝试从这张表中读取数据并且能够得到结果,所以我很难过。有人有什么想法吗?

编辑:根据要求,这是我的数据库助手类

public class DatabaseHelper extends SQLiteOpenHelper {

private static String DB_PATH = "/data/data/com.ProjectZeus.FitnessWire/databases/";
private static String DB_NAME = "fitnesswire.db";
private SQLiteDatabase myDB;
private Context  myContext;

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

public void createDataBase() throws IOException{
    boolean check = checkDataBase();
    if(check){
        //Log.e("Check", "Database exists");
    }else{
        this.getReadableDatabase();

        try{
            copyDataBase();
        }catch(Exception e){
            throw new Error("Error copying database");
        }
    }
}


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();

}

private boolean checkDataBase() {
    SQLiteDatabase checkdb = null;
    try{
        String myPath= DB_PATH + DB_NAME;
        checkdb = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    }catch(SQLiteException e){
        Log.e("checkDataBase()", "Database doesntexist yet");
    }

    if(checkdb != null){
        checkdb.close();
    }

    return checkdb != null ? true : false ;
}

public void openDataBase() throws SQLException {
    String myPath = DB_PATH + DB_NAME;
    myDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}

@Override
public synchronized void close(){
    if(myDB != null){
        myDB.close();
    }
    super.close();
}

@Override
public void onCreate(SQLiteDatabase db) {


}

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

}

}

【问题讨论】:

  • 请发布您的 DatabaseHelper 类
  • 你可以/应该使用db.execSQL(),因为insert是没有查询的(只有select是)
  • _id 是表配置文件中的自动增量列吗?
  • 您是否尝试过从手机/模拟器中删除应用程序的数据,然后重试?
  • @Nishant 不幸的是我没有使用数据库,但从架构中我可以看出它只是一个整数。

标签: android sqlite writing


【解决方案1】:

您应该使用db.execSQL() 而不是db.rawQuery(),因为插入不是查询(只有SELECT 是)

rawQuery() 中发生了很多魔法——它可能会在数据库执行之前更改语句,并尝试从结果中构建一个Cursor。该过程中的某些部分似乎失败了。

【讨论】:

    【解决方案2】:

    在 Iphone 中也会出现这种情况...数据库未正确连接。

    我们所做的(对我们有用)是从模拟器中删除所有项目清理项目并编译它...

    我不确定它是否可以在 Android 中运行,但你可以尝试一下

    干杯

    【讨论】:

      【解决方案3】:

      当列不存在时也会引发此错误(我知道,这并不理想,但就是这样)。因此,请确保您的表中确实有名为 _idFirstName 的列。

      【讨论】:

      • 我检查了表格,列存在,不过感谢您的提示
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-20
      • 2012-10-30
      • 1970-01-01
      相关资源
      最近更新 更多