データベース
public class DatabaseconectionApiDemo extends SQLiteOpenHelper {
private final Context myContext;
ArrayList<String> labels1;
// StaticValues mStaticValues;
private static SQLiteDatabase db;
@SuppressLint("SdCardPath")
public final static String DB_PATH = "/data/data/com....youpackagename..../databases/";
public final static String DB_NAME = "DataBaseName.sqlite";
public DatabaseconectionApiDemo(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
// do nothing
} else {
// By calling this method and empty database will be created into the default system path
// of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private void copyDataBase() throws IOException{
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[2048];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
// database does't exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
public void openDataBase() throws SQLException {
try {
if (db != null) {
if (db.isOpen()) {
db.close();
}
}
} catch (Exception e) {
System.out.println("no database connected to close");
}
// Open the database
String myPath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if (db != null)
db.close();
super.close();
}
public int Update(String TableName, String cols[], String values[], String whereClause, String whereArgs[]) {
int id = 0;
openDataBase();
id = db.update(TableName, getContentValues(cols, values), whereClause, whereArgs);
close();
return id;
}
public int insertwithreturnid(String TableName, String cols[], String values[]) {
int id = 0;
openDataBase();
id = (int) db.insert(TableName, null, getContentValues(cols, values));
close();
return id;
}
public int delete(String TableName, String where, String whereArgs[]) {
int id = 0;
openDataBase();
id = db.delete(TableName, where, whereArgs);
close();
return id;
}
public DataHolder readFromTableName(String TableName, String cols[], String where[], String keyword) {
openDataBase();
DataHolder _holder = null;
Cursor c = null;
c = db.query(TableName, cols, where[0], null, null, null, null);
if (c != null) {
c.moveToFirst();
_holder = new DataHolder();
while (!c.isAfterLast()) {
int count = c.getColumnCount();
_holder.CreateRow();
for (int i = 0; i < count; i++) {
_holder.setmColoumn(c.getColumnName(i), c.getString(i));
}
_holder.AddRow();
c.moveToNext();
}
}
c.close();
close();
return _holder;
}
public void exeQuery(String sql) {
openDataBase();
try {
db.execSQL(sql);
} catch (Exception e) {
System.out.println(e.toString());
}
close();
}
public boolean InsertByValue(String table, ContentValues values) {
try {
openDataBase();
db.insertOrThrow(table, null, values);
Log.d("InsertByValue", "Data Insert");
return true;
} catch (Exception e) {
Log.d("InsertByValue", e.toString());
e.printStackTrace();
return false;
}
}
public DataHolder read(String sql) {
openDataBase();
DataHolder _holder = null;
Cursor c = db.rawQuery(sql, null);
if (c != null) {
c.moveToFirst();
_holder = new DataHolder();
while (!c.isAfterLast()) {
int count = c.getColumnCount();
_holder.CreateRow();
for (int i = 0; i < count; i++) {
_holder.setmColoumn(c.getColumnName(i), c.getString(i));
}
_holder.AddRow();
c.moveToNext();
}
}
c.close();
close();
return _holder;
}
public ContentValues getContentValues(String cols[], String values[]) {
ContentValues cv = new ContentValues();
for (int i = 0; i < cols.length; i++) {
cv.put(cols[i], values[i]);
}
return cv;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
に接続し、あなたがあなたの
public class DataHolder {
private ArrayList<LinkedHashMap<String, String>> mRow;
private LinkedHashMap<String, String> mColoumn;
public DataHolder() {
super();
mRow = new ArrayList<LinkedHashMap<String, String>>();
}
public void setmColoumn(String col, String value) {
this.mColoumn.put(col, value);
}
public ArrayList<LinkedHashMap<String, String>> getmRow() {
return mRow;
}
public void CreateRow() {
this.mColoumn = new LinkedHashMap<String, String>();
}
public void AddRow() {
this.mRow.add(this.mColoumn);
}
public void clear() {
this.mRow.clear();
}
public void add(LinkedHashMap<String, String> linkedHashMap) {
this.mRow.add(linkedHashMap);
}
}
からデータを読み取り、あなたが読むことができるfinnaly用データホルダークラスを追加する必要があるためconnetionのAPIクラスを作成するために必要なすべての最初のクエリを実行してデータベーステーブルのデータを挿入、更新、削除する
public class YourActivity extends Activity{
private DataHolder mDataHolder;
private DatabaseconectionApi mDatabaseconectionApi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
// initialize dataholder and connectionapi class like bellow
mDataHolder=new DataHolder();
mDatabaseconectionApi=new DatabaseconectionApi(YourActivity.this);
try {
mDatabaseconectionApi.createDataBase();
mDatabaseconectionApi.openDataBase();
}catch (IOException e) {
e.printStackTrace();
}
...........
//now you can perform insert,update,read and delete like these
mDataHolder = new DataHolder();
mDataHolder = mDatabaseconectionApi.read("SELECT * from table_name where "+table_id
+" = '"+TId+"'");
mDatabaseconectionApi.insertwithreturnid("table_name",
new String[]{"column_name"},
new String[]{"column_value"});
mDatabaseconectionApi.Update("table_name",
new String[]{"column_name"}, new String[]{"column_value"},
"column_id"+" = ?", new String[]{"column_id_value"});
mDatabaseconectionApi.delete("table_name", null, null);
// or
mDatabaseconectionApi.delete("table_name", "column_id"+" = ?", new String[]{"column_id_value"});