Android数据库没有这样的列错误?

时间:2014-04-11 12:52:31

标签: android database

我正在尝试创建一个sql数据库,使用eclipse ADT创建和android ap和我。但是我一直得到一个" E / SQLiteLog(786):( 1)没有这样的列:location"错误显示我将它包含在数据库创建类中的事实,任何想法?

数据库创建类的代码:

    package com.example.networkassignment;


    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 DB 

        { 
                public static final String KEY_LOCATION = "location"; 
                public static final String KEY_DATE = "date"; 
                public static final String KEY_SUPPORT = "support"; 
                public static final String KEY_ROWID = "_id"; 

                private static final String TAG = "DB"; 
                private DatabaseHelper mDbHelper; 
                private SQLiteDatabase mDb; 

                /** 
                 * Database creation sql statement 
                 */ 

private static final String DATABASE_CREATE = 
        "create table shows (_id integer primary key autoincrement, " 
                + "location text not null, date text not null, support text not null);"; 


private static final String DATABASE_NAME = "data"; 
private static final String DATABASE_TABLE = "shows"; 
private static final int DATABASE_VERSION = 2; 

private final Context mCtx; 

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 shows"); 
        onCreate(db); 
    } 

} 
public DB(Context ctx) 
{ 
 this.mCtx = ctx; 
} 


public DB open() throws SQLException 
{ 
    mDbHelper = new DatabaseHelper(mCtx); 
    mDb = mDbHelper.getWritableDatabase(); 
    return this; 
} 

public void close() 
{ 
    mDbHelper.close(); 
} 

public long createShow(String location, String date, String support) 
{ 
    ContentValues initialValues = new ContentValues(); 
    initialValues.put(KEY_LOCATION, location); 
    initialValues.put(KEY_DATE, date); 
    initialValues.put(KEY_SUPPORT, support); 
    return mDb.insert(DATABASE_TABLE, null, initialValues); 
} 

public boolean deleteShow(long rowId)
{ 

 return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; 
 } 


public Cursor fetchAllShows()
{   
        return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_LOCATION, 
          KEY_DATE, KEY_SUPPORT}, null, null, null, null, null); 
} 

public Cursor fetchNote(long rowId) throws SQLException { 

    Cursor mCursor = 

            mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, 
                    KEY_LOCATION, KEY_DATE,KEY_SUPPORT }, KEY_ROWID + "=" + rowId, null, 
                    null, null, null, null); 
    if (mCursor != null)
    { 
        mCursor.moveToFirst(); 
    } 
 return mCursor; 

 } 


public boolean updateNote(long rowId, String Location, String Date, String Support) 
{ 
    ContentValues args = new ContentValues(); 
    args.put(KEY_LOCATION, Location ); 
    args.put(KEY_DATE, Date); 
    args.put(KEY_SUPPORT, Support); 

    return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0; 
} 

        }

以及使用它的类

package com.example.networkassignment;


