为什么表没有创建?

时间:2016-06-09 08:24:47

标签: android sqlite

我试图从数据库中检索数据但是logcat显示msg“没有这样的表”一切看起来都不错,但我不明白为什么会出现这个问题。任何一个请求帮助我新的android。

这属于主要活动:

public class AllContact extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.allcontactlayout_main);

      TextView  text=(TextView)findViewById(R.id.textView);
       TextView text2=(TextView)findViewById(R.id.textView2);

        ContactDatabase onbOfContactDatabase=new ContactDatabase(getBaseContext());

       Cursor get= onbOfContactDatabase.showData();
    }
}

//当我调用showData()时,logcat显示msg“没有这样的表”

这属于数据库:

    public class ContactDatabase extends SQLiteOpenHelper {
        SQLiteDatabase db;
        public static final String DATABASE_NAME="totalContact.db";
        public static final  String TABLE_NAME="contact";
        public static final  String NAME="name";
        public static final  String PHONE="phone";

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

        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            try {
                db.execSQL("craete table contact" +
                        "(id integer primary key autoincrement, name text, phone text)");
            }catch(android.database.SQLException e){
                    System.out.println("table create nhi ho rha");
            }
        }

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

   public void insertContact(String nam,String mob){

            db=this.getWritableDatabase();
            ContentValues contentValues=new ContentValues();

            contentValues.put(NAME,nam);
            contentValues.put(PHONE,mob);

            db.insert(TABLE_NAME, null, contentValues);
            db.close();
        }

      public Cursor showData(){

            db=this.getWritableDatabase();
            Cursor res =  db.rawQuery("SELECT  * FROM contact", null);
            return res;

        }
    }

0 个答案:

没有答案