Android SQLite
Android系统是内置了数据库的——SQLite。
创建数据库
- SQLiteOpenHelper帮助类。借助这个类可以非常简单的对数据库进行创建和升级。这是一个抽象类,需要重写onCreate()和onUpgrade()方法。还有两个重要方法:getReadableDatabase()和getWritableDatabase()。这两个方法都可以打开或者创建一个数据库,并返回一个可对数据库进行读写操作的对象。
- 构造方法:一般使用4个参数,Context(上下文)、数据库名、Cursor(一般传入null)、表示当前数据库版本号
- 数据库文件会放在/data/data/
/database/目录下。此时,重写的onCreae()方法也会得到执行,所以通常会在处理一些创建表的逻辑。
public class MyDatabaseHelper extends SQLiteOpenHelper {
private static final String CREATE_BOOK="create table Book ("
+"id integer primary key autoincrement,"
+"author text,"
+"price real,"
+"pages integer,"
+"name text)";
private static final String CREATE_CATEGORY="create table Category ("
+"id integer primary key autoincrement,"
+"category_name text,"
+"category_code integer)";
private Context mContext;
public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
this.mContext = context;
}
//表不存在,第一次创建时会调用的方法
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOK);
db.execSQL(CREATE_CATEGORY);
Toast.makeText(mContext,"Create succeeded",Toast.LENGTH_SHORT).show();
}
//当传进来的版本号大于当前数据库的版本号时,就会运行这个函数
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists Book");
db.execSQL("drop table if exists Category");
onCreate(db);
}
}
public class MainActivity extends AppCompatActivity {
private MyDatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//dbHelper=new MyDatabaseHelper(this,"BookStore.db",null,1);
dbHelper=new MyDatabaseHelper(this,"BookStore.db",null,2);//更新
Button create=(Button)findViewById(R.id.create_database);
create.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper.getWritableDatabase();//当有这个数据库的时候直接返回,没有的话调用onCreate()方法创建
}
});
}
增删改查
增加更新一般需要ContentValues组装数据。查询方法参数较多,用时再查。
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
//开始组装第一条数据
values.put("name","Android Studying");
values.put("author","G");
values.put("pages",450);
values.put("price",16.54);
db.insert("Book",null,values);//插入第一条数据
values.clear();
//开始组装第二条
values.put("name","Android Studying2");
values.put("author","G2");
values.put("pages",900);
values.put("price",20);
db.insert("Book",null,values);//插入第二条数据
}
});
//update()方法,第一个参数是表名,第二个是ContentValues,第三个是条件(相当于SQL中的where),第四个是条件参数
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("price",10.99);
db.update("Book",values,"name=?",new String[]{"Android Studying"});
}
});
//delete()方法,三个参数,第一个参数表名、第二三是删除的那几列,不指定的话默认删除所有行
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
db.delete("Book","price<?",new String[]{"11"});
}
});
query.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db= dbHelper.getWritableDatabase();
//查询Book表中所有的数据
Cursor cursor=db.query("Book",null,null,null,null,null,null);
if(cursor.moveToFirst()){
do{
//遍历Cursor对象,取出数据并打印
String name=cursor.getString(cursor.getColumnIndex("name"));
String author=cursor.getString(cursor.getColumnIndex("author"));
int pages=cursor.getInt(cursor.getColumnIndex("pages"));
double price =cursor.getDouble(cursor.getColumnIndex("price"));
Log.d("MainActivity","book name is "+name);
Log.d("MainActivity","book author is "+author);
Log.d("MainActivity","book pages is "+pages);
Log.d("MainActivity","book price is "+price);
}while(cursor.moveToNext());
}
cursor.close();
}
});
}
SQL语言
添加数据
db.execSQL("insert into Book (name,author,pages,price) values (?,?,?,?)",new String[]{"Android Studying","G","450","16.54"});
db.execSQL("insert into Book (name,author,pages,price) values (?,?,?,?)",new String[]{"Android Studying2","G2","900","20"});
更新数据
db.execSQL("update Book set price=? where name=?",new String[]{"10.99","Android Studying"});
删除数据
db.execSQL("delete from Book where price<?",new String[]{"11"});
查询数据
db.rawQuery("select * from Book",null);