Android的SQLite的问题 [英] Android Sqlite Problem

查看:88
本文介绍了Android的SQLite的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实施<一发现SQLite的例子href=\"http://www.android10.org/index.php/articlesdatastorage/235-creating-and-using-databases-in-android-one\"相对=nofollow> Android10 。

在code正常工作,直到我添加了两列到原来的code和崩溃。
这是DBAdapter后,我加入到这两个列,它们是链接

 公共类DBAdapter
{
    公共静态最后弦乐KEY_ROWID =_id;
    公共静态最后弦乐KEY_ISBN =ISBN;
    公共静态最后弦乐KEY_TITLE =称号;
    =,从公共静态最后弦乐KEY_FROM;
    公共静态最后弦乐KEY_LINK =联系;
    公共静态最后弦乐KEY_PUBLISHER =出版人;
    私有静态最后弦乐TAG =DBAdapter;    私有静态最后弦乐DATABASE_NAME =书;
    私有静态最后弦乐DATABASE_TABLE =标题;
    私有静态最终诠释DATABASE_VERSION = 1;    私有静态最后弦乐DATABASE_CREATE =
        创建表的标题(_id整数主键自动增量
        +ISBN文本不为空,标题文本不为空,从文本中不为空,链接文本不为空
        +出版文字不是null);;    私人最终上下文的背景下;    私人DatabaseHelper DBHelper;
    私人SQLiteDatabase分贝;    公共DBAdapter(上下文CTX)
    {
        this.context = CTX;
        DBHelper =新DatabaseHelper(背景);
    }    私有静态类DatabaseHelper扩展SQLiteOpenHelper
    {
        DatabaseHelper(上下文的背景下)
        {
            超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
        }        @覆盖
        公共无效的onCreate(SQLiteDatabase DB)
        {
            db.execSQL(DATABASE_CREATE);
        }        @覆盖
        公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,
        INT静态网页)
        {
            Log.w(TAG,从版本升级数据库+ oldVersion
                    +至
                    + NEWVERSION +,这将摧毁所有旧数据);
            db.execSQL(DROP TABLE IF EXISTS标题);
            的onCreate(DB);
        }
    }    // ---打开数据库---
    公共DBAdapter的open()抛出的SQLException
    {
        DB = DBHelper.getWritableDatabase();
        返回此;
    }    // ---关闭数据库---
    公共无效的close()
    {
        DBHelper.close();
    }    // ---插入标题到数据库---
    众长insertTitle(ISBN字符串,字符串标题,从,字符串连接字符串,字符串出版商)
    {
        ContentValues​​ initialValues​​ =新ContentValues​​();
        initialValues​​.put(KEY_ISBN,ISBN);
        initialValues​​.put(KEY_TITLE,职称);
        initialValues​​.put(KEY_FROM,来自);
        initialValues​​.put(KEY_LINK,链接);
        initialValues​​.put(KEY_PUBLISHER,发布者);
        返回db.insert(DATABASE_TABLE,空,initialValues​​);
    }    // ---删除特定的书名---
    公共布尔deleteTitle(长ROWID)
    {
        返回db.delete(DATABASE_TABLE,KEY_ROWID +
                =+ ROWID,NULL)&GT; 0;
    }    // ---检索所有的标题---
    公共光标getAllTitles()
    {
        返回db.query(DATABASE_TABLE,新的String [] {
                KEY_ROWID,
                KEY_ISBN,
                KEY_TITLE,
                KEY_FROM,
                KEY_LINK,
                KEY_PUBLISHER},
                空值,
                空值,
                空值,
                空值,
                空值);
    }    // ---检索特定的书名---
    公共光标的getTitle(长ROWID)抛出的SQLException
    {
        光标mCursor =
                db.query(真,DATABASE_TABLE,新的String [] {
                        KEY_ROWID,
                        KEY_ISBN,
                        KEY_TITLE,
                        KEY_FROM,
                        KEY_LINK,
                        KEY_PUBLISHER
                        },
                        KEY_ROWID +=+ ROWID,
                        空值,
                        空值,
                        空值,
                        空值,
                        空值);
        如果(mCursor!= NULL){
            mCursor.moveToFirst();
        }
        返回mCursor;
    }    // ---更新一个标题---
    公共布尔updateTitle(长ROWID,字符串书号,
    字符串的标题,从,字符串连接字符串,字符串出版商)
    {
        ContentValues​​ ARGS =新ContentValues​​();
        args.put(KEY_ISBN,ISBN);
        args.put(KEY_TITLE,职称);
        args.put(KEY_FROM,来自);
        args.put(KEY_LINK,链接);
        args.put(KEY_PUBLISHER,发布者);
        返回db.update(DATABASE_TABLE,ARGS,
                         KEY_ROWID +=+ ROWID,NULL)&GT; 0;
    }
}

这是DatabaseActivity后,我添加了两列

 公共类DatabaseActivity延伸活动{
    / **当第一次创建活动调用。 * /    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        DBAdapter DB =新DBAdapter(本);        // ---加2个标题---
        db.open();
        长ID;
        ID = db.insertTitle(
                0470285818,
                Hanudi是最好的:)
                Wrox的
                www.wrox.com
                WroxWrox);
        ID = db.insertTitle(
                047017661X
                专业的Windows Vista小工具程序设计,
                Wrox的
                www.wrox2.com
                WroxWrox2);
        db.close();        // ---获得所有冠军---
        db.open();
        光标C = db.getAllTitles();
        如果(c.moveToFirst())
        {
            做{
                DISPLAYTITLE(C);
            }而(c.moveToNext());
        }
        db.close();
    }
    公共无效DISPLAYTITLE(光标C)
    {
        Toast.makeText(这一点,
                ID:+ c.getString(0)+\\ N+
                ISBN:+ c.getString(1)+\\ n+
                标题:+ c.getString(2)+\\ n+
                FROM:+ c.getString(3)+\\ n+
                链接:+ c.getString(4)+\\ n+
                PUBLISHER:+ c.getString(5)
                Toast.LENGTH_LONG).show();
    }
}


