Sqlite数据库或表不存在

时间:2016-10-25 13:07:22

标签: android sqlite

我创建了一个包含3个表的数据库:名称,描述和复选框。当我尝试插入数据时,我得到一个异常,即我的数据库不存在或表缺失。我肯定知道数据库在那里,我不确定表列是怎么回事。 我有这样的帮助类:

    public class DBHelper extends SQLiteOpenHelper {

    public static final  String DATABASE_NAME ="ItemManager";
    public static final String TABLE_NAME = "Todo";
    public static final String COLUMN_ID = "id";
    public static final String ITEM_NAME = "name";

    public static final String DESCRIPTION = "description";
    public static final String COMPLETE = "complete";
    public HashMap hp;


    public DBHelper(Context context){
        super(context,DATABASE_NAME,null,1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table Todo " +
                "(id integer primary key, name text, description text, complete text)"
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS Todo");
        onCreate(db);
    }
    public boolean insertItem(String name, String description, String complete){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues content = new ContentValues();
        content.put("name",name);
        content.put("description",description);
        content.put("complete", complete);
        db.insert("Todo", null, content);
        return true;
    }
    public Cursor getData(int id){
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res = db.rawQuery("select * from Todo where id ="+id+"",null);
        return res;
    }
    public int numberOfRows(){
        SQLiteDatabase db = this.getReadableDatabase();
        int numRows = (int) DatabaseUtils.queryNumEntries(db, TABLE_NAME);
        return numRows;
    }
    public boolean updateItem(Integer id, String name, String description, String complete)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", name);
        contentValues.put("description", description);
        contentValues.put("complete", complete);

        db.update("Todo", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
        return true;
    }
    public Integer deleteItem(Integer id)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete("Todo",
                "id = ? ",
                new String[] { Integer.toString(id) });
    }
    public ArrayList<ItemsHolder> getAllItems()
    {
        ArrayList<ItemsHolder> array_list = new ArrayList<>();

        //hp = new HashMap();
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor res =  db.rawQuery("select * from Todo", null);


        while(res.moveToNext()){
            ItemsHolder itemsHolder = new ItemsHolder();
            itemsHolder.item= res.getString(res.getColumnIndex(ITEM_NAME));
            itemsHolder.Id = res.getInt(res.getColumnIndex(COLUMN_ID));
            array_list.add(itemsHolder);
        }
        return array_list;
    }

}

DisplayActivity.java

    public class DsiplayItems extends AppCompatActivity {

    private DBHelper mydb;
    TextView name,description;
    TextView complete;
    int id_To_Update = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dsiplay_items);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        name = (TextView) findViewById(R.id.editTextName);

        description = (TextView) findViewById(R.id.editTextDescription);
        complete = (EditText) findViewById(R.id.checkBox);
        final Button btnSave = (Button) findViewById(R.id.button);
        name.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if(s.toString().trim().length()==0){
                    btnSave.setEnabled(false);
                } else {
                    btnSave.setEnabled(true);
                }


            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });



        description.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if(s.toString().trim().length()==0){
                    btnSave.setEnabled(false);
                } else {
                    btnSave.setEnabled(true);
                }


            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });


        complete.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if(s.toString().trim().length()==0){
                    btnSave.setEnabled(false);
                } else {
                    btnSave.setEnabled(true);
                }


            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });


        mydb = new DBHelper(this);
        Bundle extras = getIntent().getExtras();
        if(extras != null){
            int Values = extras.getInt("id");
            if(Values > 0){
                Cursor re = mydb.getData(Values);
                id_To_Update=Values;
                re.moveToFirst();

                String nam = re.getString(re.getColumnIndex(DBHelper.ITEM_NAME));

                String des = re.getString(re.getColumnIndex(DBHelper.DESCRIPTION));
                String com = re.getString(re.getColumnIndex(DBHelper.COMPLETE));

                if (!re.isClosed())
                {
                    re.close();
                }
                Button b = (Button)findViewById(R.id.button);
                b.setVisibility(View.INVISIBLE);

                name.setText((CharSequence)nam);
                name.setFocusable(false);
                name.setClickable(false);



                description.setText((CharSequence)des);
                description.setFocusable(false);
                description.setClickable(false);

                complete.setText((CharSequence)com);
                complete.setFocusable(false);
                complete.setClickable(false);


            }
        }

    }
    public boolean isChecked(){
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        Bundle extras = getIntent().getExtras();

        if(extras !=null)
        {
            int Value = extras.getInt("id");
            if(Value>0){
                getMenuInflater().inflate(R.menu.display_items, menu);
            }

            else{
                getMenuInflater().inflate(R.menu.main_menu, menu);
            }
        }
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item)
    {
        super.onOptionsItemSelected(item);
        switch(item.getItemId())
        {
            case R.id.Edit_Contact:
                Button b = (Button)findViewById(R.id.button);
                b.setVisibility(View.VISIBLE);
                name.setEnabled(true);
                name.setFocusableInTouchMode(true);
                name.setClickable(true);



                description.setEnabled(true);
                description.setFocusableInTouchMode(true);
                description.setClickable(true);

                complete.setEnabled(true);
                complete.setFocusableInTouchMode(true);
                complete.setClickable(true);

                return true;
            case R.id.Delete_Contact:

                android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
                builder.setMessage(R.string.deleteContact)
                        .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                mydb.deleteItem(id_To_Update);
                                Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                                startActivity(intent);
                            }
                        })
                        .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // User cancelled the dialog
                            }
                        });
                android.app.AlertDialog d = builder.create();
                d.setTitle("Are you sure");
                d.show();

                return true;
            default:
                return super.onOptionsItemSelected(item);

        }
    }

    public void run(View view)
    {
        Bundle extras = getIntent().getExtras();
        if(extras !=null)
        {
            int Value = extras.getInt("id");
            if(Value>0){
                if(mydb.updateItem(id_To_Update, name.getText().toString(), description.getText().toString(), complete.getText().toString())){
                    Toast.makeText(getApplicationContext(), "Updated", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                    startActivity(intent);
                }
                else{
                    Toast.makeText(getApplicationContext(), "not Updated", Toast.LENGTH_SHORT).show();
                }
            }
            else{
                if(mydb.insertItem(name.getText().toString(), description.getText().toString(), complete.getText().toString())){
                    Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_SHORT).show();
                }

                else{
                    Toast.makeText(getApplicationContext(), "not done", Toast.LENGTH_SHORT).show();
                }
                Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                startActivity(intent);
            }
        }
    }
}

我正在接受这个

  

“插入名称时出错= grr description = ert complete = ComplerrtedTask                     android.database.sqlite.SQLiteException:table Todo没有名为name(code 1)的列:,编译时:INSERT INTO Todo(名称,描述,完整)VALUES(?,?,?)                     ################################################## ###############                     错误代码:1(SQLITE_ERROR)                     引起:SQL(查询)错误或缺少数据库。                       (表Todo没有列名为name(代码1):,编译时:INSERT INTO Todo(名称,描述,完整)VALUES(?,?,?))“exception。

我做错了什么?今天我已经多次重建这次没有成功

0 个答案:

没有答案