1.
//继承SQLiteOpenHelper类
public class SqlHelper extends SQLiteOpenHelper{
//重写构造初始化数据库
public SqlHelper(Context context) {
super(context,"info.db",null,1);
}
//创建表
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table bwie(id Integer primary key autoincrement ,data text)");
}
//更新表
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
2.
//数据库工具类
public class Sqlutils {
private SQLiteDatabase db;
public Sqlutils(Context context) {
SqlHelper helper = new SqlHelper(context);
db = helper.getWritableDatabase();
}
//添加
public long insert(String data) {
ContentValues values = new ContentValues();
values.put("data", data);
return db.insert("bwie", null, values);
}
//查询字符串
public String query() {
String dd="";
Cursor cursor = db.rawQuery("select data from bwie", null);
while (cursor.moveToNext()) {
String data = cursor.getString(cursor.getColumnIndex("data"));
dd= data;
}
return dd;
}
//查询全部
public List<String> select() {
List<String> mDatas= new ArrayList<>();
Cursor cursor = db.rawQuery("select data from bwie", null);
while (cursor.moveToNext()) {
String data = cursor.getString(cursor.getColumnIndex("data"));
mDatas.add(data);
}
return mDatas;
}
//删除全部
public void clear() {
db.delete("bwie",null,null);
}
}