Person 类:
package com.xh.tx.bean;
public class Person
{
private Integer _id;
private String name;
private Integer age;
public Integer get_id() {
return _id;
}
public void set_id(Integer _id) {
this._id = _id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(Integer _id, String name, Integer age) {
super();
this._id = _id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [_id=" + _id + ", name=" + name + ", age=" + age + "]";
}
}
MySQLLiteHelper:
package com.xh.tx.utils;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MySQLLiteHelper extends SQLiteOpenHelper
{
/**
* 函数的作用:构造方法用来创建工具类
* param1: context 上下文环境
* param2: name 数据库的名称
* param3: factory 游标对象Cursor
* parma4: varsion 数据库的版本 数据库的版本不能从0开始
* @param context
*/
public MySQLLiteHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, "test.db", null, 2);
}
/**
* 用来创建数据库
*/
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table person (_id integer primary key, name varchar(20), age integer);";
db.execSQL(sql);// 真正执行数据的创建
}
//数据库版本更新
//版本需要发生变化,否则不会执行
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d("============", oldVersion + " : " + newVersion);
if(oldVersion == 1)
{
String sql = "alter table person add balance integer;";
db.execSQL(sql);
}
}
//每次打开数据库的时候调用
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
}
}
PersonDao :
package com.xh.tx.dao;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import com.xh.tx.bean.Person;
import com.xh.tx.utils.MySQLLiteHelper;
public class PersonDao
{
private MySQLLiteHelper helper;
public PersonDao(Context context)
{
helper = new MySQLLiteHelper(context, null, null, -1);
}
/**
* 对数据库进行添加操作
* sql:insert into person(name,age) values('sz',20);\
*缺陷:
* 1. 传递参数很麻烦
* 2. 不能有返回值
*/
public void savePerson(Person p)
{
String sql = "insert into person(name,age) values(?,?);";
SQLiteDatabase db = helper.getWritableDatabase();
//db代表的是一个数据库的一个连接
if(db.isOpen())//判断数据库是否打开
{
db.execSQL("delete from person;");//添加前删除表里面所有的数据
List list = new ArrayList();
for(int i=0; i < 30; i++)
{
list.add(new Person(i,"zs" + i,i));
}
for(Person person : list)
{
db.execSQL(sql, new Object[]{person.getName(),person.getAge()}); //向数据库里面去添加一行记录
}
db.close(); //记住一定要关闭数据库的连接
}
}
/**
* 删除一个对象
* sql:delete from person where _id = 1;
*/
public void deletePerson(Integer id)
{
String sql = "delete from person where _id = ?;";
SQLiteDatabase db = helper.getWritableDatabase();
if(db.isOpen())
{
db.execSQL(sql, new Integer[]{id});
db.close();
}
}
/**
* 修改一个对象
* sql:update person set name ='cccc' where _id=1;
*/
public void updatePerson(Person p)
{
String sql = "update person set name =? where _id=?;";
SQLiteDatabase db = helper.getWritableDatabase();
if(db.isOpen())
{
db.execSQL(sql, new Object[]{p.getName(),p.get_id()});
db.close();
}
}
/**
* 查询所有
*/
public List queryPerson()
{
String sql = "select _id,name,age from person;";
SQLiteDatabase db = helper.getReadableDatabase();
List list = null;
if(db.isOpen())
{
Cursor cursor = db.rawQuery(sql, null); //查询
//cursor.getCount() 查看执行sql以后返回的结果集的个数
if(null != cursor && cursor.getCount() > 0)
{
list = new ArrayList();
while(cursor.moveToNext())
{
Integer id = cursor.getInt(0);
String name = cursor.getString(1);
Integer age = cursor.getInt(2);
list.add(new Person(id,name,age));
}
cursor.close();
db.close();
}
}
return list;
}
//Transation 事物
public void Transation()
{
String sql1 ="update person set balance = balance - 1000 where _id = 1";
String sql2 ="update person set balance = balance + 1000 where _id = 2";
SQLiteDatabase db = helper.getWritableDatabase();
if(db.isOpen())
{
//开启一个事物
db.beginTransaction();
try {
db.execSQL(sql1);
int i = 10/0;
db.execSQL(sql2);
db.setTransactionSuccessful(); // 代表代码运行到这一行的时候是符合业务需求的,设置为事物成功
}
finally{
db.endTransaction(); //事物的结束,如果没有设置成功,那么会回滚你所做的任何操作,如果设置了则提交
}
db.close();
}
}
}
MainActivity:
package com.example.baseadapter;
import java.util.List;
import com.xh.tx.bean.Person;
import com.xh.tx.dao.PersonDao;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
List list = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PersonDao dao = new PersonDao(this);
dao.savePerson(null);
list = dao.queryPerson();
ListView listview = (ListView) findViewById(R.id.list_view);
listview.setAdapter(new MyAdapter());
}
class MyAdapter extends BaseAdapter
{
@Override
public int getCount() {
return list.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) {
TextView textview = null;
if(null != convertView)
{
textview = (TextView) convertView;
}else
{
textview = new TextView(MainActivity.this);
}
textview.setText(list.get(position).toString());
return textview;
}
}
}
PersonDao1 :
package com.xh.tx.dao;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.xh.tx.bean.Person;
import com.xh.tx.utils.MySQLLiteHelper;
public class PersonDao1
{
private static final String TAG = "PersonDao1";
private MySQLLiteHelper helper;
public PersonDao1(Context context)
{
helper = new MySQLLiteHelper(context, null, null, 1);
}
public void savePerson(Person p)
{
SQLiteDatabase db = helper.getWritableDatabase();
if(db.isOpen())
{
//nullColumnHack 如果数据库里面的name子都设计的时候不允许为空,但是你传递过来的参数是空
// 如果不设置这个nullColumnHack参数那么就会报错
// 如果你设置nullColumnHack这个参数的值为name那么不会报错
ContentValues values = new ContentValues();
values.put("name", p.getName());
values.put("age", p.getAge());
Long id = db.insert("person", null, values);
Log.d(TAG, "================:" + id);
db.close();
}
}
public void deletePerson(Integer id)
{
SQLiteDatabase db = helper.getWritableDatabase();
if(db.isOpen())
{
//select * from person where id=? and name = ?;
String whereClause = "_id=?";
String[] whereArgs = new String[]{String.valueOf(id)};
int _id = db.delete("person", whereClause, whereArgs);
Log.d(TAG, "================:" + _id);
db.close();
}
}
public void updatePerson(Person p)
{
SQLiteDatabase db = helper.getWritableDatabase();
if(db.isOpen())
{
ContentValues values = new ContentValues();
values.put("name", "xintx");
values.put("age", "2");
String whereClause = "_id=?";
String[] whereArgs = new String[]{String.valueOf(p.get_id())};
db.update("peson", values, whereClause, whereArgs);
db.close();
}
}
public void queryItem(Integer id)
{
SQLiteDatabase db = helper.getWritableDatabase();
if(db.isOpen())
{
String[] columns = new String[]{"_id","name","age"};
String selection = "_id=?";
String[] selectionArgs = new String[]{String.valueOf(id)};
String groupBy = null; //按什么什么分组
String having = null; //如果select里面包含了组函数的时候,不能用where去查询 就只有用having
String orderBy = null; //按什么排序 order by id desc;
Cursor cursor = db.query("person", columns, selection, selectionArgs, groupBy, having, orderBy);
if(null != cursor && cursor.moveToFirst())
{
Integer _id = cursor.getInt(0);
String name = cursor.getString(1);
Integer age = cursor.getInt(2);
Log.d(TAG, "_id=" + _id + " name=" + name + " age = " + age);
db.close();
}
}
}
}
MainActivity1 :
package com.example.baseadapter;
import java.util.List;
import com.xh.tx.bean.Person;
import com.xh.tx.dao.PersonDao;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity1 extends Activity {
List list = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PersonDao dao = new PersonDao(this);
//dao.savePerson(null);
list = dao.queryPerson();
ListView listview = (ListView) findViewById(R.id.list_view);
listview.setAdapter(new MyAdapter());
}
class MyAdapter extends BaseAdapter
{
@Override
public int getCount() {
return list.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) {
//布局转换器 作用就是讲一个布局转换为一个对象
LayoutInflater flater = MainActivity1.this.getLayoutInflater();
View view = flater.inflate(R.layout.list_item, null); //真正将一个布局文件转为一个对象
//在一个特定的对象上去查找一个ID所对应的组件
TextView text_name = (TextView) view.findViewById(R.id.list_view_name);
TextView text_age = (TextView) view.findViewById(R.id.list_view_age);
Person person = list.get(position);
text_name.setText(person.getName());
//text_age.setText(person.getAge());
text_age.setText(String.valueOf(person.getAge()));
return view;
}
}
}