在Android中,创建或升级数据库时可以使用SQLiteOpenHelper
类。通过重写其onCreate()
和onUpgrade()
方法来执行SQL语句。,,``java,public class MyDatabaseHelper extends SQLiteOpenHelper {, private static final String DATABASE_NAME = "mydatabase.db";, private static final int DATABASE_VERSION = 1;,, public MyDatabaseHelper(Context context) {, super(context, DATABASE_NAME, null, DATABASE_VERSION);, },, @Override, public void onCreate(SQLiteDatabase db) {, db.execSQL("CREATE TABLE mytable (id INTEGER PRIMARY KEY, name TEXT)");, },, @Override, public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {, db.execSQL("DROP TABLE IF EXISTS mytable");, onCreate(db);, },},
``