SQL は初めてです。Sqliteman ブラウザーを使用して作成した mac アドレスのデータベースがあります。これは、「access_points」という名前のテーブルが 1 つだけある単純なデータベースです。macad.db という名前のデータベースは私の資産フォルダーにあります。edittextview からテーブル access_points に新しい MAC アドレスを挿入するアプリケーションがあります。私のDBアダプタには次の初期化があります:
public static String DATABASE_NAME="macad.db";
public static String DATABASE_PATH= "/data/data/com.example.myproject/databases/";
public static String LOCAL_DATABASE_NAME="macad.db";
私のテーブル(access_points)の初期化の値はどうあるべきですか。疑問符をあなたの答えに置き換えてください。
public static String DATABASE_TABLE= ????;
さらに、DB アダプターの insertmac メソッドを以下に示します。assets フォルダーに保存されている macad.db のテーブル「access_points」を参照するにはどうすればよいですか? 以下に示す db.insert() ステートメントの疑問符を回答に置き換えてください。
public long insertmac(String mac)
{
ContentValues macValue = new ContentValues();
macValue.put("macaddress1", macaddress1);
return db.insert(????, null, macValue);
}
また、mac-address が access_points テーブルに既に存在する場合、テーブルに再度配置されないようにする方法はありますか?? 解決策を事前に感謝します。私の DBAdapter クラスは次のとおりです。
public class DbAdapternew {
private static final String TAG = DbAdapter.class.getName();
//private DatabaseHelper mDbHelper;
public static SQLiteDatabase myDb;
public static String DATABASE_NAME="macad.db";
public static String DATABASE_TABLE="access_points";
public static String DATABASE_PATH= "/data/data/com.example.myproject/databases/";
public static String LOCAL_DATABASE_NAME="macad.db";
private static final int DATABASE_VERSION = 2;
private static Context myContext;
public static final String KEY_MAC = "mac_add"; // column 1
public static final String KEY_floor = "floor_id"; // column 2
public static final String KEY_build= "building_id"; //column 3
public static final String KEY_zone = "zone_id"; // column 4
private DatabaseHelper DBHelper;
private static class DatabaseHelper extends SQLiteOpenHelper {
Context helperContext;
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
helperContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database!!!!!");
onCreate(db);
}
public void createDataBase() throws IOException {
Log.i(TAG,"DB NAME : "+DATABASE_NAME);
Log.i(TAG,"DB PATH : "+DATABASE_PATH);
Log.i(TAG,"LOCAL DB NAME : "+LOCAL_DATABASE_NAME);
boolean dbExist = checkDataBase();
if (dbExist) {
} else {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
//throw new Error("Error copying database");
}
}
}
public SQLiteDatabase getDatabase() {
String myPath = DATABASE_PATH + DATABASE_NAME;
return SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DATABASE_PATH + DATABASE_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException {
Log.i(TAG,"LOCAL DB NAME : "+LOCAL_DATABASE_NAME);
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(LOCAL_DATABASE_NAME);
// Path to the just created empty db
String outFileName = DATABASE_PATH + DATABASE_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 = DATABASE_PATH + DATABASE_NAME;
myDb = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if (myDb != null)
myDb.close();
super.close();
}
}
public DbAdapternew(Context ctx) {
this.myContext = ctx;
DBHelper = new DatabaseHelper(ctx);
}
public DbAdapternew open() throws SQLException {
myDb = DBHelper.getWritableDatabase();
return this;
}
public void close() {
DBHelper.close();
}
public Cursor fetchAllData(String table) {
try {
myDb.rawQuery("SELECT * FROM " + table, null);
Log.i(TAG, "Row Collected");
} catch (SQLiteException e) {
myDb.execSQL("CREATE TABLE IF NOT EXISTS " + table
+ " (mac_add VARCHAR)");
Log.i(TAG, "TABLE Not Found");
}
return myDb.rawQuery("SELECT * FROM " + table, null);
}
public Cursor fetchData(String sqlQuery) {
try {
myDb.rawQuery(sqlQuery, null);
Log.i(TAG, "Query Executed");
} catch (SQLiteException e) {
Log.i(TAG, e.toString());
}
return myDb.rawQuery(sqlQuery, null);
}
public void executeQuery(String sql) {
myDb.execSQL(sql);
}
public long insertrow(String editmac)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_MAC, editmac.toString()); //for column 1 in macad.db
initialValues.put(KEY_floor, "uf"); //for column 2
initialValues.put(KEY_build, "ub"); //for column 3
initialValues.put(KEY_zone, "uz"); //for column 4
return myDb.insert(DATABASE_TABLE, null, initialValues);
}
}
私のアクティビティでは、更新ボタンを押すと、edittextview に入力された MAC アドレスが macad.db に入れられます。このコードは次のとおりです。
updzonedb.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
db.open();
try
{
db.insertrow(editmac.getText().toString());
}
catch (Exception ex)
{
}
db.close();
}
});