安卓存储数据之一数据库的使用
做安卓项目肯定都会用到数据库存储数据的,安卓也给我们提供了一个SQLite数据库以及操作数据库的类SQLiteOpenHelper。
我们就可以轻松得对自己的数据进行增删改查了。
下面上数据库操作类的代码:
ublic class MySQLiteOpenHelper extends SQLiteOpenHelper{
/**
* context :上下文
* name:数据库名
* factory:游标工厂,设为null则使用默认的游标工厂
* version:数据库版本号 ,一般起始设为1
*
* */
public MySQLiteOpenHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, "country", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//数据库被创建
System.out.println("数据库被创建");
db.execSQL("create table country (_id integer primary key autoincrement," +
"name varchar(20)," +
"big varchar(20))");
db.execSQL("create table city(_id integer primary key antoincreament," +
"city varchar(20)," +
"mianji integer)");
}
//数据库更新的时候调用 int version发生改变的时候调用此方法
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch (oldVersion) {
//数据库的更新。新建一张city表
case 1:
db.execSQL("create table city(_id integer primary key antoincreament," +
"city varchar(20)," +
"mianji integer)");
//数据库的更新,给已存在的表中增加字段
case 2:
db.execSQL("alert table country add countryId integer");
default:
break;
}
}
}
/**
* context :上下文
* name:数据库名
* factory:游标工厂,设为null则使用默认的游标工厂
* version:数据库版本号 ,一般起始设为1
*
* */
public MySQLiteOpenHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, "country", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//数据库被创建
System.out.println("数据库被创建");
db.execSQL("create table country (_id integer primary key autoincrement," +
"name varchar(20)," +
"big varchar(20))");
db.execSQL("create table city(_id integer primary key antoincreament," +
"city varchar(20)," +
"mianji integer)");
}
//数据库更新的时候调用 int version发生改变的时候调用此方法
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch (oldVersion) {
//数据库的更新。新建一张city表
case 1:
db.execSQL("create table city(_id integer primary key antoincreament," +
"city varchar(20)," +
"mianji integer)");
//数据库的更新,给已存在的表中增加字段
case 2:
db.execSQL("alert table country add countryId integer");
default:
break;
}
}
}
以上就是SQLiteOpenHelper的使用,注释都已经写清楚 了,就不做解释
下面就是我们要进行增删改查的代码:
ublic class MainActivity extends Activity implements OnClickListener{
private Button btn_add,btn_delete,btn_change,btn_check;
private MySQLiteOpenHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper=new MySQLiteOpenHelper(this, "country", null, 1);
initView();
}
private void initView() {
btn_add=(Button)findViewById(R.id.btn_add);
btn_delete=(Button)findViewById(R.id.btn_delete);
btn_change=(Button)findViewById(R.id.btn_change);
btn_check=(Button)findViewById(R.id.btn_check);
btn_add.setOnClickListener(this);
btn_delete.setOnClickListener(this);
btn_change.setOnClickListener(this);
btn_check.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_add:
add();
break;
case R.id.btn_delete:
delete();
break;
case R.id.btn_change:
change();
break;
case R.id.btn_check:
check();
break;
default:
break;
}
}
//添加数据
private void add() {
SQLiteDatabase database=helper.getWritableDatabase();
Random random=new Random();
database.execSQL("insert into country (name,big) values (?,?)", new Object[]{"中国","1000"+random.nextInt(100)});
Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
System.out.println("添加成功");
database.close();
}
//删除数据
private void delete() {
SQLiteDatabase database=helper.getWritableDatabase();
database.execSQL("delete from country");
Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
System.out.println("删除成功");
database.close();
}
//改动数据
private void change() {
SQLiteDatabase database=helper.getWritableDatabase();
database.execSQL("update country set big=? where name=?",new String []{"2222","中国"});
// database.execSQL("update country set name=?",new String[]{"美国"});
Toast.makeText(this, "更新成功", Toast.LENGTH_SHORT).show();
System.out.println("更新成功");
database.close();
}
//查找单个数据
private void check() {
SQLiteDatabase database=helper.getReadableDatabase();
Cursor cursor=database.rawQuery("select name,big from country where name=?",new String[]{"中国"} );
//如果还有下一个
while(cursor.moveToNext()){
String name=cursor.getString(cursor.getColumnIndex("name"));
String big=cursor.getString(cursor.getColumnIndex("big"));
System.out.println(name+":"+big);
}
System.out.println("查找成功");
cursor.close();
database.close();
}
//查找所有数据
private void checkAll(){
SQLiteDatabase database=helper.getReadableDatabase();
Cursor cursor=database.rawQuery("select *from country", null);
while(cursor.moveToNext()){
String name=cursor.getString(cursor.getColumnIndex("name"));
String big=cursor.getString(cursor.getColumnIndex("big"));
System.out.println(name+":"+big);
}
System.out.println("查找成功");
cursor.close();
database.close();
}
}
private Button btn_add,btn_delete,btn_change,btn_check;
private MySQLiteOpenHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper=new MySQLiteOpenHelper(this, "country", null, 1);
initView();
}
private void initView() {
btn_add=(Button)findViewById(R.id.btn_add);
btn_delete=(Button)findViewById(R.id.btn_delete);
btn_change=(Button)findViewById(R.id.btn_change);
btn_check=(Button)findViewById(R.id.btn_check);
btn_add.setOnClickListener(this);
btn_delete.setOnClickListener(this);
btn_change.setOnClickListener(this);
btn_check.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_add:
add();
break;
case R.id.btn_delete:
delete();
break;
case R.id.btn_change:
change();
break;
case R.id.btn_check:
check();
break;
default:
break;
}
}
//添加数据
private void add() {
SQLiteDatabase database=helper.getWritableDatabase();
Random random=new Random();
database.execSQL("insert into country (name,big) values (?,?)", new Object[]{"中国","1000"+random.nextInt(100)});
Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
System.out.println("添加成功");
database.close();
}
//删除数据
private void delete() {
SQLiteDatabase database=helper.getWritableDatabase();
database.execSQL("delete from country");
Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
System.out.println("删除成功");
database.close();
}
//改动数据
private void change() {
SQLiteDatabase database=helper.getWritableDatabase();
database.execSQL("update country set big=? where name=?",new String []{"2222","中国"});
// database.execSQL("update country set name=?",new String[]{"美国"});
Toast.makeText(this, "更新成功", Toast.LENGTH_SHORT).show();
System.out.println("更新成功");
database.close();
}
//查找单个数据
private void check() {
SQLiteDatabase database=helper.getReadableDatabase();
Cursor cursor=database.rawQuery("select name,big from country where name=?",new String[]{"中国"} );
//如果还有下一个
while(cursor.moveToNext()){
String name=cursor.getString(cursor.getColumnIndex("name"));
String big=cursor.getString(cursor.getColumnIndex("big"));
System.out.println(name+":"+big);
}
System.out.println("查找成功");
cursor.close();
database.close();
}
//查找所有数据
private void checkAll(){
SQLiteDatabase database=helper.getReadableDatabase();
Cursor cursor=database.rawQuery("select *from country", null);
while(cursor.moveToNext()){
String name=cursor.getString(cursor.getColumnIndex("name"));
String big=cursor.getString(cursor.getColumnIndex("big"));
System.out.println(name+":"+big);
}
System.out.println("查找成功");
cursor.close();
database.close();
}
}
以上就是SQlite数据库的增删改查的简单使用。