SQL数据库列错误,还是不存在?

时间:2012-03-14 05:11:36

标签: java android sql database sqlite

我正在使用我的sqldatabase,但是我无法继续前进,因为我的程序给出了一个错误,即没有这样的列,即使在“KEY_NAME”下我的代码顶部明确定义了列如果有人可以帮我或者需要我发布更多代码请告诉我谢谢! 我只发布了我的数据库。

** * **** 最近的logcat信息 03-14 01:24:54.285:E / Database(26073):android.database.sqlite.SQLiteException:table PeopleTable没有名为persons_name:的列,在编译时:INSERT INTO PeopleTable(persons_hotness,persons_name)VALUES(?,?) ;


public class HotOrNot {
    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "persons_name";
    public static final String KEY_HOTNESS = "persons_hotness";

    private static final String DATABASE_NAME = "HotOrNotDB";
    private static final String DATABASE_TABLE = "PeopleTable";
    private static final int DATABASE_VERSION = 1;

    private DbHelper ourHelper;
    private final Context ourContext;
    private SQLiteDatabase ourDatabase;

    private static class DbHelper extends SQLiteOpenHelper {
        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
                    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
                    + " TEXT NOT NULL, " + KEY_HOTNESS + " TEXT NOT NULL);");

            // String sql = String.format("create table %s "
            // + "(%s int primary key, %s int, %s text, %s text)",
            // DATABASE_TABLE, KEY_ROWID, KEY_NAME, KEY_HOTNESS);
            // db.execSQL(sql);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
            onCreate(db);
        }
    }

    public HotOrNot(Context c) {
        ourContext = c;
    }

    public HotOrNot open() throws SQLException {
        ourHelper = new DbHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        ourHelper.close();
    }

    public long createEntry(String name, String hotness) {
        // TODO Auto-generated method stub
        ContentValues cv = new ContentValues();
        cv.put(KEY_NAME, name);
        cv.put(KEY_HOTNESS, hotness);
        return ourDatabase.insert(DATABASE_TABLE, null, cv);
    }

    public String getData() {
        // TODO Auto-generated method stub
        String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_HOTNESS };
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
                null, null);
        String result = "";
        int iRow = c.getColumnIndex(KEY_ROWID);
        int iName = c.getColumnIndex(KEY_NAME);
        int iHotness = c.getColumnIndex(KEY_HOTNESS);

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
            result = result + c.getString(iRow) + " " + c.getString(iName)
                    + " " + c.getString(iHotness) + "\n";
        }
        return result;
    }
}

2 个答案:

答案 0 :(得分:3)

db.execSQL(“CREATE TABLE”+ DATABASE_TABLE +“(”+ KEY_ROWID“                     +“INTEGER PRIMARY KEY AUTOINCREMENT,KEY_NAME TEXT NOT NULL,”+ KEY_HOTNESS +“TEXT NOT NULL);”);

尝试以上更改。我认为问题可能是由于在KEY_NAME之前添加了空格。希望这有助于你..

答案 1 :(得分:2)

用这个

替换你的db.execSQL查询
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
            + " TEXT NOT NULL, " + KEY_HOTNESS + " TEXT NOT NULL);");