My Android application correctly creates a database with a single table, but when a method is called to insert another row into the table, it fails and returns no error.
My goal is to have the method addOffice() add a row to my SQLite DB table Offices, then check to see if any rows exist, using the method ifExists().
(For ease-of-reading, I've only included relevant code.)
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private static String TAG = MainActivity.class.getSimpleName();
TextView testText = null;
DBHelper dbHelper = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testText = (TextView) findViewById(R.id.testText);
dbHelper = new DBHelper(this, null, null, 1);
// Creates a new Office object, setting items _id, name, and acro.
Office office = new Office(1, "testOffice", "TEST");
dbHelper.addOffice(office);
testText.setText(String.valueOf(dbHelper.ifExists("Offices")));
}
DBHelper.java:
public class DBHelper extends SQLiteOpenHelper{
public static final int DATABASE_VERSION = 1;
public static final String TAG = SQLiteOpenHelper.class.getSimpleName();
public static final String DATABASE_NAME = "emergency.db";
public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE Contacts( " +
"_id INTEGER PRIMARY_KEY, " +
"office_id INTEGER, " +
"name TEXT," +
"phone TEXT);";
db.execSQL(query);
query = "CREATE TABLE Offices( " +
"_id INTEGER PRIMARY_KEY, " +
"acro TEXT, " +
"name TEXT);";
db.execSQL(query);
db.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS Contacts;");
db.execSQL("DROP TABLE IF EXISTS Offices;");
onCreate(db);
}
public void addOffice (Office office){
ContentValues values = new ContentValues();
values.put("_id", office.get_id());
values.put("acro", office.get_acro());
values.put("name", office.get_name());
SQLiteDatabase db = getWritableDatabase();
db.insert("Offices", null, values);
db.close();
}
public boolean ifExists (String tableName) {
SQLiteDatabase db = getReadableDatabase();
String query = "SELECT * FROM " + tableName;
Cursor c = db.rawQuery(query, null);
if(c.getCount() <= 0){
c.close();
return false;
}
c.close();
return true;
}
public String dbToString() {
String result = "default";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM Offices";
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
while (!c.isAfterLast()) {
if (c.getString(c.getColumnIndex("_id")) != null) {
result += c.getString((c.getColumnIndex("_id"))) + "\n";
} else {
result += "_id was NULL";
}
}
c.close();
db.close();
return result;
}
}
So far, ifExists() has been consistently returning false to testText, and when directly returning data from the table using dbToString(), only "default" is returned.
I know that this should be relatively simple, but I've been stuck on this for longer than I should be. Any help is appreciated.
Aucun commentaire:
Enregistrer un commentaire