请继承SQLiteOpenHelper实现

   1.创建一个版本为1“diaryOpenHelper.db”的数据库,
   2
.同时创建一个 “diary” 表(包含一个_id主键并自增长,topic字符型100
       
长度, content字符型1000长度)
   3
.在数据库版本变化时请删除diary表,并重新创建出diary表。

publicclass DBHelper  extends SQLiteOpenHelper{

       public final static StringDATABASENAME ="diaryOpenHelper.db";
       public final static intDATABASEVERSION =1;

       //
创建数据库
       public DBHelper(Contextcontext,Stringname,CursorFactory factory,int version)
       {
              super(context,name, factory,version);
       }
       //
创建表等机构性文件
       public void onCreate(SQLiteDatabasedb)
       {
              Stringsql ="create tablediary"+
                                   "("+
                                   "_idintegerprimary key autoincrement,"+
                                   "topicvarchar(100),"+
                                   "contentvarchar(1000)"+
                                   ")";
              db.execSQL(sql);
       }
       //
若数据库版本有更新,则调用此方法

       public voidonUpgrade(SQLiteDatabasedb,int oldVersion,int newVersion)
       {
              
              Stringsql = "drop table ifexists diary";
              db.execSQL(sql);
              this.onCreate(db);
       }
}


你可能感兴趣的:(Android常用面试题汇总)