データベースを更新するアプリケーションを作成しています。以下のデータベースとの列の変更は私のコードです。列をチェックしてからデータベースに挿入するかどうかを確認したいだけです。データベース テーブル名「uss_school」を更新したいのですが、8 つの列があり、すべてがテキスト フィールドです。そのテーブルにダミー データを追加する方法をテストするために電話してください。テーブルを削除して再度ファイルしたくない データベースに列が存在しない場合にのみ挿入したい テーブルを更新する方法?? スクリーンショットを参照してくださいhttp://imgur.com/QkKk1r8以下は select のクエリです テーブル「uss_school」の tp update database が必要です plz put 1 row with dummy data
public class DataBase extends SQLiteOpenHelper{
//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.schoollunchapp/databases/";
private static String DB_NAME = "YummiSlice.sqlite";
private SQLiteDatabase myDataBase;
private SQLiteDatabase myData;
private final Context myContext;
public DataBase(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
/**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
CopyFiles();
}
}
private void CopyFiles()
{
try
{
InputStream is = myContext.getAssets().open(DB_NAME);
File outfile = new File(DB_PATH,DB_NAME);
outfile.getParentFile().mkdirs();
outfile.createNewFile();
if (is == null)
throw new RuntimeException("stream is null");
else
{
FileOutputStream out = new FileOutputStream(outfile);
// BufferedOutputStream out = new BufferedOutputStream( new
FileOutputStream(outfile));
byte buf[] = new byte[128];
do {
int numread = is.read(buf);
if (numread <= 0)
break;
out.write(buf, 0, numread);
} while (true);
is.close();
out.close();
}
//AssetFileDescriptor af = am.openFd("world_treasure_hunter_deluxe.apk");
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
/**
* Check if the database already exist to avoid re-copying the file each time you open the
application.
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transfering bytestream.
* */
@SuppressWarnings("unused")
private void copyDataBase() throws IOException{
//Open your local db as the input stream
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[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
//---retrieve records---
public Cursor selectQuery(String query) throws SQLException
{
String myPath = DB_PATH + DB_NAME;
myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
Cursor mCursor =myData.rawQuery(query, null);
mCursor.moveToFirst();
myData.close();
return mCursor;
}
////////// For Insert And Update Data ////////
public void insert_update(String query) throws SQLException
{
String myPath = DB_PATH + DB_NAME;
myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
myData.execSQL(query);
myData.close();
}
}
DataBase db;
public class MealGroups extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mealsgroups);
db = new DataBase(getBaseContext());
try {
db.createDataBase();
} catch (IOException e1) {
e1.printStackTrace();
}
mma = new mealgroupscreenadapter(this);
new getDataTask().execute();
listmealsgroups.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
Intent iMenuList = new Intent(MealGroups.this, MenuGroup.class);
iMenuList.putExtra("meal_groupname",
Category_name.get(position));
iMenuList.putExtra("Meal_groupid", Category_ID.get(position));
iMenuList.putExtra("id", schedule_id);
iMenuList.putExtra("title", semestertitle);
iMenuList.putExtra("startdate", start_date);
iMenuList.putExtra("enddate", end_date);
iMenuList.putExtra("SchoolLevelId", SchoolLevelId);
iMenuList.putExtra("School_ID", School_ID);
iMenuList.putExtra("School_name", School_name);
startActivity(iMenuList);
}
});
}
public class getDataTask extends AsyncTask<Void, String, String> {
getDataTask() {
if (!prgLoading.isShown()) {
prgLoading.setVisibility(0);
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// TODO Auto-generated method stub
}
@Override
protected String doInBackground(Void... arg0) {
// TODO Auto-generated method stub
displayData();
return _response;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
prgLoading.setVisibility(8);
// pleaseselect.setVisibility(0);
if ((Category_ID.size() > 0)) {
listmealsgroups.setVisibility(0);
listmealsgroups.setAdapter(mma);
mma.notifyDataSetChanged();
} else {
listmealsgroups.setVisibility(View.GONE);
NoMenuAvailable.setVisibility(View.VISIBLE);
NoMenuAvailable
.setText("No Meal Group Available for this School.");
}
}
}
private void displayData() {
Cursor mCursor2 = db
.selectQuery("SELECT DISTINCT WHERE s.school_id ='"
+ School_ID + "'");
Category_ID.clear();
Category_name.clear();
if (mCursor2.moveToFirst()) {
do {
Category_ID.add(mCursor2.getString(mCursor2
.getColumnIndex("meal_group_id")));
Category_name.add(mCursor2.getString(mCursor2
.getColumnIndex("title")));
} while (mCursor2.moveToNext());
}
mCursor2.close();
}
}