Android数据库的增删改查

原创 2016年06月02日 08:18:10

Bean包中

package com.bwie.www.bean;

import java.io.Serializable;

public class Goods implements Serializable{
	private int id;
	private int picture;
	private String name;
	private int price;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getPicture() {
		return picture;
	}
	public void setPicture(int picture) {
		this.picture = picture;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	public Goods(int id, int picture, String name, int price) {
		super();
		this.id = id;
		this.picture = picture;
		this.name = name;
		this.price = price;
	}
	public Goods() {
		super();
		// TODO Auto-generated constructor stub
	}
	
}	
在Dao包中

package com.bwie.www.dao;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.bwie.www.bean.Goods;
import com.bwie.www.db.MySqliteOpenHelper;

public class GoodsDao {

	private SQLiteDatabase db;

	public GoodsDao(Context context) {
		MySqliteOpenHelper helper=new MySqliteOpenHelper(context);
		db = helper.getWritableDatabase();
	}
	//查询
	public ArrayList<Goods> queryAll(){
		ArrayList<Goods> list=null;
		Cursor cursor = db.query("goods", null, null, null, null, null, null);
		while(cursor.moveToNext()){
			if(list==null){
				list=new ArrayList<Goods>();
			}
			Goods g=new Goods();
			g.setId(cursor.getInt(cursor.getColumnIndex("_id")));
			g.setPicture(cursor.getInt(cursor.getColumnIndex("picture")));
			g.setName(cursor.getString(cursor.getColumnIndex("name")));
			g.setPrice(cursor.getInt(cursor.getColumnIndex("price")));
			list.add(g);
		}
		return list;	
	}
	//模糊查询
	public ArrayList<Goods> query(String name){
		ArrayList<Goods> list=null;
		Cursor cursor = db.query("goods", null, "name like ?", new String[]{"%"+name+"%"}, null, null, null);
		while(cursor.moveToNext()){
			if(list==null){
				list=new ArrayList<Goods>();
			}
			Goods g=new Goods();
			g.setId(cursor.getInt(cursor.getColumnIndex("_id")));
			g.setPicture(cursor.getInt(cursor.getColumnIndex("picture")));
			g.setName(cursor.getString(cursor.getColumnIndex("name")));
			g.setPrice(cursor.getInt(cursor.getColumnIndex("price")));
			list.add(g);
		}
		return list;	
	}
	//删除
	public int delete(int id){
		int i = db.delete("goods", "_id=?", new String[]{""+id});
		return i;
	}
	//修改
	public int update(Goods g){
		ContentValues values=new ContentValues();
		values.put("name", g.getName());
		values.put("price", g.getPrice());
		int i = db.update("goods", values, "_id=?", new String[]{""+g.getId()});
		
		return i;
		
	}
}
创建数据库

package com.bwie.www.db;

import com.bwie.www.R;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class MySqliteOpenHelper extends SQLiteOpenHelper {

	public MySqliteOpenHelper(Context context) {
		super(context, "week2.db", null, 1);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		//创建数据库表
		db.execSQL("create table goods(_id integer primary key autoincrement,picture integer,name vaarchar(30),price integer)");
		//插入数据
		insertSql(db);
	}

	private void insertSql(SQLiteDatabase db) {
		for(int i=0;i<10;i++){
			int ii=(int) (Math.random()*100+1);
			ContentValues values=new ContentValues();
			values.put("picture", R.drawable.ic_launcher);
			values.put("name", "英雄.MP3"+"----"+i);
			values.put("price", ii);
			db.insert("goods", null, values);
			
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

}
在Activity中

package com.bwie.www.db;

import com.bwie.www.R;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class MySqliteOpenHelper extends SQLiteOpenHelper {

	public MySqliteOpenHelper(Context context) {
		super(context, "week2.db", null, 1);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		//创建数据库表
		db.execSQL("create table goods(_id integer primary key autoincrement,picture integer,name vaarchar(30),price integer)");
		//插入数据
		insertSql(db);
	}

	private void insertSql(SQLiteDatabase db) {
		for(int i=0;i<10;i++){
			int ii=(int) (Math.random()*100+1);
			ContentValues values=new ContentValues();
			values.put("picture", R.drawable.ic_launcher);
			values.put("name", "英雄.MP3"+"----"+i);
			values.put("price", ii);
			db.insert("goods", null, values);
			
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

}
在UpDateActivity中

package com.bwie.www;

import java.io.Serializable;

import com.bwie.www.bean.Goods;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class UpdateMainActivity extends Activity {

	private EditText et_name;
	private EditText et_price;
	private Button update;
	private Goods g;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_update_main);
		//获取意图对象
		Intent intent=getIntent();
		g = (Goods) intent.getSerializableExtra("good");
		//找控件
		et_name = (EditText) findViewById(R.id.et_name);
		et_price = (EditText) findViewById(R.id.et_price);
		update = (Button) findViewById(R.id.update);
		et_name.setText(g.getName());
		et_price.setText(g.getPrice()+"");
		//为修改添加监听
		update.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String name = et_name.getText().toString();
				String price = et_price.getText().toString();
				//将得到的值输入进数据中
				Goods goods=new Goods(g.getId(), R.drawable.ic_launcher, name, Integer.parseInt(price));
				Intent intent=new Intent(UpdateMainActivity.this,MainActivity.class);
				intent.putExtra("goods", goods);
				setResult(2,intent);
				finish();
			}
		});
	}
}






版权声明:本文为博主原创文章,未经博主允许不得转载。

Android开发(10)使用SQLiteDatabase操作SQLite数据库实现增删改查(源码)

Android提供了一个名为SQLiteDatabase的类,该类封装了一些操作数据库的API,使用该类可以完成对数据进行添加(Create)、查询(Retrieve)、更新(Update)和删除(D...

【Android】Sqlite数据库增删改查

  • 2015年07月28日 20:06
  • 1.39MB
  • 下载

android 对sqlite数据库的增删改查

android平台下数据库的一些概念 Sqlite是android平台的一款轻量级的、嵌入式的、关系型数据库产品,以下是一些主要概念。 1.Sqlite通过文件保存数据库,创建的数据库文件默认...

Android数据库的增删改查(方式2)

前边已经讲过了Android数据库的增删改查的一种方式,第一种方式存在不足之处就是没有返回值,需要我们手动写sql语句,有时我们容易写错sql语句,所以不利于我们在开发中使用,google工程师也考虑...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Android数据库的增删改查
举报原因:
原因补充:

(最多只允许输入30个字)