当我在sqlite数据库中创建一个表时,它显示以下错误

时间:2016-07-22 07:50:15

标签: android database sqlite cursor

我创建了一个 sqlite 数据库,并存储了另一个类的项目列表,但现在它显示了该表不存在的错误

我的数据库

public class DBHelper extends SQLiteOpenHelper {

    private final static String DATABASE_NAME = "ContactLocation1";
    private final static int DATABASE_VERSION = 1;
 private static final String TABLE_LIST = "MyListItem";

    // Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_ListItem = "listitem";



    public DBHelper(Context context) {
        //1. Initialised database
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) { 
 String CREATE_LIST_TABLE = "CREATE TABLE " + TABLE_LIST + "(" + KEY_ID
                + " INTEGER," + KEY_ListItem + " TEXT" + ")";

        db.execSQL(CREATE_LIST_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  db.execSQL("DROP TABLE IF EXISTS " + TABLE_LIST);


        onCreate(db);

    }


    void addListItem(ArrayList<String> listItem) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        for (int i = 0; i < listItem.size(); i++) {

            Log.e("vlaue inserting==", "" + listItem.get(i));
            values.put(KEY_ListItem, listItem.get(i));
            db.insert(TABLE_LIST, null, values);

        }

        db.close(); // Closing database connection
    }

    Cursor getListItem() {
        String selectQuery = "SELECT  * FROM " + TABLE_LIST;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        return cursor;
    }
}

和 这是我的主要课程

 public class ContactDetails extends AppCompatActivity {
        String phoneNumber;

        ListView listview;
        ArrayList<String> arraylist= new ArrayList<String>();


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_contact_details);

            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            listview= (ListView) findViewById(R.id.lv);

            getNumber(this.getContentResolver());

            DBHelper db = new DBHelper(this);

            db.addListItem(arraylist);

            Cursor cursor = db.getListItem();

            Log.e("count", " " + cursor.getCount());
            if (cursor != null) {
                cursor.moveToNext();

                do {

                    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
                            android.R.layout.simple_list_item_1,arraylist);
                    listview.setAdapter(adapter1);

                    Log.e("value==", "" + cursor.getString(1));

                } while (cursor.moveToNext());
            }

        }

        private void getNumber(ContentResolver cr) {
            Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
            while (phones.moveToNext())
            {
                String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
              //  System.out.println(".................."+name);
                System.out.println(" "+phoneNumber);
                arraylist.add(phoneNumber);
    //            aa.add(name);

            }
            phones.close();// close cursor
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1,arraylist);
          //  listview.setAdapter(adapter);
            //display contact numbers in the list
        }
    }

它显示了这些行中的错误

 Cursor cursor = db.getListItem();

 Cursor cursor = db.rawQuery(selectQuery, null);

不存储数据n显示表不存在的错误。

0 个答案:

没有答案