数据库入门---增删改查
来源:互联网 发布:剑三成男免费捏脸数据 编辑:程序博客网 时间:2024/05/22 03:52
使用google提供的另外一种方式操作数据库表:
1、插入数据
public void insert(View v){
//db.execSQL("insert into person (name)values(?)", new String[]{"lisi"});
ContentValues values = new ContentValues();
Random r = new Random();
values.put("name", "zhangsan"+r.nextInt(100));
long rowId = db.insert("person", null, values);
System.out.println("rowId="+rowId);
Toast.makeText(this, "插入数据成功", 0).show();
}
2、查询数据
public void query(View v){
/**
* table 表名
* columns 查询的列
* selection 查询条件"id=1"
* selectionArgs 查询条件的值
* String groupBy
* String having
* String orderBy
*
*/
Cursor cursor = db.query("person", new String[]{"id","name" }, null, null, null, null, null);
while(cursor.moveToNext()){
int id = cursor.getInt(0);
String name = cursor.getString(1);
System.out.println("id="+id+"; name="+name);
}
Toast.makeText(this, "查询数据成功", 0).show();
}
// db.execSQL("update person set name='wangwu' where id=?", new Object[]{1});
//用来封装要修改的列名和值
ContentValues values = new ContentValues();
values.put("name", "wangwu");
db.update("person", values, "id=?", new String[]{"1"});
Toast.makeText(this, "更新数据成功", 0).show();
}
4.删除数据
public void delete(View v){
// db.execSQL("delete from person where id=?", new Object[]{1});
db.delete("person", "id=?", new String[]{"2"});
Toast.makeText(this, "删除数据成功", 0).show();
}
5. 代码例子
1、插入数据
public void insert(View v){
//db.execSQL("insert into person (name)values(?)", new String[]{"lisi"});
ContentValues values = new ContentValues();
Random r = new Random();
values.put("name", "zhangsan"+r.nextInt(100));
long rowId = db.insert("person", null, values);
System.out.println("rowId="+rowId);
Toast.makeText(this, "插入数据成功", 0).show();
}
2、查询数据
public void query(View v){
/**
* table 表名
* columns 查询的列
* selection 查询条件"id=1"
* selectionArgs 查询条件的值
* String groupBy
* String having
* String orderBy
*
*/
Cursor cursor = db.query("person", new String[]{"id","name" }, null, null, null, null, null);
while(cursor.moveToNext()){
int id = cursor.getInt(0);
String name = cursor.getString(1);
System.out.println("id="+id+"; name="+name);
}
Toast.makeText(this, "查询数据成功", 0).show();
}
3.修改数据
public void update(View v){
// db.execSQL("update person set name='wangwu' where id=?", new Object[]{1});
//用来封装要修改的列名和值
ContentValues values = new ContentValues();
values.put("name", "wangwu");
db.update("person", values, "id=?", new String[]{"1"});
Toast.makeText(this, "更新数据成功", 0).show();
}
4.删除数据
public void delete(View v){
// db.execSQL("delete from person where id=?", new Object[]{1});
db.delete("person", "id=?", new String[]{"2"});
Toast.makeText(this, "删除数据成功", 0).show();
}
5. 代码例子
1.Dao层的方法封装
package com.example.asus.db;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.util.Log;/** * Created by ASUS on 2017/5/16. */public class Dao { private static final String TAG ="MyActivity"; private DBOpenHelper dbOpenHelper; public Dao(Context context) { dbOpenHelper=new DBOpenHelper(context); } public boolean add(Student student) { SQLiteDatabase db=dbOpenHelper.getReadableDatabase(); // db.execSQL("insert into student (name,phone) values (?,?);",new Object[]{student.name,student.phone}); // 表名 null表示添加一个空行 values 表示添加的一行的值 ContentValues values=new ContentValues(); // 是使用 map 封装,来存放值 values.put("name",student.name); values.put("phone",student.phone); // 返回值表示新一行的行id -1 表示添加失败 long result= db.insert("student",null,values); //这种方法有返回值 db.close(); if (result!=-1) { return true; } else return false; } public int del(String name) { SQLiteDatabase db=dbOpenHelper.getReadableDatabase(); //db.execSQL("delete from student where name=?;",new Object[]{name}); // 表名 where 删除条件 int result=db.delete("student","name=?",new String[]{name}); //返回值表示成功删除多少行 db.close(); return result; } public int update(Student student) { SQLiteDatabase db=dbOpenHelper.getReadableDatabase(); // db.execSQL("update student set phone=? where name=?;",new Object[]{student.name,student.phone}); // ContentValues values=new ContentValues();// 是用map封装,来存放值 values.put("name",student.name); int result=db.update("student",values,"name=?",new String[]{student.name}); db.close(); // 返回值表示成功修改多少行 return result; } public void query(String name) { SQLiteDatabase db=dbOpenHelper.getReadableDatabase(); // Cursor cursor=db.rawQuery("select _id,name,phone from student where name=?",new String[]{name}); Cursor cursor = db.query("student",null,"name=?",new String[]{name},null,null,"_id"); //这个方法没有第一种灵活,表名是写死的 // null 查询所有的 if (cursor!=null&&cursor.getCount()>0) { while (cursor.moveToNext()) { int id=cursor.getInt(0); String name1 = cursor.getString(1); String phone =cursor.getString(2); Log.i(TAG,"_id:"+id+";name:"+name1+";phone:"+phone); } cursor.close(); } db.close(); }}
2.Activity中的具体操作
package com.example.asus.db;import android.app.Activity;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity { private Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = this; DBOpenHelper dbopenhelper = new DBOpenHelper(context); SQLiteDatabase db = dbopenhelper.getReadableDatabase(); findViewById(R.id.Button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Dao dao = new Dao(context); Student student2 = new Student(); student2.name = "向++"; student2.phone = "10086"; boolean result=dao.add(student2); if (result) { Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_LONG).show(); } else { Toast.makeText(MainActivity.this, "GG", Toast.LENGTH_LONG).show(); } } }); findViewById(R.id.Button1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Dao dao = new Dao(context); int result= dao.del("向++"); Toast.makeText(MainActivity.this,"成功删除"+result+"行",Toast.LENGTH_LONG).show(); } }); findViewById(R.id.Button2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Dao dao = new Dao(context); Student student2=new Student(); student2.name="周++"; student2.phone="10010"; int result=dao.update(student2); Toast.makeText(MainActivity.this,"你成功更新"+result+"行",Toast.LENGTH_LONG).show(); } }); findViewById(R.id.Button3).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Dao dao = new Dao(context); dao.query("向++"); dao.query("周++"); Toast.makeText(MainActivity.this,"GG",Toast.LENGTH_LONG).show(); } }); }}
阅读全文
0 0
- 数据库入门---增删改查
- 数据库入门之增删改查
- 数据库入门(增删查改)
- 数据库-增删改查
- 数据库增删改查
- 数据库增删改查
- 数据库增删改查
- 数据库增删改查
- 数据库增删改查
- 数据库增删改查
- 数据库增删查改
- 数据库增删改查
- 数据库增删改查
- 数据库增删改查!
- 数据库-增删改查
- 数据库 增删改查
- 数据库增删改查
- mongo入门--java对mongo数据库的增删改查
- Qt国际化实时切换
- 使用IDEA创建Gradle项目整合SSM框架
- 创建ssm项目
- 方法
- tomcat启动https的配置
- 数据库入门---增删改查
- 【剑指offer】二叉树的深度
- 使用sqoop导入导出mysql、hdfs、hive出现的问题
- wx-charts 微信小程序图表插件
- 初入java
- STC15系列读取DS18B20温度传感器串口显示代码
- Cow Exhibition (01 背包变形)
- 软考网络工程师上午考试知识点总结整理
- 计算机视觉(图像分类、检测、分割)数据集和比赛