执行readNumber()方法时出现‘列不存在’错误,CREATE建表查询看似正确
Hey there! Let's troubleshoot that frustrating "no column exists" error you're seeing when running readNumber()—even though your CREATE table statement looks correct at first glance. Here are the most likely culprits and how to fix them:
1. You're Stuck with an Old Database Schema
SQLiteOpenHelper caches the database once it's created. If you modified your table structure (like adding a new column) but didn't update the DATABASE_VERSION value, it won't re-run onCreate() to apply the changes.
Your code sets DATABASE_VERSION = 1—if you've ever installed the app before with an older table schema, that's the problem.
Fixes:
- Bump the version number to 2 (or any higher integer):
private static final int DATABASE_VERSION = 2; - Make sure you have an
onUpgrade()method that handles schema updates (for development, you can just drop and rebuild the table):@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // For development only—use data migration logic in production! db.execSQL("DROP TABLE IF EXISTS " + DBcontract.TABLE_NAME); onCreate(db); } - Alternatively, uninstall the app from your device/emulator to wipe the old database entirely.
2. Your CREATE Statement Is Incomplete
Looking at your code snippet, the CREATE statement gets cut off:
private static final String CREATE = "create table "+DBcontract.TABLE_NAME+"(id integer primary key,"+DBcontract.INCOMI...
It looks like you didn't finish defining the DBcontract.INCOME (or similar) column—you might be missing its data type, other columns, or the closing parenthesis.
Fixes:
- Complete the CREATE statement with full column definitions. For example:
private static final String CREATE = "create table " + DBcontract.TABLE_NAME + "(id integer primary key autoincrement, " + DBcontract.INCOME + " real, " + DBcontract.EXPENSE + " real, " + DBcontract.NOTE + " text)"; - Add a log in
onCreate()to print the full SQL statement and verify it's correct:@Override public void onCreate(SQLiteDatabase db) { Log.d("DbHelperDebug", "Running CREATE SQL: " + CREATE); db.execSQL(CREATE); }
3. Mismatched Column Names in readNumber()
Even if your CREATE statement is correct, you might be querying a column name that doesn't match what's in the table (typos, capitalization differences, or using hardcoded strings instead of your DBcontract constants).
Fixes:
- Always use your
DBcontractconstants when referencing columns in queries. For example, inreadNumber():Cursor cursor = db.query(DBcontract.TABLE_NAME, new String[]{DBcontract.ID, DBcontract.INCOME}, null, null, null, null, null); - Double-check that the column name in your query exactly matches what's defined in the CREATE statement (SQLite is case-insensitive by default, but it's better to be consistent).
4. Forgot to Add the Column to the CREATE Statement
It sounds obvious, but sometimes we overlook adding a new column to the CREATE statement after defining it in DBcontract. Double-check that every column you're trying to read in readNumber() is actually included in your CREATE table SQL.
内容的提问来源于stack exchange,提问作者chetan raj

