SQLiteOpenHelper文件代码是什么
时间: 2023-12-08 16:03:53 浏览: 92
SQLHelper代码详解
以下是一个简单的 SQLiteOpenHelper 文件代码:
```
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "myDatabase.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "myTable";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_NAME + " TEXT)";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);
}
}
```
这个类继承自 SQLiteOpenHelper 类,用于创建和升级 SQLite 数据库。它包含数据库名称、版本、表名称和列名称等常量,以及创建表的 SQL 语句。在 onCreate 方法中执行 CREATE TABLE 语句创建表,而在 onUpgrade 方法中执行 DROP TABLE 语句删除旧表并重新创建新表。
阅读全文