解决方案

2的问题可能导致您解忧:


  1. 请确保你在你的 DATABASE_CREATE 有足够的逗号!目前,您不必出版商一个逗号。这将抛出一个错误,如果你在你的SQLite管理工具运行它,和有可能是你的崩溃原因。您的字符串结束是:

    ....,链接文本不nullpublisher文本不为空


  2. 您正在尝试使用SQL保留关键字 FROM 作为列名。这可能会为你未来的第二个问题。最好的做法是,你选择你列一个良好的命名和有意义的名称。也许称之为 fromCustomer fromYear 或更具描述性的。


I am trying to implement a SQLite example found on Android10.

The code works fine until I added two more columns to the original code, and it crashes. This is the DBAdapter after i added to it two more columns and they are from and link

public class DBAdapter 
{
    public static final String KEY_ROWID = "_id";
    public static final String KEY_ISBN = "isbn";
    public static final String KEY_TITLE = "title";
    public static final String KEY_FROM = "from";
    public static final String KEY_LINK = "link";
    public static final String KEY_PUBLISHER = "publisher";    
    private static final String TAG = "DBAdapter";

    private static final String DATABASE_NAME = "books";
    private static final String DATABASE_TABLE = "titles";
    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE =
        "create table titles (_id integer primary key autoincrement, "
        + "isbn text not null, title text not null, from text not null, link text not null"
        + "publisher text not null);";

    private final Context context; 

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    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) 
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion 
                    + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS titles");
            onCreate(db);
        }
    }    

    //---opens the database---
    public DBAdapter open() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    //---closes the database---    
    public void close() 
    {
        DBHelper.close();
    }

    //---insert a title into the database---
    public long insertTitle(String isbn, String title,String from, String link, String publisher) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_ISBN, isbn);
        initialValues.put(KEY_TITLE, title);
        initialValues.put(KEY_FROM, from);
        initialValues.put(KEY_LINK, link);
        initialValues.put(KEY_PUBLISHER, publisher);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    //---deletes a particular title---
    public boolean deleteTitle(long rowId) 
    {
        return db.delete(DATABASE_TABLE, KEY_ROWID + 
                "=" + rowId, null) > 0;
    }

    //---retrieves all the titles---
    public Cursor getAllTitles() 
    {
        return db.query(DATABASE_TABLE, new String[] {
                KEY_ROWID, 
                KEY_ISBN,
                KEY_TITLE,
                KEY_FROM,
                KEY_LINK,
                KEY_PUBLISHER}, 
                null, 
                null, 
                null, 
                null, 
                null);
    }

    //---retrieves a particular title---
    public Cursor getTitle(long rowId) throws SQLException 
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE, new String[] {
                        KEY_ROWID,
                        KEY_ISBN, 
                        KEY_TITLE,
                        KEY_FROM,
                        KEY_LINK,
                        KEY_PUBLISHER
                        }, 
                        KEY_ROWID + "=" + rowId, 
                        null,
                        null, 
                        null, 
                        null, 
                        null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    //---updates a title---
    public boolean updateTitle(long rowId, String isbn, 
    String title, String from, String link, String publisher) 
    {
        ContentValues args = new ContentValues();
        args.put(KEY_ISBN, isbn);
        args.put(KEY_TITLE, title);
        args.put(KEY_FROM, from);
        args.put(KEY_LINK, link);
        args.put(KEY_PUBLISHER, publisher);
        return db.update(DATABASE_TABLE, args, 
                         KEY_ROWID + "=" + rowId, null) > 0;
    }
}

This is DatabaseActivity after I added the two columns

public class DatabaseActivity extends Activity {
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        DBAdapter db = new DBAdapter(this);

        //---add 2 titles---
        db.open();        
        long id;
        id = db.insertTitle(
                "0470285818",
                "Hanudi is the best :)",
                "Wrox",
                "www.wrox.com",
                "WroxWrox");        
        id = db.insertTitle(
                "047017661X",
                "Professional Windows Vista Gadgets Programming",
                "Wrox",
                "www.wrox2.com",
                "WroxWrox2");
        db.close();

        //---get all titles---
        db.open();
        Cursor c = db.getAllTitles();
        if (c.moveToFirst())
        {
            do {          
                DisplayTitle(c);
            } while (c.moveToNext());
        }
        db.close();
    }    
    public void DisplayTitle(Cursor c)
    {
        Toast.makeText(this, 
                "id: " + c.getString(0) + "\n" +
                "ISBN: " + c.getString(1) + "\n" +
                "TITLE: " + c.getString(2) + "\n" +
                "FROM: " + c.getString(3) + "\n" +
                "LINK: " + c.getString(4) + "\n" +
                "PUBLISHER:  " + c.getString(5),
                Toast.LENGTH_LONG).show();        
    }    
}

解决方案

2 problems likely causing you grief:

  1. Make sure you have adequate commas in your DATABASE_CREATE! Currently you don't have a comma between null and publisher. This will throw an error if you ran it in your SQLite management tool, and likely the cause of your crash. Your string ends up being:

    ...., link text not nullpublisher text not null

  2. You're trying to use a SQL reserved keyword FROM as a column name. This likely will be a second problem for you going forward. Best practice is that you choose a well-named and meaningful name for your column. Perhaps call it fromCustomer or fromYear or something more descriptive.

这篇关于Android的SQLite的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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