import android.app.ListActivity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.view.ContextMenu; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ContextMenu.ContextMenuInfo; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 
import android.widget.AdapterView.AdapterContextMenuInfo; 
import android.app.Activity;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class database extends ListActivity 
{
    private static final int ACTIVITY_CREATE=0; 
    private static final int ACTIVITY_EDIT=1; 
    private static final int INSERT_ID = Menu.FIRST; 
    private static final int DELETE_ID = Menu.FIRST + 1; 

    private DB mDbHelper; 
    private Cursor mNotesCursor;


    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.database);
        mDbHelper = new DB(this); 
        mDbHelper.open(); 
        fillData(); 


        registerForContextMenu(getListView());
    }

        private void fillData()
        { 
            mNotesCursor = mDbHelper.fetchAllShows(); 
            startManagingCursor(mNotesCursor); 

            String[] from = new String[]{DB.KEY_LOCATION, DB.KEY_DATE, DB.KEY_SUPPORT}; 

            // and an array of the fields we want to bind those fields to (in this case just text1) 
            int[] to = new int[]{R.id.txtLocation, R.id.txtDate, R.id.txtSupport};

            // Now create a simple cursor adapter and set it to display 
            SimpleCursorAdapter shows = 
                    new SimpleCursorAdapter(this, R.layout.show_row, mNotesCursor, from, to); 
            setListAdapter(shows); 
        } 

        @Override 
        public boolean onCreateOptionsMenu(Menu menu)
        { 
            super.onCreateOptionsMenu(menu); 
            menu.add(0, INSERT_ID, 0, R.string.menu_insert); 
            return true; 
        } 

        @Override 
        public boolean onMenuItemSelected(int featureId, MenuItem item) 
        { 
            switch(item.getItemId()) { 
            case INSERT_ID: 
                createNote(); 
                return true; 
            } 

            return super.onMenuItemSelected(featureId, item); 
        } 

        @Override 
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
        { 
            super.onCreateContextMenu(menu, v, menuInfo); 
            menu.add(0, DELETE_ID, 0, R.string.menu_delete); 
        } 

        @Override 
        public boolean onContextItemSelected(MenuItem item) 
        { 
            switch(item.getItemId())
            { 
            case DELETE_ID: 
                AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 
                mDbHelper.deleteShow(info.id); 
                fillData(); 
                return true; 
            } 
            return super.onContextItemSelected(item); 
        } 

        private void createNote() 
        { 
            Intent i = new Intent(this,ShowEdit.class); 
            startActivityForResult(i, ACTIVITY_CREATE); 
        } 

        @Override 
        protected void onListItemClick(ListView l, View v, int position, long id)
        { 
            super.onListItemClick(l, v, position, id); 
                Cursor c = mNotesCursor; 
                c.moveToPosition(position); 
                Intent i = new Intent(this, ShowEdit.class); 
                i.putExtra(DB.KEY_ROWID, id);
                i.putExtra(DB.KEY_LOCATION, c.getString( c.getColumnIndexOrThrow(DB.KEY_LOCATION))); 
                i.putExtra(DB.KEY_DATE, c.getString( c.getColumnIndexOrThrow(DB.KEY_DATE))); 
                i.putExtra(DB.KEY_SUPPORT, c.getString( c.getColumnIndexOrThrow(DB.KEY_SUPPORT))); 
                startActivityForResult(i, ACTIVITY_EDIT); 
        }

        protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 
                super.onActivityResult(requestCode, resultCode, intent); 
                Bundle extras = intent.getExtras(); 
                switch(requestCode) { 
                    case ACTIVITY_CREATE: 
                        String location = extras.getString(DB.KEY_LOCATION); 
                        String date = extras.getString(DB.KEY_DATE); 
                        String support = extras.getString(DB.KEY_SUPPORT); 
                        mDbHelper.createShow(location, date, support); 
                        fillData(); 
                        break; 
                    case ACTIVITY_EDIT: 
                        Long rowId = extras.getLong(DB.KEY_ROWID); 
                        if (rowId != null) { 
                            String editLocation = extras.getString(DB.KEY_LOCATION); 
                            String editDate = extras.getString(DB.KEY_DATE);
                            String editSupport = extras.getString(DB.KEY_SUPPORT); 
                            mDbHelper.updateNote(rowId, editLocation, editDate, editSupport); 
                        } 
                        fillData(); 
                        break; 
                } 
            } 

}

编辑:意外地提出旧版本的DB类....

2 个答案:

答案 0 :(得分:0)

您在创建表格时静态传递表名称

create table events 

那是你的错。访问表时,您使用的是变量DATABASE_TABLE。简而言之,您正在创建名为'events'的表,但是当您插入数据时,您正试图将数据插入到“show”表中,而这些表实际上并未创建。

要解决此问题,只需按以下步骤更新您的create table查询,

"create table " + DATABASE_TABLE + " (_id integer primary key autoincrement, " 
            + "location text not null, date text not null, support text not null);";

执行此操作后,删除旧应用程序并运行新代码。

答案 1 :(得分:0)

create table语句中的

正在创建问题:

create table events (_id integer primary key autoincrement,location text not null, date text not null, support text not null)

直接使用它...不需要连接数据..这种连接会产生问题......