无法在sqlite中找到列

时间:2018-06-15 10:44:45

标签: java android sqlite

我是sqlite的新手,我正在尝试使用sqlite开发应用程序,但我遇到了错误

  

没有这样的专栏:提醒。

我搜索了很多网站但却找不到问题。我不知道我需要发布的所有细节。但是我在logcat中遇到这样的错误。

  

引起:android.database.sqlite.SQLiteException:没有这样的列:提醒(代码1):,同时编译:SELECT id,party_name,bank_name,reminder,expiry_date,reminder_date,cheque_number,amount FROM financeTBL ORDER BY idDESC < / p>

DatabaseHandler类

public class DatabaseHandler extends SQLiteOpenHelper {
        private Context ctx;
        public DatabaseHandler(Context context) {
            super(context, Constants.DB_NAME, null, Constants.DB_VERSION);
            this.ctx = context;
        }
        @Override
        public void onCreate(SQLiteDatabase db) {
            String CREATE_ALERT_TABLE = "CREATE TABLE " + Constants.TABLE_NAME + "("
                    + Constants.KEY_ID + " INTEGER PRIMARY KEY,"
                    + Constants.KEY_PARTY_NAME + "TEXT,"
                    + Constants.KEY_CHEQUE_NUMBER + "TEXT"
                    + Constants.KEY_AMOUNT + "TEXT"
                    + Constants.KEY_EXPIRY_DATE + "LONG"
                    + Constants.KEY_REMINDER_DATE + "LONG"
                    + Constants.KEY_REMINDER + "Text,"
                    + Constants.KEY_BANK_NAME + "TEXT);";    
            db.execSQL(CREATE_ALERT_TABLE);
        }

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

            db.execSQL("DROP TABLE IF EXISTS " + Constants.TABLE_NAME);

            onCreate(db);

        }
            public void addfinance(Finance finance) {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put(Constants.KEY_PARTY_NAME, finance.getPartyName());
            values.put(Constants.KEY_BANK_NAME, finance.getBankName());
            values.put(Constants.KEY_REMINDER, finance.getReminder());
            values.put(Constants.KEY_REMINDER_DATE, finance.getReminderDate().getTime());
            values.put(Constants.KEY_AMOUNT, finance.getAmount());
            values.put(Constants.KEY_CHEQUE_NUMBER,finance.getChequeNumber());
            values.put(Constants.KEY_EXPIRY_DATE, finance.getExpiryDate().getTime());
                db.insert(Constants.TABLE_NAME, null, values);
            Log.d("Saved!!", "Saved to DB");

       }
        public Finance getGrocery(int id) {
            SQLiteDatabase db = this.getWritableDatabase();

            Cursor cursor = db.query(Constants.TABLE_NAME, new String[] {Constants.KEY_ID,
                            Constants.KEY_AMOUNT,
                            Constants.KEY_REMINDER_DATE,
                            Constants.KEY_PARTY_NAME,
                            Constants.KEY_BANK_NAME,
                            Constants.KEY_REMINDER,
                            Constants.KEY_CHEQUE_NUMBER,
                            Constants.KEY_EXPIRY_DATE},
                    Constants.KEY_ID + "=?",
                    new String[] {String.valueOf(id)}, null, null, null, null);

            if (cursor != null)
                cursor.moveToFirst();


            Finance finance = new Finance();
            finance.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(Constants.KEY_ID))));
            finance.setPartyName(cursor.getString(cursor.getColumnIndex(Constants.KEY_PARTY_NAME)));
            finance.setBankName(cursor.getString(cursor.getColumnIndex(Constants.KEY_BANK_NAME)));
            finance.setReminder(cursor.getString(cursor.getColumnIndex(Constants.KEY_REMINDER)));
            finance.setChequeNumber(cursor.getString(cursor.getColumnIndex(Constants.KEY_CHEQUE_NUMBER)));
            finance.setAmount(cursor.getString(cursor.getColumnIndex(Constants.KEY_AMOUNT)));
            Date date= new Date(cursor.getLong(cursor.getColumnIndex(Constants.KEY_EXPIRY_DATE)));
            finance.setExpiryDate(date);
            date=new Date(cursor.getLong(cursor.getColumnIndex(Constants.KEY_REMINDER_DATE)));
            finance.setReminderDate(date);
            return finance;
        }
        //Get all Groceries
        public List<Finance> getAllFinanceDetails() {
            SQLiteDatabase db = this.getReadableDatabase();

            List<Finance> financeList = new ArrayList<>();

            Cursor cursor = db.query(Constants.TABLE_NAME, new String[] {
                    Constants.KEY_ID,
                    Constants.KEY_PARTY_NAME,
                    Constants.KEY_BANK_NAME,
                    Constants.KEY_REMINDER,
                    Constants.KEY_EXPIRY_DATE,
                    Constants.KEY_REMINDER_DATE,
                    Constants.KEY_CHEQUE_NUMBER
                    ,Constants.KEY_AMOUNT
                   }, null, null, null, null, Constants.KEY_ID + "DESC");

            if (cursor.moveToFirst()) {
                do {
                    Finance finance = new Finance();
                    finance.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(Constants.KEY_ID))));
                    finance.setPartyName(cursor.getString(cursor.getColumnIndex(Constants.KEY_PARTY_NAME)));
                    finance.setBankName(cursor.getString(cursor.getColumnIndex(Constants.KEY_BANK_NAME)));
                    finance.setReminder(cursor.getString(cursor.getColumnIndex(Constants.KEY_REMINDER)));
                    Date date= new Date(cursor.getLong(cursor.getColumnIndex(Constants.KEY_EXPIRY_DATE)));
                    finance.setExpiryDate(date);
                    date=new Date(cursor.getLong(cursor.getColumnIndex(Constants.KEY_REMINDER_DATE)));
                    finance.setReminderDate(date);
                    finance.setChequeNumber(cursor.getString(cursor.getColumnIndex(Constants.KEY_CHEQUE_NUMBER)));
                    finance.setAmount(cursor.getString(cursor.getColumnIndex(Constants.KEY_AMOUNT)));
                    financeList.add(finance);

                }while (cursor.moveToNext());
            }

            return financeList;
        }

