Androidデータストレージ——sqlite方式一
6570 ワード
テストエンジニアリングを追加するには
データベースの作成: package com.example.yabushanandroid.service;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DbOpenHelper extends SQLiteOpenHelper {
private static String name = "mydb.db";//
private static int version = 1;//
public DbOpenHelper(Context context) {
super(context, name, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// : , , ,
String sqlString = "create table person(id integer primary key autoincrenent,"
+ "name varchar(64),address varchar(64))";
db.execSQL(sqlString);
}
/**
* 、
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// , version , onCreate ,
String upsql = "alter table person add sec varchar(8)";
db.execSQL(upsql);
}
}
データベース・インタフェースの操作: package com.example.yabushanandroid.service;
import java.util.List;
import java.util.Map;
import android.R.string;
public interface dbService {
public boolean addPerson(Object[] params);
public boolean deletePerson(Object[] params);
public boolean updatePerson(Object[] params);
public Map<String,String> ViewPerson(String[] selectionArgs);
public List<Map<String, String>> listViewMaps(String[] selectionArgs);
}
データベース・インタフェースの実装: package com.example.yabushanandroid.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.R.string;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class dbServiceImpl implements dbService {
private DbOpenHelper helper=null;
public dbServiceImpl(Context context) {
helper = new DbOpenHelper(context);
}
@Override
public boolean addPerson(Object[] params) {
boolean flag=false;
SQLiteDatabase database=null;
try {
String sqlString="insert into person(name,address) values(?,?)";
database=helper.getWritableDatabase();//
database.execSQL(sqlString, params);
flag=true;
} catch (Exception e) {
}finally{
if(database!=null){
database.close();
}
}
return flag;
}
@Override
public boolean deletePerson(Object[] params) {
boolean flag=false;
SQLiteDatabase database=null;
try {
String sql="delete from person where id=?";
database=helper.getWritableDatabase();
database.execSQL(sql,params);
flag=true;
} catch (Exception e) {
}finally{
database.close();
}
return flag;
}
@Override
public boolean updatePerson(Object[] params) {
boolean flag=false;
SQLiteDatabase database=null;
try {
String sql="update person set name = ?,address = ? where id=?";
database.execSQL(sql,params);
flag=true;
} catch (Exception e) {
}finally{
database.close();
}
return flag;
}
@Override
public Map<String, String> ViewPerson(String[] selectionArgs) {
Map<String, String> map=new HashMap<String, String>();
SQLiteDatabase database=null;
try {
String sqlString="select * from person where id = ? ";
database=helper.getReadableDatabase();
Cursor cursor=database.rawQuery(sqlString, selectionArgs);
//
int colums=cursor.getColumnCount();
while(cursor.moveToNext()){
for(int i=0;i<colums;i++){
String cols_name=cursor.getColumnName(i);
String cols_value=cursor.getString(cursor.getColumnIndex(cols_name));
if(cols_value==null){
cols_value="";
}
map.put(cols_name, cols_value);
}
}
} catch (Exception e) {
// TODO: handle exception
}finally{
if(database!=null){
database.close();
}
}
return map;
}
@Override
public List<Map<String, String>> listViewMaps(String[] selectionArgs) {
List<Map<String, String>> list=new ArrayList<Map<String,String>>();
String sqlString="select * from person ";
SQLiteDatabase database=null;
try {
database=helper.getReadableDatabase();
Cursor cursor=database.rawQuery(sqlString, selectionArgs);
int colums=cursor.getColumnCount();
while(cursor.moveToNext()){
Map<String, String> map=new HashMap<String, String>();
for(int i=0;i<colums;i++){
String cols_name=cursor.getColumnName(i);
String cols_value=cursor.getString(cursor.getColumnIndex(cols_name));
if(cols_value==null){
cols_value="";
}
map.put(cols_name, cols_value);
}
list.add(map);
}
} catch (Exception e) {
// TODO: handle exception
}finally{
if(database!=null){
database.close();
}
}
return null;
}
}
テスト: package com.example.yabushanandroid.service;
import java.util.List;
import java.util.Map;
import android.test.AndroidTestCase;
import android.util.Log;
public class dbTest extends AndroidTestCase{
public dbTest() {
// TODO Auto-generated constructor stub
}
public void createDb(){
DbOpenHelper dbOpenHelper=new DbOpenHelper(getContext());
// ,
dbOpenHelper.getReadableDatabase();
//dbOpenHelper.getWritableDatabase();
}
/**
*
*/
public void insertDB(){
dbService service=new dbServiceImpl(getContext());
Object[] params={" "," "};
boolean flag=service.addPerson(params);
System.out.println(flag);
}
/**
*
*/
public void deleteDB(){
dbService service=new dbServiceImpl(getContext());
Object[] params={1};
boolean flag=service.deletePerson(params);
System.out.println(">>>"+flag);
}
/**
*
*/
public void updateDB(){
dbService service=new dbServiceImpl(getContext());
Object[] params={" "," ","1"};
service.updatePerson(params);
}
/**
*
*/
public void ViewDB(){
dbService service=new dbServiceImpl(getContext());
String[] selectionArgs = {"1"};
Map<String, String> map = service.ViewPerson(selectionArgs);
Log.i("TEST", "--->>>"+map.toString());
}
/**
*
*/
public void ListDB(){
dbService service=new dbServiceImpl(getContext());
List<Map<String, String>> list=service.listViewMaps(null);
Log.i("TEST", "--->>>"+list.toString());
}
}
package com.example.yabushanandroid.service;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DbOpenHelper extends SQLiteOpenHelper {
private static String name = "mydb.db";//
private static int version = 1;//
public DbOpenHelper(Context context) {
super(context, name, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// : , , ,
String sqlString = "create table person(id integer primary key autoincrenent,"
+ "name varchar(64),address varchar(64))";
db.execSQL(sqlString);
}
/**
* 、
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// , version , onCreate ,
String upsql = "alter table person add sec varchar(8)";
db.execSQL(upsql);
}
}
データベース・インタフェースの操作: package com.example.yabushanandroid.service;
import java.util.List;
import java.util.Map;
import android.R.string;
public interface dbService {
public boolean addPerson(Object[] params);
public boolean deletePerson(Object[] params);
public boolean updatePerson(Object[] params);
public Map<String,String> ViewPerson(String[] selectionArgs);
public List<Map<String, String>> listViewMaps(String[] selectionArgs);
}
データベース・インタフェースの実装: package com.example.yabushanandroid.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.R.string;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class dbServiceImpl implements dbService {
private DbOpenHelper helper=null;
public dbServiceImpl(Context context) {
helper = new DbOpenHelper(context);
}
@Override
public boolean addPerson(Object[] params) {
boolean flag=false;
SQLiteDatabase database=null;
try {
String sqlString="insert into person(name,address) values(?,?)";
database=helper.getWritableDatabase();//
database.execSQL(sqlString, params);
flag=true;
} catch (Exception e) {
}finally{
if(database!=null){
database.close();
}
}
return flag;
}
@Override
public boolean deletePerson(Object[] params) {
boolean flag=false;
SQLiteDatabase database=null;
try {
String sql="delete from person where id=?";
database=helper.getWritableDatabase();
database.execSQL(sql,params);
flag=true;
} catch (Exception e) {
}finally{
database.close();
}
return flag;
}
@Override
public boolean updatePerson(Object[] params) {
boolean flag=false;
SQLiteDatabase database=null;
try {
String sql="update person set name = ?,address = ? where id=?";
database.execSQL(sql,params);
flag=true;
} catch (Exception e) {
}finally{
database.close();
}
return flag;
}
@Override
public Map<String, String> ViewPerson(String[] selectionArgs) {
Map<String, String> map=new HashMap<String, String>();
SQLiteDatabase database=null;
try {
String sqlString="select * from person where id = ? ";
database=helper.getReadableDatabase();
Cursor cursor=database.rawQuery(sqlString, selectionArgs);
//
int colums=cursor.getColumnCount();
while(cursor.moveToNext()){
for(int i=0;i<colums;i++){
String cols_name=cursor.getColumnName(i);
String cols_value=cursor.getString(cursor.getColumnIndex(cols_name));
if(cols_value==null){
cols_value="";
}
map.put(cols_name, cols_value);
}
}
} catch (Exception e) {
// TODO: handle exception
}finally{
if(database!=null){
database.close();
}
}
return map;
}
@Override
public List<Map<String, String>> listViewMaps(String[] selectionArgs) {
List<Map<String, String>> list=new ArrayList<Map<String,String>>();
String sqlString="select * from person ";
SQLiteDatabase database=null;
try {
database=helper.getReadableDatabase();
Cursor cursor=database.rawQuery(sqlString, selectionArgs);
int colums=cursor.getColumnCount();
while(cursor.moveToNext()){
Map<String, String> map=new HashMap<String, String>();
for(int i=0;i<colums;i++){
String cols_name=cursor.getColumnName(i);
String cols_value=cursor.getString(cursor.getColumnIndex(cols_name));
if(cols_value==null){
cols_value="";
}
map.put(cols_name, cols_value);
}
list.add(map);
}
} catch (Exception e) {
// TODO: handle exception
}finally{
if(database!=null){
database.close();
}
}
return null;
}
}
テスト: package com.example.yabushanandroid.service;
import java.util.List;
import java.util.Map;
import android.test.AndroidTestCase;
import android.util.Log;
public class dbTest extends AndroidTestCase{
public dbTest() {
// TODO Auto-generated constructor stub
}
public void createDb(){
DbOpenHelper dbOpenHelper=new DbOpenHelper(getContext());
// ,
dbOpenHelper.getReadableDatabase();
//dbOpenHelper.getWritableDatabase();
}
/**
*
*/
public void insertDB(){
dbService service=new dbServiceImpl(getContext());
Object[] params={" "," "};
boolean flag=service.addPerson(params);
System.out.println(flag);
}
/**
*
*/
public void deleteDB(){
dbService service=new dbServiceImpl(getContext());
Object[] params={1};
boolean flag=service.deletePerson(params);
System.out.println(">>>"+flag);
}
/**
*
*/
public void updateDB(){
dbService service=new dbServiceImpl(getContext());
Object[] params={" "," ","1"};
service.updatePerson(params);
}
/**
*
*/
public void ViewDB(){
dbService service=new dbServiceImpl(getContext());
String[] selectionArgs = {"1"};
Map<String, String> map = service.ViewPerson(selectionArgs);
Log.i("TEST", "--->>>"+map.toString());
}
/**
*
*/
public void ListDB(){
dbService service=new dbServiceImpl(getContext());
List<Map<String, String>> list=service.listViewMaps(null);
Log.i("TEST", "--->>>"+list.toString());
}
}
package com.example.yabushanandroid.service;
import java.util.List;
import java.util.Map;
import android.R.string;
public interface dbService {
public boolean addPerson(Object[] params);
public boolean deletePerson(Object[] params);
public boolean updatePerson(Object[] params);
public Map<String,String> ViewPerson(String[] selectionArgs);
public List<Map<String, String>> listViewMaps(String[] selectionArgs);
}
package com.example.yabushanandroid.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.R.string;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class dbServiceImpl implements dbService {
private DbOpenHelper helper=null;
public dbServiceImpl(Context context) {
helper = new DbOpenHelper(context);
}
@Override
public boolean addPerson(Object[] params) {
boolean flag=false;
SQLiteDatabase database=null;
try {
String sqlString="insert into person(name,address) values(?,?)";
database=helper.getWritableDatabase();//
database.execSQL(sqlString, params);
flag=true;
} catch (Exception e) {
}finally{
if(database!=null){
database.close();
}
}
return flag;
}
@Override
public boolean deletePerson(Object[] params) {
boolean flag=false;
SQLiteDatabase database=null;
try {
String sql="delete from person where id=?";
database=helper.getWritableDatabase();
database.execSQL(sql,params);
flag=true;
} catch (Exception e) {
}finally{
database.close();
}
return flag;
}
@Override
public boolean updatePerson(Object[] params) {
boolean flag=false;
SQLiteDatabase database=null;
try {
String sql="update person set name = ?,address = ? where id=?";
database.execSQL(sql,params);
flag=true;
} catch (Exception e) {
}finally{
database.close();
}
return flag;
}
@Override
public Map<String, String> ViewPerson(String[] selectionArgs) {
Map<String, String> map=new HashMap<String, String>();
SQLiteDatabase database=null;
try {
String sqlString="select * from person where id = ? ";
database=helper.getReadableDatabase();
Cursor cursor=database.rawQuery(sqlString, selectionArgs);
//
int colums=cursor.getColumnCount();
while(cursor.moveToNext()){
for(int i=0;i<colums;i++){
String cols_name=cursor.getColumnName(i);
String cols_value=cursor.getString(cursor.getColumnIndex(cols_name));
if(cols_value==null){
cols_value="";
}
map.put(cols_name, cols_value);
}
}
} catch (Exception e) {
// TODO: handle exception
}finally{
if(database!=null){
database.close();
}
}
return map;
}
@Override
public List<Map<String, String>> listViewMaps(String[] selectionArgs) {
List<Map<String, String>> list=new ArrayList<Map<String,String>>();
String sqlString="select * from person ";
SQLiteDatabase database=null;
try {
database=helper.getReadableDatabase();
Cursor cursor=database.rawQuery(sqlString, selectionArgs);
int colums=cursor.getColumnCount();
while(cursor.moveToNext()){
Map<String, String> map=new HashMap<String, String>();
for(int i=0;i<colums;i++){
String cols_name=cursor.getColumnName(i);
String cols_value=cursor.getString(cursor.getColumnIndex(cols_name));
if(cols_value==null){
cols_value="";
}
map.put(cols_name, cols_value);
}
list.add(map);
}
} catch (Exception e) {
// TODO: handle exception
}finally{
if(database!=null){
database.close();
}
}
return null;
}
}
テスト: package com.example.yabushanandroid.service;
import java.util.List;
import java.util.Map;
import android.test.AndroidTestCase;
import android.util.Log;
public class dbTest extends AndroidTestCase{
public dbTest() {
// TODO Auto-generated constructor stub
}
public void createDb(){
DbOpenHelper dbOpenHelper=new DbOpenHelper(getContext());
// ,
dbOpenHelper.getReadableDatabase();
//dbOpenHelper.getWritableDatabase();
}
/**
*
*/
public void insertDB(){
dbService service=new dbServiceImpl(getContext());
Object[] params={" "," "};
boolean flag=service.addPerson(params);
System.out.println(flag);
}
/**
*
*/
public void deleteDB(){
dbService service=new dbServiceImpl(getContext());
Object[] params={1};
boolean flag=service.deletePerson(params);
System.out.println(">>>"+flag);
}
/**
*
*/
public void updateDB(){
dbService service=new dbServiceImpl(getContext());
Object[] params={" "," ","1"};
service.updatePerson(params);
}
/**
*
*/
public void ViewDB(){
dbService service=new dbServiceImpl(getContext());
String[] selectionArgs = {"1"};
Map<String, String> map = service.ViewPerson(selectionArgs);
Log.i("TEST", "--->>>"+map.toString());
}
/**
*
*/
public void ListDB(){
dbService service=new dbServiceImpl(getContext());
List<Map<String, String>> list=service.listViewMaps(null);
Log.i("TEST", "--->>>"+list.toString());
}
}
package com.example.yabushanandroid.service;
import java.util.List;
import java.util.Map;
import android.test.AndroidTestCase;
import android.util.Log;
public class dbTest extends AndroidTestCase{
public dbTest() {
// TODO Auto-generated constructor stub
}
public void createDb(){
DbOpenHelper dbOpenHelper=new DbOpenHelper(getContext());
// ,
dbOpenHelper.getReadableDatabase();
//dbOpenHelper.getWritableDatabase();
}
/**
*
*/
public void insertDB(){
dbService service=new dbServiceImpl(getContext());
Object[] params={" "," "};
boolean flag=service.addPerson(params);
System.out.println(flag);
}
/**
*
*/
public void deleteDB(){
dbService service=new dbServiceImpl(getContext());
Object[] params={1};
boolean flag=service.deletePerson(params);
System.out.println(">>>"+flag);
}
/**
*
*/
public void updateDB(){
dbService service=new dbServiceImpl(getContext());
Object[] params={" "," ","1"};
service.updatePerson(params);
}
/**
*
*/
public void ViewDB(){
dbService service=new dbServiceImpl(getContext());
String[] selectionArgs = {"1"};
Map<String, String> map = service.ViewPerson(selectionArgs);
Log.i("TEST", "--->>>"+map.toString());
}
/**
*
*/
public void ListDB(){
dbService service=new dbServiceImpl(getContext());
List<Map<String, String>> list=service.listViewMaps(null);
Log.i("TEST", "--->>>"+list.toString());
}
}