我需要制作一个方法,该方法会将数据添加到数据库中,然后旋转器应该显示它。经我测试,显示效果很好,但是我不知道如何告诉数据库我只想创建一些数据。例如,我有一个名为ChestExercises的表,其中三列firstColumn,secondColumn,thirdColumn具有值:
firstColumn = "exerciseOne"
secondColumn = "exerciseTwo"
thirdColumn = "exerciseThree"
现在,我希望从程序中仅创建一次这些数据。正如我之前所写,我已经进行了一些“测试”,并且数据库一直在不断创建这些数据,我不想重复。所以..我怎样才能“告诉”我只想创建一次的数据库?
数据库
public databaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_2 + " TEXT," + COLUMN_3 + " TEXT," + COLUMN_4 + " TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean insertData() {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_2, "1");
contentValues.put(COLUMN_3, "2");
contentValues.put(COLUMN_4, "3");
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1) {
return false;
} else
return true;
}
public List<String> getAllLabels() {
List<String> labels = new ArrayList<String>();
String selectQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(1));
labels.add(cursor.getString(2));
labels.add(cursor.getString(3));
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return labels;
}
}
MainActivity
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinner);
databaseHelper db = new databaseHelper(getApplicationContext());
if (spinner == null){
db.insertData();
}else {
Toast.makeText(MainActivity.this,
"Your Message", Toast.LENGTH_LONG).show();
}
loadData();
}
public void loadData(){
databaseHelper db = new databaseHelper(getApplicationContext());
List<String> labels = db.getAllLabels();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, labels);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String label = adapterView.getItemAtPosition(i).toString();
Toast.makeText(adapterView.getContext(),"Selected: "+label,Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
答案 0 :(得分:0)
一种选择是定义表,以使3列合并形成 UNIQUE 约束,因此如果合并的列存在,将导致冲突。
例如
db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_2 + " TEXT," + COLUMN_3 + " TEXT," + COLUMN_4 + " TEXT, UNIQUE (" + COLUMN_2 + "," + COLUMN_3 + "," + COLUMN_4 + " ))");
在这种情况下,将不会插入相同的值组合(因为insert
方法有效地使用了INSERT或IGNORE,因此如果发生冲突,则会忽略INSERT)。
另一种方法可能是通过检查行数来检查表是否已包含任何数据。
您可以在DatabaseHelper类中拥有一个方法,例如:-
public boolean isTableEmpty(String tablename) {
return DatabaseUtils.queryNumEntries(this.getWritableDatabase(),tablename) < 1;
}
public boolean insertDataCheckingForDuplicate(String value1, String value2, String value3) {
SQLiteDatabase db = this.getWritableDatabase();
int rowcount = 0;
String whereclause = COLUMN_2 + "=? AND " + COLUMN_3 + "=? AND " + COLUMN_4 + "=?";
String[] whereargs = new String[]{value1,value2,value3};
Cursor csr = db.query(TABLE_NAME,null,whereclause,whereargs,null,null,null);
rowcount = csr.getCount();
csr.close();
if (rowcount > 0) {
return false;
}
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_2, value1);
contentValues.put(COLUMN_3, value2);
contentValues.put(COLUMN_4, value3);
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1) {
return false;
} else
return true;
}
使用这种方法,您可以提供预加载的数据库(即基本数据存在)。将数据库放置在资产文件夹中,并在首次打开数据库时将其从资产文件夹复制到相关位置。该过程将查看数据库是否存在,如果存在,则打开数据库,如果不存在,则复制数据库。
您可以使用SQLiteOpenAssethelper简化此过程,请参见android-sqlite-asset-helper。还有许多如何在StackOverflow上自行执行此操作的示例。