列未找到sqlite错误

时间:2017-10-31 17:47:48

标签: android sqlite android-sqlite

未找到专栏:_PDESC在编译SELECT _ID,_PNAME,-PDESC ...... FROM ADVTABLE

我在提出这个问题之前看到了很多堆叠溢出的页面,但没有得到任何帮助。 数据库被创建,但每当我尝试使用getdata()时,它都会给我错误。

如果有人也可以告诉我如何使用_id ang搜索表格,只获取2个值,姓名和电话

public class advDB {
    public static final String KEY_ROWID = "_id";
    public static final String KEY_PNAME = "_pname";
    public static final String KEY_PDESC = "_pdesc";
    public static final String KEY_PPRICE = "_pprice";
    public static final String KEY_PLOC = "_ploc";
    public static final String KEY_NAME = "_name";
    public static final String KEY_PHONE = "_phone";

    private final String DATABASE_NAME = "advDB";
    private final String DATABASE_TABLE = "advTable";
    private final int DATABASE_VERSION = 1;

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

    public advDB(Context context)
    {
        ourContext = context;
    }

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

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

            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
            onCreate(db);

        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            String sqlCode = "CREATE TABLE " + DATABASE_TABLE + "(" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_PNAME + " TEXT NOT NULL," + KEY_PDESC + "TEXT NOT NULL," + KEY_PLOC + " TEXT NOT NULL," + KEY_PPRICE + " INTEGER NOT NULL," + KEY_NAME + "TEXT NOT NULL," + KEY_PHONE + "INTEGER NOT NULL" + ")" ;

            db.execSQL(sqlCode);
        }
    }

    public long createEntry(String pname, String pdesc, String ploc, String pprice, String name, String phone)
    {
        ContentValues cv = new ContentValues();
        cv.put("KEY_PNAME", pname);
        cv.put("KEY_PDESC", pdesc);
        cv.put("KEY_PLOC", ploc);
        cv.put("KEY_PPRICE", pprice);
        cv.put("KEY_NAME", name);
        cv.put("KEY_PHONE", phone);
        return ourDatabase.insert(DATABASE_TABLE,null, cv);
    }

    public String getData()
    {
        String [] columns = new String[] {KEY_ROWID, KEY_PNAME, KEY_PDESC, KEY_PLOC, KEY_PPRICE};
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns,null,null,null,null, null);
        String result = "";
        int iRowID = c.getColumnIndex(KEY_ROWID);
        int iPName = c.getColumnIndex(KEY_PNAME);
        int iPDesc = c.getColumnIndex(KEY_PDESC);
        int iPLoc = c.getColumnIndex(KEY_PLOC);
        int iPPrice = c.getColumnIndex(KEY_PPRICE);

        for (c.moveToFirst();c.isAfterLast();c.moveToNext())
        {
            result = result + c.getString(iRowID) + ": " + c.getString(iPName) + " " + c.getString(iPDesc) + " " + c.getString(iPLoc) + " " + c.getString(iPPrice) + "/n";
        }
        c.close();
        return result;
    }
}

1 个答案:

答案 0 :(得分:1)

这是一个错字。在CREATE TABLE声明中:

+ KEY_PDESC + "TEXT NOT NULL,"

您在TEXT NOT NULL,之前错过了一个空格。纠正这个:

+ KEY_PDESC + " TEXT NOT NULL,"

KEY_NAMEKEY_PHONE相同。

另外一件事是,当您使用常量时,从ContentValues对象的键中删除引号。即:

cv.put(KEY_PNAME, pname);