1

请问为什么我会收到错误“SQLiteException:没有这样的表:tabella(code1)???

当我尝试从活动访问数据库时,从 LogCat 我得到:“SQLiteException:没有这样的表:tabella (code1)”。

这是 SQLiteOpenHelper 代码:

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String CREATE_DB_SQL = "CREATE TABLE tabella (_id INTEGER     PRIMARY KEY AUTOINCREMENT, colonna1 TEXT, colonna2 TEXT, colonna3 TEXT);";
    public DatabaseHelper(Context context) {
    super(context, "basedati", null, 1);
    }

    @Override
public void onCreate(SQLiteDatabase db) {

        db.execSQL(CREATE_DB_SQL);
        ContentValues cv = new ContentValues (3);
        cv.put("colonna1", "valore colonna 1");
        cv.put("colonna2", "valore colonna 3");
        cv.put("colonna3", "valore colonna 3");
        db.insert("tabella", "colonna1", cv);
}

@Override
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
        db.execSQL("DROP TABLE IF EXISTS tabella");
        onCreate(db);   
    }
}

这是我尝试打开数据库的活动代码:

public class MainActivity extends Activity {
    private DatabaseHelper db=null;
    private Cursor constantsCursor=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        db = new DatabaseHelper(this);

        constantsCursor=db.getReadableDatabase().rawQuery("SELECT colonna1 FROM  tabella ORDER BY colonna1", null);
        }
}
4

1 回答 1

0

很可能您在运行应用程序至少一次后更改了表格。您不能简单地更改 Java String CREATE_DB_SQL,还必须更新 SQL 模式。最简单的方法是增加版本号:

public DatabaseHelper(Context context) {
    super(context, "basedati", null, 2); // Change the number to two 
}
于 2012-11-21T20:19:14.767 回答