用谷歌封装号的api增删改查
2017-08-17 17:45
225 查看
Mainactive
public class MainActivity extends Activity {
private MyOpenHelper myOpenHelper;
private List<Person> lists;
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//[0]找到lv
lv = (ListView) findViewById(R.id.lv);
myOpenHelper = new MyOpenHelper(getApplicationContext());
//[1]定义一个集合用来存listview 要展示的数据
lists = new ArrayList<Person>();
//打开或者创建数据库 如果是第一次就是创建
// SQLiteDatabase sqLiteDatabase = myOpenHelper.getWritableDatabase();
//打开或者创建数据库 如果是第一次就是创建 如果磁盘满了 返回只读的
// SQLiteDatabase readableDatabase = myOpenHelper.getReadableDatabase();
}
//点击按钮增加一条记录
public void click1(View v){
//[1]获取数据库对象
SQLiteDatabase db = myOpenHelper.getWritableDatabase();
//[2]执行增加一条的sql语句
// db.execSQL("insert into info(name,phone) values(?,?)", new Object[]{"张三","1388888"});
/**
* table 表名
* ContentValues 内部封装了一个map key: 对应列的名字 value对应的值
*/
ContentValues values = new ContentValues();
values.put("name", "王五");
values.put("phone", "110");
//返回值代表插入新行的id
long insert = db.insert("info", null, values); //底层就在组拼sql语句
//[3]数据库用完需要关闭
db.close();
if (insert>0) {
Toast.makeText(getApplicationContext(), "添加成功", 1).show();
}else {
Toast.makeText(getApplicationContext(), "添加fail", 1).show();
}
}
//删除
public void click2(View v){
SQLiteDatabase db = myOpenHelper.getWritableDatabase();
// db.execSQL("delete from info where name=?", new Object[]{"张三"});
//返回值代表影响的行数
int delete = db.delete("info", "name=?", new String[]{"王五"});
db.close();
Toast.makeText(getApplicationContext(), "删除了"+delete+"行", 0).show();
}
//更新
public void click3(View v){
SQLiteDatabase db = myOpenHelper.getWritableDatabase();
// db.execSQL("update info set phone=? where name=? ", new Object[]{"138888888","张三"});
ContentValues values = new ContentValues();
values.put("phone", "114");
//代表更新了多少行
int update = db.update("info", values, "name=?", new String[]{"王五"});
db.close();
Toast.makeText(getApplicationContext(), "更新了"+update+"行", 0).show();
}
//查找
public void click4(View v){
SQLiteDatabase db = myOpenHelper.getReadableDatabase();
//columns 代表你要查询的列
//selection 根据什么查询phone
// Cursor cursor = db.query("info", new String[]{"name","phone"}, "name=?", new String[]{"王五"}, null, null, null);
Cursor cursor = db.query("info", null,null, null, null, null, null);
// Cursor cursor = db.rawQuery("select * from info", null);
if (cursor!= null&&cursor.getCount()>0) {
while(cursor.moveToNext()){
//columnIndex代表列的索引
String name = cursor.getString(1);
String phone = cursor.getString(2);
//把数据封装到javabean
Person person = new Person();
person.setName(name);
person.setPhone(phone);
//把javabena对象加入到集合
lists.add(person);
}
//设置数据适配器
lv.setAdapter(new MyAdapter());
}
}
//定义listview 的数据适配器
private class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return lists.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
//创建新的view 对象
view = View.inflate(getApplicationContext(), R.layout.item, null);
}else{
view = convertView;
}
//[找到控件用来显示数据]
TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
TextView tv_phone = (TextView) view.findViewById(R.id.tv_phone);
//如何显示数据
Person person = lists.get(position);
tv_name.setText(person.getName());
tv_phone.setText(person.getPhone());
return view;
}
}
}
MyOpenHelper.java
package com.itheima.sqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyOpenHelper extends SQLiteOpenHelper {
/**
*
* @param context 上下文
* name:数据库的名字
* fact
4000
ory 目的创建cursor对象
*
* version 数据库的版本 从1开始
*/
public MyOpenHelper(Context context) {
super(context, "itheima.db", null,4);
}
/**
* Called when the database is created for the first time.
* 当数据库第一次创建的时候调用
* 那么这个方法特别适合做表结构的初始化 创建表就是写sql语句
*/
@Override
public void onCreate(SQLiteDatabase db) {
//id 一般以_id
db.execSQL("create table info(_id integer primary key autoincrement,name varchar(20),phone varchar(20))");
}
/**
* Called when the database needs to be upgraded
* 当数据库版本升级的时候调用
*
* 这个方法适合做 表结构的更新
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("alter table info add phone varchar(20)");
}
}
person
package com.itheima.sqlite;
public class Person {
private String name;
private String phone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
public class MainActivity extends Activity {
private MyOpenHelper myOpenHelper;
private List<Person> lists;
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//[0]找到lv
lv = (ListView) findViewById(R.id.lv);
myOpenHelper = new MyOpenHelper(getApplicationContext());
//[1]定义一个集合用来存listview 要展示的数据
lists = new ArrayList<Person>();
//打开或者创建数据库 如果是第一次就是创建
// SQLiteDatabase sqLiteDatabase = myOpenHelper.getWritableDatabase();
//打开或者创建数据库 如果是第一次就是创建 如果磁盘满了 返回只读的
// SQLiteDatabase readableDatabase = myOpenHelper.getReadableDatabase();
}
//点击按钮增加一条记录
public void click1(View v){
//[1]获取数据库对象
SQLiteDatabase db = myOpenHelper.getWritableDatabase();
//[2]执行增加一条的sql语句
// db.execSQL("insert into info(name,phone) values(?,?)", new Object[]{"张三","1388888"});
/**
* table 表名
* ContentValues 内部封装了一个map key: 对应列的名字 value对应的值
*/
ContentValues values = new ContentValues();
values.put("name", "王五");
values.put("phone", "110");
//返回值代表插入新行的id
long insert = db.insert("info", null, values); //底层就在组拼sql语句
//[3]数据库用完需要关闭
db.close();
if (insert>0) {
Toast.makeText(getApplicationContext(), "添加成功", 1).show();
}else {
Toast.makeText(getApplicationContext(), "添加fail", 1).show();
}
}
//删除
public void click2(View v){
SQLiteDatabase db = myOpenHelper.getWritableDatabase();
// db.execSQL("delete from info where name=?", new Object[]{"张三"});
//返回值代表影响的行数
int delete = db.delete("info", "name=?", new String[]{"王五"});
db.close();
Toast.makeText(getApplicationContext(), "删除了"+delete+"行", 0).show();
}
//更新
public void click3(View v){
SQLiteDatabase db = myOpenHelper.getWritableDatabase();
// db.execSQL("update info set phone=? where name=? ", new Object[]{"138888888","张三"});
ContentValues values = new ContentValues();
values.put("phone", "114");
//代表更新了多少行
int update = db.update("info", values, "name=?", new String[]{"王五"});
db.close();
Toast.makeText(getApplicationContext(), "更新了"+update+"行", 0).show();
}
//查找
public void click4(View v){
SQLiteDatabase db = myOpenHelper.getReadableDatabase();
//columns 代表你要查询的列
//selection 根据什么查询phone
// Cursor cursor = db.query("info", new String[]{"name","phone"}, "name=?", new String[]{"王五"}, null, null, null);
Cursor cursor = db.query("info", null,null, null, null, null, null);
// Cursor cursor = db.rawQuery("select * from info", null);
if (cursor!= null&&cursor.getCount()>0) {
while(cursor.moveToNext()){
//columnIndex代表列的索引
String name = cursor.getString(1);
String phone = cursor.getString(2);
//把数据封装到javabean
Person person = new Person();
person.setName(name);
person.setPhone(phone);
//把javabena对象加入到集合
lists.add(person);
}
//设置数据适配器
lv.setAdapter(new MyAdapter());
}
}
//定义listview 的数据适配器
private class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return lists.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
//创建新的view 对象
view = View.inflate(getApplicationContext(), R.layout.item, null);
}else{
view = convertView;
}
//[找到控件用来显示数据]
TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
TextView tv_phone = (TextView) view.findViewById(R.id.tv_phone);
//如何显示数据
Person person = lists.get(position);
tv_name.setText(person.getName());
tv_phone.setText(person.getPhone());
return view;
}
}
}
MyOpenHelper.java
package com.itheima.sqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyOpenHelper extends SQLiteOpenHelper {
/**
*
* @param context 上下文
* name:数据库的名字
* fact
4000
ory 目的创建cursor对象
*
* version 数据库的版本 从1开始
*/
public MyOpenHelper(Context context) {
super(context, "itheima.db", null,4);
}
/**
* Called when the database is created for the first time.
* 当数据库第一次创建的时候调用
* 那么这个方法特别适合做表结构的初始化 创建表就是写sql语句
*/
@Override
public void onCreate(SQLiteDatabase db) {
//id 一般以_id
db.execSQL("create table info(_id integer primary key autoincrement,name varchar(20),phone varchar(20))");
}
/**
* Called when the database needs to be upgraded
* 当数据库版本升级的时候调用
*
* 这个方法适合做 表结构的更新
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("alter table info add phone varchar(20)");
}
}
person
package com.itheima.sqlite;
public class Person {
private String name;
private String phone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
相关文章推荐
- Android数据库二:使用谷歌封装好的api对数据库增删改查
- Android使用谷歌封装好的api增删查改
- 用谷歌封装好的API进行数据库增删查改(crud)
- Android(java)学习笔记193:利用谷歌API对数据库增删改查(推荐使用)
- hibernate基于泛型基础增删改查方法封装
- Entity Framework 增删改查基本操作(封装基类)
- 【动态】浏览器与比特币:谷歌、苹果等开始采用兼容加密货币的API
- HBase 使用 Java Api 实现对数据的增删改查
- appium笔记六:appium常用api二次封装
- Delphi-IOCP API代码的封装和流程分析
- ElasticSearch6.1的增删改查Rest API
- c#封装百度web服务geocoding api 、百度坐标转换示例
- C# 视频监控系列(11):H264播放器——封装API[HikPlayM4.dll](4)
- experiment : 封装 xxPagedLookasideList API
- Gson jar常用API的封装
- 百度音乐Api简单简单实用实例及封装
- 从Win32 API封装Thread类[2]
- Android对SQLite数据库进行封装使用反射来进行表的增删改查