ROR / AndroidRuntime:引起:android.database.sqlite.SQLiteException:没有这样的列类型:

时间:2012-02-05 19:06:05

标签: android sqlite listactivity

现在我再次找到了问题。

我想将db中的数据导入listview。

但它有错误我无法纠正这个问题。

请帮帮我

感谢!!

这是我的数据库

package com.BITS.DB;

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 Database {

    public String Serach = null;
    public static final String KEY_ROWID = "_id";
    public static final String KEY_WORD = "word";
    public static final String KEY_TYPE = "type";
    public static final String KEY_EXPLANTION = "Explanation";
    public static final String TAG = "DatabasePray";
    public static final String DATABASE_NAME = "BITSonMobiles";
    public static final String DATABASE_TABLE = "pray";
    public static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE = "create table pray"
            + "(_id integer primary key autoincrement," + "word char(40)NOT NULL,"
            + "Explanation text NOT NULL,"+"type text NOT NULL);";

    private Context context = null;
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;



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

    public Database open() throws SQLException {
        db = DBHelper.getWritableDatabase();
        return this;
    }

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

    public long insertWord(String word, String type, String Explanation) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_WORD, word);
        initialValues.put(KEY_TYPE, type);
        initialValues.put(KEY_EXPLANTION, Explanation);
        return db.replace(DATABASE_TABLE, null, initialValues);
    }

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

    }

    public boolean updateWord(long rowId, String type, String word, String Explanation) {
        ContentValues args = new ContentValues();
        args.put(KEY_WORD, word);
        args.put(KEY_TYPE, type);
        args.put(KEY_EXPLANTION, Explanation);
        return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
    }

    public Cursor getAllWord(String type) {
        return db.query(DATABASE_TABLE, new String[] {
                KEY_ROWID,
                KEY_WORD,
                KEY_TYPE,
                KEY_EXPLANTION }, 
                KEY_TYPE + "=" + type , 
                null, 
                null, 
                null, 
                KEY_WORD);
    }

    public Cursor searchWord(String Wordcom) throws SQLException {

        Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
                KEY_ROWID,
                KEY_WORD,
                KEY_TYPE,
                KEY_EXPLANTION },
                KEY_WORD + "=" + Wordcom,
                null, 
                null, 
                null, 
                KEY_WORD, 
                null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }


    public Cursor getWord(long rowId) throws SQLException {

        Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
                KEY_ROWID,
                KEY_WORD,
                KEY_TYPE,
                KEY_EXPLANTION },
                KEY_ROWID + "=" + rowId,
                null, 
                null, 
                null, 
                null, 
                null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

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

}

和tihs main

package com.BITS.DB;

