【问题标题】:I keep getting the SQLException error with this我不断收到 SQLException 错误
【发布时间】:2011-07-03 12:33:30
【问题描述】:

07-03 01:52:08.037: 错误/Android 运行时(627): java.lang.RuntimeException:无法 开始活动 组件信息{com.fttech.books/com.fttech.books.viewBooks}: android.database.sqlite.SQLiteException: 没有这样的专栏:作者:,而 编译:SELECT book, author, isbn, 评分来自收藏

代码:

public class booksDbAdapter {


private static final String DATABASE_NAME = " data";
private static final String DATABASE_TABLE = "collection";
private static final int DATABASE_VERSION = 1;

public static final String KEY_BOOK = "book";
public static final String KEY_AUTHOR = "author";
public static final String KEY_ISBN = "isbn";
public static final String KEY_RATING = "rating";
public static final String KEY_ROWID = "_id";

private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;


private static final String DATABASE_CREATE = 
        " create table " +  DATABASE_TABLE  + " ("
        + KEY_ROWID + " integer primary key autoincrement,  "
        + KEY_AUTHOR + " text not null, "
        + KEY_BOOK + " text not null, "
        + KEY_ISBN + " text not null, "
        + KEY_RATING + " text not null);";

private final Context mCtx;


public booksDbAdapter (Context ctx){
    this.mCtx = ctx;



        }

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


        }

        @Override
        public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);


        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
    }
}
        public booksDbAdapter open() throws SQLException{

            mDbHelper =  new DatabaseHelper(mCtx);
            mDb = mDbHelper.getWritableDatabase();
            return this;
        }
        public void close(){
            mDbHelper.close();
        }

        public long createBook(String book, String author, String isbn, String rating){
            ContentValues initialValues = new ContentValues();
            initialValues.put(KEY_BOOK, book);
            initialValues.put(KEY_AUTHOR, author);
            initialValues.put(KEY_ISBN, isbn);
            initialValues.put(KEY_RATING, rating);

            return mDb.insert(DATABASE_TABLE, null, initialValues);

        }
        public boolean deleteBook(long rowId){
            return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;

        }
        public Cursor fetchAllBooks(){          
            return mDb.query(DATABASE_TABLE, new String[]{KEY_BOOK, KEY_AUTHOR, KEY_ISBN, KEY_RATING}, null, null, null, null, null);
        }
        public Cursor fetchBook(long rowId) throws SQLException{
            Cursor mCursor = 
            mDb.query(DATABASE_TABLE, new String[]{KEY_BOOK, KEY_AUTHOR, KEY_ISBN, KEY_RATING}, KEY_ROWID + "=" +
                        rowId, null, null, null, null);

            if(mCursor != null){
                mCursor.moveToFirst();
            }
            return mCursor;

        }
        public boolean updateBook(long rowId, String book, String author, String isbn, String rating){
            ContentValues args = new ContentValues();
            args.put(KEY_BOOK, book);
            args.put(KEY_AUTHOR, author);
            args.put(KEY_ISBN, isbn);
            args.put(KEY_RATING, rating);
            return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null)> 0;


        }

}

【问题讨论】:

  • "没有这样的专栏:作者" - 非常具体
  • 我知道!我只是找不到我在哪里搞砸了!我已经找了一个小时了。你看到了吗?
  • 我告诉它创建列,正如您在代码中看到的那样......所以我不明白问题是什么。
  • 如果没有 SQLException,您将无法通过任何事情,对吗? xD
  • 我知道!我不知道它是什么......我通常不会有任何 SQL 问题

标签: android sql sqlite


【解决方案1】:

我建议您通过命令行运行.dump sqlite 数据库并检查它。然后也通过命令行运行您的查询。如果一切正常,请查找拼写错误。找出哪个方法有错误的查询也很有帮助,这样您就可以缩小问题的范围。

【讨论】:

    【解决方案2】:

    你的表真的有作者列吗?我假设您在通过db.execSQL(DATABASE_CREATE); 创建表格后添加了该列

    您的问题是您在 onUpgrade() 中没有任何代码,因此当您添加新列时,它不会添加,因为数据库创建会导致错误,例如“数据库已存在”

    您需要向 onUpgrade() 添加代码,这将至少删除所有表然后重新创建。或者更好地运行一个改变表等......

    然后您需要增加 int DATABASE_VERSION 以便 onUpgrade() 将运行。最后,您的新列“作者”将被添加到表中..

    当然,您也可以通过command line 连接到您的数据库来检查该列是否存在

    希望对你有帮助

    这是我使用的一些代码,可能有助于创建/升级。显然我的表创建常量是特定于我的......

    编辑:...您也没有尝试/捕获您的表创建。我相信如果添加这个,你会发现表创建命令没有运行,因为它得到“表已经存在等......”

    ...
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    
    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            db.execSQL(CREATE_TABLE_SITE);  
            db.execSQL(CREATE_TABLE_SITE_USER);
            db.execSQL(CREATE_TABLE_ARTICLE);
            db.execSQL(CREATE_TABLE_USER);
        } catch (Exception e) {
            Log.e("dbAdapter", e.getMessage().toString());
        }
    }
    
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS user");
        db.execSQL("DROP TABLE IF EXISTS site");
        db.execSQL("DROP TABLE IF EXISTS site_user");
        db.execSQL("DROP TABLE IF EXISTS article");
        onCreate(db); 
    }
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-26
      • 2016-02-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-10
      • 2021-11-06
      • 2023-03-22
      • 2014-03-22
      相关资源
      最近更新 更多