创建数据库
public class MyHepler extends SQLiteOpenHelper {
//TODO 1:构造方法
//参数一 上下文 参数二 数据库名称 参数三 工厂 null 参数四 版本号 1
public MyHepler( Context context) {
super(context, "1704.db", null, 1);
}
//TODO 2: 创建数据库的过程 创建表+初始化数据
@Override
public void onCreate(SQLiteDatabase db) {
//造表
db.execSQL("create table student(id integer primary key autoincrement,name varchar(30),age integer )");
//默认初始化数据
db.execSQL("insert into student values(null,'小明',19)");
db.execSQL("insert into student values(null,'小杨',29)");
db.execSQL("insert into student values(null,'小红',17)");
db.execSQL("insert into student values(null,'小静',9)");
}
//TODO 3:更新数据库用的
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
/**
* 一。复习sql语句
* 1.创建表:create table 表名(字段名 类型);
* 2.插入数据:insert into 表名 values(......);
* 3.删除数据:delete from 表名 where .....;
* 4.修改数据:update 表名 set 字段名=值 where .....
* 5.查询数据:select 字段列表 from 表明 where .....
* 二。sqlite介绍
* 简单,本地,不需要安装
* 存储位置:data/data/应用程序包名/databases文件夹/Xxx.db
* 三。实现
* 1.创建数据库:SQLiteOpenHelper
* (1)自定义类继承SQLiteOpenHelper
* 2.增删改查:SQLiteDatabase
* (1)增删改:execSQL(sql语句,String[] 占位符)
* (2)查询:rawQuery(sql语句,String[] 占位符)
* 3.增删改查:SQLiteDatabase
* (1)增加:insert()
* (2)删除:delete()
* (3)修改:update()
* (4)查询:query()
*四.事务
* 1.什么是事务?
* 将多件事情当作一件事情来处理。 全班在一条船上,要不全活,要不全over。
* 2.应用?
* (1)批量插入大量数据:节省时间(和磁盘交互的时间)
* 假如插入500条数据,就要和磁盘交互500次,如果当作一件事情和磁盘交互1次,节省时间
* (2)银行转账
*
* */
public class SqliteActivity extends AppCompatActivity implements View.OnClickListener {
MyHepler myHepler;
SQLiteDatabase database;
Button bt_insert,bt_delete,bt_update,bt_query,bt_insert_all;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sqlite);
bt_insert=findViewById(R.id.bt_insert);
bt_insert.setOnClickListener(this);
bt_delete=findViewById(R.id.bt_delete);
bt_delete.setOnClickListener(this);
bt_update=findViewById(R.id.bt_update);
bt_update.setOnClickListener(this);
bt_query=findViewById(R.id.bt_query);
bt_query.setOnClickListener(this);
bt_insert_all=findViewById(R.id.bt_insert_all);
bt_insert_all.setOnClickListener(this);
//TODO 1:创建数据库
myHepler = new MyHepler(this);
database = myHepler.getWritableDatabase();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_delete:
delete();
break;
case R.id.bt_insert:
insert();
break;
case R.id.bt_update:
upadte();
break;
case R.id.bt_query:
query();
break;
case R.id.bt_insert_all:
insert_all();
break;
}
}
private void insert() {
//database.execSQL("insert into student values(null,?,?)",new String[]{"理由","56"});
//String table, String nullColumnHack, ContentValues values
//参数一 表明 参数二 默认值null 参数三:你要插入的值
ContentValues contentValues = new ContentValues();
contentValues.put("name","理由");
contentValues.put("age",56);
//该返回值 代表你插入的表中的第几条数据 加入表中有80条 你插入一条后 返回81
long num = database.insert("student", null, contentValues);
if(num>0){
Toast.makeText(this, "插入成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "插入失败", Toast.LENGTH_SHORT).show();
}
}
private void upadte() {
// database.execSQL("update student set age=? where name=?",new String[]{"78","理由"});
//String table, ContentValues values, String whereClause, String[] whereArgs
//参数一 表明 参数二 要修改值 参数三 条件 参数四 为条件?赋值
ContentValues contentValues = new ContentValues();
contentValues.put("age",78);
int num = database.update("student", contentValues, "name=?", new String[]{"理由"});
if(num>0){
Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "修改失败", Toast.LENGTH_SHORT).show();
}
}
private void delete() {
//方案一:
// database.execSQL("delete from student where name=?",new String[]{"理由"});
//方案二:
//String table, String whereClause, String[] whereArgs
//参数一 表名 参数二 条件 参数三 为条件中?赋值
int num = database.delete("student", "name=?", new String[]{"理由"});
if(num>0){
Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show();
}
}
//查询数据 rawQuery
private void query() {
//方案一:
// Cursor cursor = database.rawQuery("select * from student where age>? ", new String[]{"18"});
// //遍历游标
// while (cursor.moveToNext()){
// //cursor.getColumnIndex("id") 根据列名获得字段下标
// int id = cursor.getInt(cursor.getColumnIndex("id"));
// String name=cursor.getString(cursor.getColumnIndex("name"));
// int age=cursor.getInt(cursor.getColumnIndex("age"));
// Toast.makeText(this, ""+name+":"+age, Toast.LENGTH_SHORT).show();
// }
//方案二:query
//String table, String[] columns, String selection,String[] selectionArgs, String groupBy, String having,String orderBy
//参数一 :表名
//参数二:要查询字段列表 new String[]{"id","name"} 查询所有字段:null
//参数三:条件 where name=?
//参数四:为条件中的?赋值
//参数五:分组 没有null
//参数六:分组后的条件 没有null
//参数七:排序 没有null
//案例1:select * from student
// Cursor cursor = database.query("student", null, null, null, null, null, null);
// while (cursor.moveToNext()){
// //cursor.getColumnIndex("id") 根据列名获得字段下标
// int id = cursor.getInt(cursor.getColumnIndex("id"));
// String name=cursor.getString(cursor.getColumnIndex("name"));
// int age=cursor.getInt(cursor.getColumnIndex("age"));
// Toast.makeText(this, ""+name+":"+age, Toast.LENGTH_SHORT).show();
// }
//案例二:select * from student where age>50 order by age desc;
Cursor cursor =database.query("student",null,"age>?",new String[]{"30"},null,null,"age desc");
while (cursor.moveToNext()){
//cursor.getColumnIndex("id") 根据列名获得字段下标
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name=cursor.getString(cursor.getColumnIndex("name"));
int age=cursor.getInt(cursor.getColumnIndex("age"));
Toast.makeText(this, ""+name+":"+age, Toast.LENGTH_SHORT).show();
}
}
private void insert_all() {
//不使用事务
// long start = System.currentTimeMillis();
// for(int i=0;i<500;i++){
// database.execSQL("insert into student values(null,?,?)",new String[]{"理由"+i+"",i+""});
// }
// long end = System.currentTimeMillis();
// Toast.makeText(this, ""+(end-start), Toast.LENGTH_SHORT).show();
long start = System.currentTimeMillis();
//开启事务
database.beginTransaction();
try {
for(int i=0;i<500;i++){
database.execSQL("insert into student values(null,?,?)",new String[]{"理由"+i+"",i+""});
}
database.setTransactionSuccessful();//如果上面没有产生错误,就会执行该方法提交 如果报错就不执行该方法
}catch (Exception e){
e.printStackTrace();
}finally {
//最终都要关闭事务
database.endTransaction();
}
long end = System.currentTimeMillis();
Toast.makeText(this, ""+(end-start), Toast.LENGTH_SHORT).show();
}
}