我不断收到与此有关的SQLException错误 [英] I keep getting the SQLException error with this

查看:91
本文介绍了我不断收到与此有关的SQLException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

一月7日至3日:52:08.037:
  ERROR / AndroidRuntime(627):
  了java.lang.RuntimeException:无法
  活动启动
  ComponentInfo {com.fttech.books / com.fttech.books.viewBook​​s}:
  android.database.sqlite.SQLiteException:
  没有这样的列:作者:,而
  编译:选择预订,作者,ISBN
  评级从集合


code:

 公共类booksDbAdapter {
私有静态最后弦乐DATABASE_NAME =数据;
私有静态最后弦乐DATABASE_TABLE =收藏;
私有静态最终诠释DATABASE_VERSION = 1;公共静态最后弦乐KEY_BOOK =书;
公共静态最后弦乐KEY_AUTHOR =作者;
公共静态最后弦乐KEY_ISBN =ISBN;
公共静态最后弦乐KEY_RATING =等级;
公共静态最后弦乐KEY_ROWID =_id;私人DatabaseHelper mDbHelper;
私人SQLiteDatabase MDB;
私有静态最后弦乐DATABASE_CREATE =
        CREATE TABLE+ DATABASE_TABLE +(
        + KEY_ROWID +整数主键自动增量
        + KEY_AUTHOR +文本不为空,
        + KEY_BOOK +文本不为空,
        + KEY_ISBN +文本不为空,
        + KEY_RATING +文字NOT NULL);;私人最终上下文mCtx;
公共booksDbAdapter(上下文CTX){
    this.mCtx = CTX;        }        私有静态类DatabaseHelper扩展SQLiteOpenHelper {
        DatabaseHelper(上下文的背景下){
            超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
        }        @覆盖
        公共无效的onCreate(SQLiteDatabase DB){
        db.execSQL(DATABASE_CREATE);
        }        @覆盖
        公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
            // TODO自动生成方法存根
    }
}
        公共booksDbAdapter的open()抛出的SQLException {            mDbHelper =新DatabaseHelper(mCtx);
            MDB = mDbHelper.getWritableDatabase();
            返回此;
        }
        公共无效的close(){
            mDbHelper.close();
        }        众长createBook(书字符串,字符串作者,ISBN字符串,字符串等级){
            ContentValues​​ initialValues​​ =新ContentValues​​();
            initialValues​​.put(KEY_BOOK,书籍);
            initialValues​​.put(KEY_AUTHOR,作家);
            initialValues​​.put(KEY_ISBN,ISBN);
            initialValues​​.put(KEY_RATING,等级);            返回mDb.insert(DATABASE_TABLE,空,initialValues​​);        }
        公共布尔deleteBook(长ROWID){
            返回mDb.delete(DATABASE_TABLE,KEY_ROWID +=+ ROWID,NULL)> 0;        }
        公共光标fetchAllBooks(){
            返回mDb.query(DATABASE_TABLE,新的String [] {KEY_BOOK,KEY_AUTHOR,KEY_ISBN,KEY_RATING},NULL,NULL,NULL,NULL,NULL);
        }
        公共光标fetchBook(长ROWID)抛出的SQLException {
            光标mCursor =
            mDb.query(DATABASE_TABLE,新的String [] {KEY_BOOK,KEY_AUTHOR,KEY_ISBN,KEY_RATING},KEY_ROWID +=+
                        ROWID,NULL,NULL,NULL,NULL);            如果(mCursor!= NULL){
                mCursor.moveToFirst();
            }
            返回mCursor;        }
        公共布尔updateBook(长ROWID,弦乐的书,作者的字符串,字符串ISBN,串等级){
            ContentValues​​ ARGS =新ContentValues​​();
            args.put(KEY_BOOK,书籍);
            args.put(KEY_AUTHOR,作家);
            args.put(KEY_ISBN,ISBN);
            args.put(KEY_RATING,等级);
            返回mDb.update(DATABASE_TABLE,ARGS,KEY_ROWID +=+ ROWID,NULL)> 0;
        }}


解决方案

请问你的表真的有列作者?我想你已经通过 db.execSQL(DATABASE_CREATE)创建表之后添加列;

你的问题是你没有在 onUpgrade任何code()所以,当你添加一个新列的不添加,因为数据库中创建将导致错误类似数据库已经存在

您需要code添加到onUpgrade(),这将ATLEAST删除所有数据表,然后重新创建。或更好的运行ALTER TABLE等等...

然后你需要增加 INT DATABASE_VERSION 所以onUpgrade()将运行。最后你的新栏目作者将被添加到表..

当然,你也可以检查如果通过的命令行

希望帮助

下面是一些code我用这可能有助于创造方面/升级。显然,我的表中创建常量是特定于我......

编辑:...还你有你的桌子周围没有try / catch语句创建。我相信,如果添加此,你会发现表创建命令没有运行作为其获得表已经存在,等等..

  ...
DatabaseHelper(上下文的背景下){
    超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
}@覆盖
公共无效的onCreate(SQLiteDatabase DB){
    尝试{
        db.execSQL(CREATE_TABLE_SITE);
        db.execSQL(CREATE_TABLE_SITE_USER);
        db.execSQL(CREATE_TABLE_ARTICLE);
        db.execSQL(CREATE_TABLE_USER);
    }赶上(例外五){
        Log.e(dbAdapter,e.getMessage()的toString());
    }
}@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
    Log.w(TAG,从版本升级数据库+ oldVersion +到
            + NEWVERSION +,这将摧毁所有旧数据);
    db.execSQL(DROP TABLE IF EXISTS用户);
    db.execSQL(DROP TABLE IF EXISTS网站);
    db.execSQL(DROP TABLE IF EXISTS site_user);
    db.execSQL(DROP TABLE IF EXISTS条);
    的onCreate(DB);
}
...

07-03 01:52:08.037: ERROR/AndroidRuntime(627): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fttech.books/com.fttech.books.viewBooks}: android.database.sqlite.SQLiteException: no such column: author: , while compiling: SELECT book, author, isbn, rating FROM collection

CODE:

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;


        }

}

解决方案

Does your table really have the column author? i assume you've added that column after creating the table via db.execSQL(DATABASE_CREATE);

your problem is you don't have any code within onUpgrade() so when you add a new column its not added because database create will result in an error something like "database already exists"

You need to add code to onUpgrade() which will atleast drop all tables then recreate. or better run an alter table etc...

you then need to increment int DATABASE_VERSION so onUpgrade() will run. FINALLY your new column "author" will be added to the table..

of course you can also check if the column exists by connecting to your database via the command line

hope that helps

Here is some code i use which might help regarding creation/upgrade. Obviously my table create constants are specific to me...

Edit: ...also you have no try/catch around your table create. I am sure if add this, you'll discover the table create command is not running as its getting "table already exists etc..."

...
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); 
}
...

这篇关于我不断收到与此有关的SQLException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