Android SQLite不插入或返回任何行
我的Android应用程序使用一个表正确地创建了一个数据库,但是当调用一个方法向表中插入另一行时,它会失败,并且不会返回任何错误 我的目标是让addOffice方法向我的SQLite DB table Offices添加一行,然后使用ifExists方法检查是否存在任何行 为了便于阅读,我只包含了相关代码 MainActivity.java: DBHelper.java: 到目前为止,ifExists始终将false返回给testText,当使用dbToString直接从表返回数据时,只返回默认值Android SQLite不插入或返回任何行,android,sqlite,Android,Sqlite,我的Android应用程序使用一个表正确地创建了一个数据库,但是当调用一个方法向表中插入另一行时,它会失败,并且不会返回任何错误 我的目标是让addOffice方法向我的SQLite DB table Offices添加一行,然后使用ifExists方法检查是否存在任何行 为了便于阅读,我只包含了相关代码 MainActivity.java: DBHelper.java: 到目前为止,ifExists始终将false返回给testText,当使用dbToString直接从表返回数据时,只返回默认
我知道这应该是相对简单的,但我已经在这上面停留了很长时间。感谢您的帮助。您为什么关闭db.close;在所有覆盖方法中?你不应该去。它应该只添加到onPause方法中
试试这个网址。这将对您有所帮助。您已经尝试过调试它了吗?是的,但在捕获并发送到Logcat或Toast时,我没有收到任何错误。每次更改数据库时,您都必须清除应用程序中的数据,无需调用db.close;在OnCreate方法中。请将其删除,然后重试一次。同时增加数据库版本。让我知道它是否解决了您的问题。一个只有一个表的数据库,那么为什么要在onUpgrade方法中删除一个不存在的表联系人?
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")));
}
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) {
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;
}
}