一.定义DBHelper继承SQLiteOpenHelper
public class DBHelper extends SQLiteOpenHelper{
private final Context mContext;
public DBHelper(Context context,String name,int version) {
super(context,name,null,version);
mContext = context;
}
//数据第一次创建的时候会调用onCreate
@Override
public void onCreate(SQLiteDatabase db) {
//创建表
/*
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//在表info中增加一列other
//db.execSQL("ALTER TABLE info ADD COLUMN other STRING");
}
}
二.使用数据库
1.定义初始化:并创建数据库,有则不创建。
void initbusroute()
{
helper = new DBHelper(this,"busroute.db",1);
db = helper.getWritableDatabase();
db.execSQL("CREATE TABLE IF NOT EXISTS busroute (ROUTENAME TEXT ,FIRSTNAME TEXT ," +
"LASTNAME TEXT ,ROUTE_DIR TEXT,ROUTE_CODE TEXT )");
}
2.向数据库中添加数据:
private void addroute() {
db.execSQL("INSERT INTO busroute VALUES(?,?,?,?,?)", new Object[]{routename, firstname, lastname, route_dir, route_code});
}
3.按照条件删除数据库表:
private void DelRouteDb(String routecd, String routedir) {
String sql = "delete from busroute where (ROUTE_CODE='" + routecd + "' AND ROUTE_DIR='" + routedir + "')";//删除操作的SQL语句
db.execSQL(sql);//执行删除操作
}