import java.util.ArrayList;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class ChoicePray extends ListActivity {

    AlertDialog dialog;
    EditText wordNameSearch;
    TextView typeShow;
    ImageView imageSearch;
    static Database db;
    static String database;
    String wordSearch = null;
    private int idWord;
    public String type="Pray";

    public static Context context;
    private static final ArrayList<String> ListviewContent = new ArrayList<String>();
    private static final ArrayList<Integer> ListID = new ArrayList<Integer>();
    ListViewAdapter listAdap;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choicepray);
        typeShow = (TextView) findViewById(R.id.groupshow);
        wordNameSearch = (EditText) findViewById(R.id.namePraySearch);
        imageSearch = (ImageView) findViewById(R.id.imageSearch);
        context = getBaseContext();
        db = new Database(this);
        showAllword();
        typeShow.setText("Pray");
        listAdap = new ListViewAdapter(this);
        listAdap.notifyDataSetChanged();
        setListAdapter(listAdap);
        SearchOnButton();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
        }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) { 
    case R.id.add:{  
            Intent a = new Intent(getBaseContext(),Insert_Form.class);
            startActivity(a);
            break;
            }
    case R.id.pray:{
            type="A";
            typeShow.setText("A");
            showAllword();
            break;
            }
    case R.id.pray1:{
            type="B";
            typeShow.setText("B");
            showAllword();
            break;
            }
    case R.id.pray2:{
            type="C";
            typeShow.setText("C");
            showAllword();
            break;
            }
        } 
    return true; 
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
             AlertDialog dialog = new AlertDialog.Builder(this).create();
             dialog.setMessage("ARE YOU SURE EXIT ?");
             dialog.setButton(DialogInterface.BUTTON_POSITIVE, "YES",new DialogInterface.OnClickListener() { 
                 public void onClick(DialogInterface dialog, int which) {
                     {   finish();
                         android.os.Process.killProcess(android.os.Process.myPid());
                         System.exit(0);  
                        }
                     dialog.cancel();
                 } 
            });
            dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "NO", 
                     new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int which) {
                         dialog.cancel();
                     } 
           });
            dialog.show();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void SearchOnButton(){
        imageSearch.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                wordSearch = wordNameSearch.getText().toString();
                ListID.clear();
                ListviewContent.clear();
                db = new Database(context);
                db.open();
                Cursor c = db.searchWord(wordSearch);
                if (c.moveToFirst()) {
                    do {
                        displayPray(c);
                    } while (c.moveToNext());
                }
                db.close();
                wordNameSearch.setText("");
                typeShow.setText("All");
                listAdap.notifyDataSetChanged();
                setListAdapter(listAdap);
            }

        });

    }

    public void ButtonAlertDialog(final int position){
         AlertDialog dialog = new AlertDialog.Builder(this).create();
         dialog.setMessage("ARE YOU SURE DELETE WORD ?");
         dialog.setButton(DialogInterface.BUTTON_POSITIVE, "YES",
                 new DialogInterface.OnClickListener() { 
                 public void onClick(DialogInterface dialog, int which) {
                     {
                            Log.v("Delete", "delete id" + ListID.get(position));
                            db.open();
                            boolean flag = db.deleteWord(ListID.get(position));
                            if (flag) {
                                Toast.makeText(context, "Delete Succeed.",
                                        Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(context, "Delete Failed",
                                        Toast.LENGTH_SHORT).show();
                            }
                            db.close();
                            showAllword(); 
                        }
                     dialog.cancel();
                 } 
       });
         dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "NO", 
                 new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int which) {
                     dialog.cancel();
                 } 
       });
         dialog.show();
    }

    public  void showAllword() {
        ListID.clear();
        ListviewContent.clear();
        db = new Database(context);
        db.open();
        Cursor c = db.getAllWord(type);
        if (c.moveToFirst()) {
            do {
                displayPray(c);
            } while (c.moveToNext());
        }
        db.close();
        listAdap = new ListViewAdapter(this);
        listAdap.notifyDataSetChanged();
        setListAdapter(listAdap);
    }

    public static void displayPray(Cursor c) {
        ListviewContent.add(c.getString(1));
        ListID.add(Integer.parseInt(c.getString(0)));
    }


    private class ListViewAdapter extends BaseAdapter {
        public LayoutInflater mInflater;

        public ListViewAdapter(Context context) {
            mInflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            return ListviewContent.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {

            ListContent holder;
            View v = convertView;
            if (v == null || (v.getTag()==null)) {
                    v = mInflater.inflate(R.layout.list_item, null);
                    holder = new ListContent();
                    holder.text = (TextView) v.findViewById(R.id.TextView01);
                    v.setTag(holder);
            }
                    else {
                holder = (ListContent) v.getTag();
            }


            v.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    Intent ex = new Intent(getBaseContext(), Explan.class);
                    idWord = ListID.get(position);
                    ex.putExtra("idWord", idWord);
                    ex.putExtra("type", type);
                    startActivity(ex);
                }
            });

            holder.text.setText(ListviewContent.get(position)); 
            v.setOnLongClickListener(new OnLongClickListener(){
                 @Override
                 public boolean onLongClick(View v) {
                     ButtonAlertDialog(position);
                        return true;
                    }
                });
                return v;
        }

        private class ListContent {
            TextView text;  
        }
    }


}

以下是来自LogCat的错误消息:

02-06 01:00:01.832: ERROR/AndroidRuntime(13689): FATAL EXCEPTION: main
02-06 01:00:01.832: ERROR/AndroidRuntime(13689): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.BITS.DB/com.BITS.DB.ChoicePray}: android.database.sqlite.SQLiteException: no such column: type: , while compiling: SELECT _id, word, type, Explanation FROM pray WHERE type=Pray ORDER BY word
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at android.os.Looper.loop(Looper.java:123)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at java.lang.reflect.Method.invokeNative(Native Method)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at java.lang.reflect.Method.invoke(Method.java:521)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at dalvik.system.NativeStart.main(Native Method)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689): Caused by: android.database.sqlite.SQLiteException: no such column: type: , while compiling: SELECT _id, word, type, Explanation FROM pray WHERE type=Pray ORDER BY word
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1229)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1184)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1264)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at com.BITS.DB.Database.getAllWord(Database.java:69)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at com.BITS.DB.ChoicePray.showAllword(ChoicePray.java:187)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at com.BITS.DB.ChoicePray.onCreate(ChoicePray.java:53)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-06 01:00:01.832: ERROR/AndroidRuntime(13689):     ... 11 more

那么,任何人都可以帮忙解决这个问题吗?提前谢谢!

1 个答案:

答案 0 :(得分:0)

在我看来,你的代码是正确的。但我认为问题是在您错误地创建数据库之前。所以我建议做以下步骤:

尝试在您的create语句中添加空格:

private static final String DATABASE_CREATE = "create table pray"
            + "(_id integer primary key autoincrement, " + "word char(40) NOT NULL, "
            + "Explanation text NOT NULL, "+"type text NOT NULL);";

之后,从/ data / data / yourpackage / databases文件夹中删除数据库并运行您的应用程序。