常量类

public class Constants {


    public static final int DB_VERSION = 1;
    public static final String DB_NAME = "financeDB";
    public static final String TABLE_NAME = "financeTBL";

    public static final String KEY_ID = "id";
    public static final String KEY_PARTY_NAME = "party_name";
    public static final String KEY_BANK_NAME = "bank_name";
    public static final String KEY_REMINDER = "reminder";
    public static  final String KEY_CHEQUE_NUMBER ="cheque_number";
    public static  final String KEY_AMOUNT = "amount";
    public static  final String KEY_EXPIRY_DATE = "expiry_date";
    public static  final String KEY_REMINDER_DATE = "reminder_date";
}

2 个答案:

答案 0 :(得分:5)

您在CREATE TABLE声明中遗漏了一些逗号,这意味着它们将被视为一列而不是分开。

String CREATE_ALERT_TABLE = "CREATE TABLE " + Constants.TABLE_NAME + "("
                + Constants.KEY_ID + " INTEGER PRIMARY KEY,"
                + Constants.KEY_PARTY_NAME + "TEXT,"
                + Constants.KEY_CHEQUE_NUMBER + "TEXT"  // <-- HERE
                + Constants.KEY_AMOUNT + "TEXT"         // <-- HERE
                + Constants.KEY_EXPIRY_DATE + "LONG"    // <-- HERE
                + Constants.KEY_REMINDER_DATE + "LONG"  // <-- HERE
                + Constants.KEY_REMINDER + "Text,"
                + Constants.KEY_BANK_NAME + "TEXT);";  

您还会在列类型之前缺少空格,因此例如reminder列将在构造的语句中显示为reminderText。我建议您重新格式化CREATE TABLE语句:

String CREATE_ALERT_TABLE = "CREATE TABLE " + Constants.TABLE_NAME + " ("
                + Constants.KEY_ID + " INTEGER PRIMARY KEY,"
                + Constants.KEY_PARTY_NAME + " TEXT,"
                + Constants.KEY_CHEQUE_NUMBER + " TEXT,"
                + Constants.KEY_AMOUNT + " TEXT,"
                + Constants.KEY_EXPIRY_DATE + " LONG,"
                + Constants.KEY_REMINDER_DATE + " LONG,"
                + Constants.KEY_REMINDER + " TEXT,"
                + Constants.KEY_BANK_NAME + " TEXT);";  

您需要添加的另一个空间位于此查询的ORDER BY部分:

Constants.KEY_ID + "DESC"

需要:

Constants.KEY_ID + " DESC"

答案 1 :(得分:3)

在你的onCreate你忘了逗号。试试这个

public void onCreate(SQLiteDatabase db) {
            String CREATE_ALERT_TABLE = "CREATE TABLE " + Constants.TABLE_NAME + "("
                    + Constants.KEY_ID + " INTEGER PRIMARY KEY,"
                    + Constants.KEY_PARTY_NAME + " TEXT,"
                    + Constants.KEY_CHEQUE_NUMBER + " TEXT,"
                    + Constants.KEY_AMOUNT + " TEXT,"
                    + Constants.KEY_EXPIRY_DATE + " LONG,"
                    + Constants.KEY_REMINDER_DATE + " LONG,"
                    + Constants.KEY_REMINDER + " Text,"
                    + Constants.KEY_BANK_NAME + " TEXT);";    
            db.execSQL(CREATE_ALERT_TABLE);
        }