我正在尝试在运行时创建多个数据库。用户输入数据库名称,系统必须创建它。
这是我的代码
First.Java
public class First extends Activity implements OnClickListener {
Button btnOk;
EditText inputName;
DatabaseHandler db = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.db);
try {
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
initVar();
}
catch (Exception e) {
CommonUtils.postException(e, e.getMessage().toString());
}
}
private void initVar() {
try {
btnOk = (Button) findViewById(R.id.btnOk);
inputName = (EditText) findViewById(R.id.dbName);
btnOk.setOnClickListener(this);
}
catch (Exception e) {
CommonUtils.postException(e, e.getMessage().toString());
}
}
public void onClick(View v) {
if (v == btnOk ){
DBConstants.DATABASE_NAME=inputName.getText().toString().trim();
Toast.makeText(getApplicationContext(), DBConstants.DATABASE_NAME, Toast.LENGTH_LONG).show();
db = new DatabaseHandler(getApplicationContext());
db.getmyDb();
if(DatabaseHandler.doesDatabaseExist(getApplication(), inputName.getText().toString().trim())== true ){
Toast.makeText(getApplicationContext(), "DB created", Toast.LENGTH_LONG).show();
db.close();
}else{
Toast.makeText(getApplicationContext(), "DB Not created", Toast.LENGTH_LONG).show();
db.close();
}
}
}
}
DatabaseHandler.java
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static SQLiteDatabase myDb = null;
public DatabaseHandler(Context context) {
super(context, DBConstants.DATABASE_NAME, null, DATABASE_VERSION);
}
public SQLiteDatabase getmyDb() {
if (myDb == null)
myDb = this.getWritableDatabase();
return myDb;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onCreate(db);
}
public static boolean doesDatabaseExist(ContextWrapper context, String dbName) {
File dbFile=context.getDatabasePath(dbName);
return dbFile.exists();
}
按下OK按钮我需要创建一个数据库,其中包含用户在EditText中输入的文本
目前这项工作只有一次,如果用户第二次输入不同的文本,则不会创建数据库
答案 0 :(得分:0)
我怀疑它正在制作你的数据库。当你检查它是否已被创建时,你就错了。
来自onClick方法的这一行:
db.getmyDb();
将始终返回第一个数据库,因为它调用它:
public SQLiteDatabase getmyDb() {
if (myDb == null)
myDb = this.getWritableDatabase();
return myDb;
}
当第一次调用时,将myDb设置为第一个数据库,从那时起,始终返回该值。
您将需要设置数据库索引,并在数组中跟踪数据库。
答案 1 :(得分:0)
public void createDynamicDatabase(Context context,String tableName,ArrayList<String> title) {
Log.i("INSIDE createLoginDatabase() Method","*************creatLoginDatabase*********");
try {
int i;
String querryString;
myDataBase = context.openOrCreateDatabase("Db",Context.MODE_WORLD_WRITEABLE, null); //Opens database in writable mode.
//System.out.println("Table Name : "+tableName.get(0));
querryString = title.get(0)+" VARCHAR(30),";
Log.d("**createDynamicDatabase", "in oncreate");
for(i=1;i<title.size()-1;i++)
{
querryString += title.get(i);
querryString +=" VARCHAR(30)";
querryString +=",";
}
querryString+= title.get(i) +" VARCHAR(30)";
querryString = "CREATE TABLE IF NOT EXISTS " + tableName + "("+querryString+");";
System.out.println("Create Table Stmt : "+ querryString);
myDataBase.execSQL(querryString);
} catch (SQLException ex) {
Log.i("CreateDB Exception ",ex.getMessage());
}
}
public void insert(Context context,ArrayList<String> array_vals,ArrayList<String> title,String TABLE_NAME) {
Log.d("Inside Insert","Insertion starts for table name: "+TABLE_NAME);
myDataBase = context.openOrCreateDatabase("Db",Context.MODE_WORLD_WRITEABLE, null); //Opens database in writable mode.
String titleString=null;
String markString= null;
int i;
titleString = title.get(0)+",";
markString = "?,";
Log.d("**createDynamicDatabase", "in oncreate");
for(i=1;i<title.size()-1;i++)
{
titleString += title.get(i);
titleString +=",";
markString += "?,";
}
titleString+= title.get(i);
markString += "?";
//System.out.println("Title String: "+titleString);
//System.out.println("Mark String: "+markString);
INSERT="insert into "+ TABLE_NAME + "("+titleString+")"+ "values" +"("+markString+")";
System.out.println("Insert statement: "+INSERT);
//System.out.println("Array size iiiiii::: "+array_vals.size());
//this.insertStmt = this.myDataBase.compileStatement(INSERT);
int s=0;
while(s<array_vals.size()){
System.out.println("Size of array1"+array_vals.size());
//System.out.println("Size of array"+title.size());
int j=1;
this.insertStmt = this.myDataBase.compileStatement(INSERT);
for(int k =0;k< title.size();k++)
{
//System.out.println("Value of column "+title+" is "+array_vals.get(k+s));
//System.out.println("PRINT S:"+array_vals.get(k+s));
System.out.println("BindString: insertStmt.bindString("+j+","+ array_vals.get(k+s)+")");
insertStmt.bindString(j, array_vals.get(k+s));
j++;
}
s+=title.size();
}
insertStmt.executeInsert();
}
答案 2 :(得分:0)
在这一行:
private static SQLiteDatabase myDb = null;
尝试删除static修饰符 - 该字段用作实例对象。