我正在开发一个应用程序,它可以通过按用户在编辑文本中输入数据库名称来创建多个数据库,并在列表视图中显示。我的问题:创建数据库后,我不能添加多个数据库,直到关闭应用程序并重新打开它。感谢您的帮助!
public static final File sdcard = new File(Environment.getExternalStorageDirectory() + "/memoryplus");
public static final File DB_NAME = new File(sdcard.getAbsolutePath() + File.separator + MainActivity.dbName + ".db");
Button btnAdd = (Button)dlc.findViewById(R.id.button);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbName=ed.getText().toString();
myDb=new MyDatabase(getApplicationContext());
boolean result= myDb.insertData(null,null);
FilesInFolder.clear();
Adapter.clear();
RefreshListView();
ed.setText("");
myDb.close();
dlc.dismiss();
}
});
答案 0 :(得分:1)
您可以有多个数据库,并且可以同时打开它们。
下面是一个示例,该示例创建(并打开)20个数据库,然后在其中列出表(1个表,每个表中都添加了相同的表)。
它使用 Database Helper (即SQLiteOpenHelper类的子类)创建/打开10,而不使用 DatbaseHelper 创建/打开10。
因此,它具有20个并发打开的数据库。
DBHelper.java 数据库帮助器:-
public class DBHelper extends SQLiteOpenHelper {
public static final int DBVERSION = 1;
public static final String TB_MYTABLE = "mytable";
public static final String MYTABLE_COL_ID = BaseColumns._ID;
public static final String MYTABLE_COL_NAME = "_name";
public static final String MYTABLE_COL_EMAIL = "_email";
public static final String MYTABLE_COL_PASSWORD = "_password";
static final String crtsql = "CREATE TABLE IF NOT EXISTS " + TB_MYTABLE + "(" +
MYTABLE_COL_ID + " INTEGER PRIMARY KEY, " +
MYTABLE_COL_NAME + " TEXT, " +
MYTABLE_COL_EMAIL + " TEXT UNIQUE, " +
MYTABLE_COL_PASSWORD + " TEXT " +
")";
SQLiteDatabase mDB;
public DBHelper(Context context, String name) {
super(context, name, null, DBVERSION);
mDB = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(crtsql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
}
public void logTables() {
Cursor csr = mDB.query("sqlite_master",null,null,null,null,null,null);
while (csr.moveToNext()) {
Log.d("DBHLPRTABLES",csr.getString(csr.getColumnIndex("name")));
}
csr.close();
}
}
MainActivity.java :调用/使用活动:-
public class MainActivity extends AppCompatActivity {
ArrayList<SQLiteDatabase> mDatabaseList;
ArrayList<DBHelper> mDBHelperList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDatabaseList = new ArrayList<>();
mDBHelperList = new ArrayList<>();
for (int i=0;i < 10; i++) {
mDatabaseList.add(createSQLiteDatabase("mydb" + String.valueOf(i)));
mDBHelperList.add(new DBHelper(this, "dbmine" + String.valueOf(i)));
}
for (SQLiteDatabase db: mDatabaseList) {
Cursor csr = db.query("sqlite_master",null,null,null,null,null,null);
while (csr.moveToNext()) {
Log.d("TABLES",csr.getString(csr.getColumnIndex("name")));
}
}
for (DBHelper dbhlpr: mDBHelperList) {
dbhlpr.logTables();
}
}
private SQLiteDatabase createSQLiteDatabase(String dbname) {
File dbpath = getDatabasePath(dbname);
File dir = new File(dbpath.getParent());
if (!dir.exists()) {
dir.mkdirs();
}
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbpath,null);
db.execSQL("CREATE TABLE IF NOT EXISTS mytable (ID INTEGER PRIMARY KEY, mycolumn TEXT)");
return db;
}
}
这将循环10次。对于每个循环
mDB = this.getWritableDatabase();
而有效地打开或创建了数据库),添加了 DBHelper 实例到 mDBHelperList ArrayList。然后循环遍历 mDatabaseList (10个数据库),对每个列出了 sqlite_master 表中名称的数据库运行查询。
logTables
方法)执行的操作大致相同。09-19 22:06:49.503 1820-1820/? D/TABLES: android_metadata
mytable
android_metadata
mytable
android_metadata
mytable
09-19 22:06:49.507 1820-1820/? D/TABLES: android_metadata
mytable
android_metadata
mytable
android_metadata
mytable
android_metadata
mytable
android_metadata
mytable
android_metadata
mytable
android_metadata
mytable
09-19 22:06:49.507 1820-1820/? D/DBHLPRTABLES: android_metadata
mytable
sqlite_autoindex_mytable_1
09-19 22:06:49.511 1820-1820/? D/DBHLPRTABLES: android_metadata
mytable
sqlite_autoindex_mytable_1
android_metadata
mytable
sqlite_autoindex_mytable_1
android_metadata
mytable
sqlite_autoindex_mytable_1
android_metadata
mytable
sqlite_autoindex_mytable_1
android_metadata
mytable
sqlite_autoindex_mytable_1
android_metadata
mytable
sqlite_autoindex_mytable_1
android_metadata
mytable
sqlite_autoindex_mytable_1
android_metadata
mytable
sqlite_autoindex_mytable_1
09-19 22:06:49.519 1820-1820/? D/DBHLPRTABLES: android_metadata
mytable
sqlite_autoindex_mytable_1