package com.mckallip.BeerOnTheWall;import androID.content.ContentValues;import androID.content.Context;import androID.database.Cursor;import androID.database.sqlException;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteOpenHelper;import androID.util.Log;public class DatabaseUtil{ private static final String TAG = "DatabaseUtil";/** * Database name */private static final String DATABASE_name = "Beer_Database";/** * Database Version */private static final int DATABASE_VERSION = 1;/** * table name */private static final String DATABASE_table = "Beer_List";/** * table columns */public static final String KEY_BEER_name = "beer_name";public static final String KEY_BEER_STYLE = "beer_style";public static final String KEY_BREWERY = "beer_brewery";public static final String KEY_ABV = "beer_abv";public static final String KEY_BEER_score = "beer_score";public static final String KEY_BEER_IMAGE = "beer_image";public static final String KEY_BEER_COMMENTS = "beer_comments";public static final String KEY_ROWID = "_ID";/** * Database creation sql statement */private static final String CREATE_BEER_table = "CREATE table " + DATABASE_table + " (" + KEY_ROWID + " INTEGER PRIMARY KEY autoINCREMENT," + KEY_BEER_name + " TEXT," + KEY_BEER_STYLE + " TEXT," + KEY_BREWERY + " TEXT," + KEY_ABV + " TEXT," + KEY_BEER_score + "TEXT," + KEY_BEER_IMAGE + " TEXT," + KEY_BEER_COMMENTS + "TEXT );";/** * Context */private final Context mCtx;private DatabaseHelper mDbHelper;private sqliteDatabase mDb;/** * Inner private class. Database Helper class for creating and updating database. */private static class DatabaseHelper extends sqliteOpenHelper { DatabaseHelper(Context context) { super(context,DATABASE_name,null,DATABASE_VERSION); } /** * onCreate method is called for the 1st time when database doesn't exists. */ @OverrIDe public voID onCreate(sqliteDatabase db) { Log.i(TAG,"Creating DataBase: " + CREATE_BEER_table); db.execsql(CREATE_BEER_table); } /** * onUpgrade method is called when database version changes. */ @OverrIDe public voID onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) { Log.w(TAG,"Upgrading database from version " + oldVersion + " to " + newVersion); }}protected sqliteDatabase getDataBase(){ return mDb;}/** * Constructor - takes the context to allow the database to be * opened/created * * @param ctx the Context within which to work */public DatabaseUtil(Context ctx) { this.mCtx = ctx;}/** * This method is used for creating/opening connection * @return instance of DatabaseUtil * @throws sqlException */public DatabaseUtil open() throws sqlException { mDbHelper = new DatabaseHelper(mCtx); mDb = mDbHelper.getWritableDatabase(); return this;}/** * This method is used for closing the connection. */public voID close() { mDbHelper.close();}/** * This method is used to create/insert new record Beer record. * @param sbeer_name * @param sbeer_style * @param sbeer_score * @param sbrewery * @param sabv * @param simageLoc * @param scomments * @return long */public long createBeer(String beer_name,String beer_style,String beer_score,String brewery,String abv,String imageLoc,String comments) { ContentValues initialValues = new ContentValues(); initialValues.put(KEY_BEER_name,beer_name); initialValues.put(KEY_BEER_STYLE,beer_style); initialValues.put(KEY_BREWERY,brewery); initialValues.put(KEY_ABV,abv); initialValues.put(KEY_BEER_score,beer_score); initialValues.put(KEY_BEER_IMAGE,imageLoc); initialValues.put(KEY_BEER_COMMENTS,comments); return mDb.insert(DATABASE_table,initialValues);}/** * This method will delete Beer record. * @param rowID * @return boolean */public boolean deleteBeer(long rowID) { return mDb.delete(DATABASE_table,KEY_ROWID + "=" + rowID,null) > 0;}/** * This method will return Cursor holding all the Beer records. * @return Cursor */public Cursor fetchAllBeers() { return mDb.query(DATABASE_table,new String[] {KEY_ROWID,KEY_BEER_name,KEY_BEER_STYLE,KEY_BREWERY,KEY_ABV,KEY_BEER_score,KEY_BEER_IMAGE,KEY_BEER_COMMENTS},null);}/** * This method will return Cursor holding the specific Beer record. * @param ID * @return Cursor * @throws sqlException */public Cursor fetchBeer(long ID) throws sqlException { Cursor mCursor = mDb.query(true,DATABASE_table,KEY_BEER_COMMENTS },KEY_ROWID + "=" + ID,null); if (mCursor != null) { mCursor.movetoFirst(); } return mCursor;}/** * This method will update Beer record. * @param ID * @param name * @param standard * @return boolean */public boolean updateBeer(int ID,String beer_name,String comments) { ContentValues args = new ContentValues(); args.put(KEY_BEER_name,beer_name); args.put(KEY_BEER_STYLE,beer_style); args.put(KEY_BREWERY,brewery); args.put(KEY_BEER_score,beer_score); args.put(KEY_ABV,abv); args.put(KEY_BEER_IMAGE,imageLoc); args.put(KEY_BEER_COMMENTS,comments); return mDb.update(DATABASE_table,args,null) > 0;}}
添加数据的类是:
package com.mckallip.BeerOnTheWall; import androID.content.Context; import androID.content.Intent; import androID.database.sqlite.sqliteDatabase; import androID.os.Bundle; import androID.vIEw.VIEw; import androID.vIEw.VIEw.OnClickListener; import androID.Widget.button; import androID.Widget.EditText; import androID.Widget.Spinner; import androID.Widget.TextVIEw; import androID.Widget.Toast; public class AddBeerActivity extends BeerOnTheWallActivity { private EditText et1; private EditText et2; private EditText et3; private EditText et4; private Spinner score_spin; private EditText et5; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.add_beer); mDatabase.open(); Context e = getApplicationContext(); String test = "read only"; if (mDatabase.getDataBase().isReadonly()){ test = "Not Readonly" + mDatabase.getDataBase().getPath(); } Toast t = Toast.makeText(e,test,2); t.show(); et1 = (EditText) findVIEwByID(R.ID.add_beer_name); et2 = (EditText)findVIEwByID(R.ID.add_beer_style); et3 = (EditText) findVIEwByID(R.ID.add_brewery); et4 = (EditText) findVIEwByID(R.ID.add_abv); score_spin = (Spinner) findVIEwByID(R.ID.score_spinner); et5 = (EditText) findVIEwByID(R.ID.add_comments); ((button)findVIEwByID(R.ID.add_submit)).setonClickListener(new OnClickListener() { public voID onClick( VIEw v ){ if (mDatabase.getDataBase().isReadonly())mDatabase.open(); long beer_ID = mDatabase.createBeer( et1.toString(),et2.toString(),score_spin.toString(),et3.toString(),et4.toString(),"image",et5.toString() ); Context context = getApplicationContext(); CharSequence text = "Your beer was added at database position " + beer_ID; if ( beer_ID == -1 ){ text = "There was an error and the beer Could not be added."; } Toast toast = Toast.makeText(context,text,2); toast.show(); // reset form if ( beer_ID != -1 ){ et1.setText(null); et2.setText(null); et3.setText(null); et4.setText(null); et5.setText(null); } } }); // Handle Go to List button final button gotoList = (button) findVIEwByID(R.ID.beer_List); gotoList.setonClickListener(new VIEw.OnClickListener() { public voID onClick(VIEw v) { // Go to other activity that displays beer List Intent intent = new Intent(AddBeerActivity.this,BeerListActivity.class); startActivity(intent); } }); } }
我添加了toast作为尝试和调试它的方法.第一个声称它是只读的,我无法弄清楚为什么或如何使它可写.
我还将BeerOnTheWallActivity中的DatabaseUtil实例化为静态变量,因此……
public class BeerOnTheWallActivity extends Activity { protected DatabaseUtil mDatabase = null; //protected Cursor mCursor = null; //protected sqliteDatabase mDB = null; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mDatabase = new DatabaseUtil(this.getApplicationContext()); mDatabase.open(); }
当我按下按钮添加数据时,错误日志指出:
07-14 00:31:45.549: ERROR/Database(31512): Error inserting beer_abv=androID.Widget.EditText@47a27ea8 beer_style=androID.Widget.EditText@47a25d28 beer_score=androID.Widget.Spinner@47a2a768 beer_name=androID.Widget.EditText@47a245a8 beer_comments=androID.Widget.EditText@47a2b560 beer_image=image beer_brewery=androID.Widget.EditText@47a26de8
07-14 00:31:45.549: ERROR/Database(31512): androID.database.sqlite.sqliteException: table Beer_List has no column named beer_score:,while compiling: INSERT INTO Beer_List(beer_abv,beer_style,beer_score,beer_name,beer_comments,beer_image,beer_brewery) VALUES(?,?,?);
我想这就是全部.任何帮助将非常感激.
解决方法 请检查下面你的代码是
private static final String CREATE_BEER_table = "CREATE table " + DATABASE_table + " (" + KEY_ROWID + " INTEGER PRIMARY KEY autoINCREMENT," + KEY_BEER_COMMENTS + "TEXT );";
改成使用了这个
private static final String CREATE_BEER_table = "CREATE table " + DATABASE_table + " (" + KEY_ROWID + " INTEGER PRIMARY KEY autoINCREMENT," + KEY_BEER_score + " TEXT," + KEY_BEER_COMMENTS + " TEXT );";总结
以上是内存溢出为你收集整理的适用于Android的SQLite DB问题全部内容,希望文章能够帮你解决适用于Android的SQLite DB问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)