import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Created by chen on 2015/12/16.
*/
public class AddressDAO extends SQLiteOpenHelper{
private SQLiteDatabase db;
public AddressDAO(Context context)
{
super(context,"address_db",null,1);
}
//建立数据库
@Override
public void onCreate(SQLiteDatabase db)
{
Log.e("onCreate", "create");
String sql = "CREATE TABLE IF NOT EXISTS people(id integer primary key autoincrement,name text,phone text,workplace text,gender integer);";
db.execSQL(sql);
}
//当数据库升级的时候,Android系统会自动调用onUpgrade方法
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
String sql = "DROP TABLE IF EXISTS address_db";
db.execSQL(sql);
onCreate(db);
}
//关闭数据库服务
public void closeDB(SQLiteDatabase db)
{
if(db!=null)
{
db.close();
}
}
//插入数据
public void add(peopleBean people)
{
/* String name = people.getName();
String phone = people.getPhone();
String workplace = people.getWorkplace();
String gender = people.getGender()==0?"男":"女";*/
String sql = "insert into people values(null,'"+people.getName()+"','"+people.getPhone()+"','"+people.getWorkplace()+"','"+people.getGender()+"')";
//String sql = "insert into people values(null,'张亚茹','13716762131','北京','0');";
//获取SQLiteDatabase对象实例
db = this.getWritableDatabase();
db.execSQL(sql);
}
//查询数据:返回的是一个Cursor对象
public Cursor query()
{
String sql = "select * from people";
//获取SQLiteDatabase对象实例
db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(sql, null);
return cursor;
}
}