点击(此处)折叠或打开
-
package org.lxh.demo;
-
import android.content.Context;
-
import android.database.sqlite.SQLiteDatabase;
-
import android.database.sqlite.SQLiteOpenHelper;
-
public class MyDatabaseHelper extends SQLiteOpenHelper { // 继承SQLiteOpenHelper类
-
private static final String DATABASENAME = "mldn.db" ; // 数据库名称
-
private static final int DATABASEVERSION = 1 ; // 数据库名称
-
private static final String TABLENAME = "mytab" ; // 数据表名称
-
public MyDatabaseHelper(Context context) {
-
super(context, DATABASENAME, null, DATABASEVERSION);// 调用父类构造
-
}
-
@Override
-
public void onCreate(SQLiteDatabase db) { // 创建数据表
-
String sql = "CREATE TABLE " + TABLENAME + " (" +
-
"id INTEGER PRIMARY KEY ," +
-
"name VARCHAR(50) NOT NULL ," +
-
"birthday DATE NOT NULL)";// SQL语句
-
db.execSQL(sql) ; // 执行SQL语句
-
}
-
@Override
-
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
-
String sql = "DROP TABLE IF EXISTS " + TABLENAME ; // SQL语句
-
db.execSQL(sql); // 执行SQL语句
-
this.onCreate(db); // 创建表
-
}
- }