0

我正在尝试在运行时创建多个数据库。用户输入数据库名称,系统必须创建它。

这是我的代码

一、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();
            }
        }
    }
}

数据库处理程序.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 中输入的文本创建一个数据库

目前这项工作只工作一次,如果用户第二次输入不同的文本,则不会创建数据库

4

3 回答 3

0

在这条线上:

private static SQLiteDatabase myDb = null;

尝试删除静态修饰符 - 该字段用作实例对象。

于 2015-04-16T17:22:26.967 回答
0

我怀疑它正在制作您的数据库。当您检查它是否已创建时,您只是误会了。

您的 onClick 方法中的这一行:

    db.getmyDb();

将始终返回第一个数据库,因为它调用了这个:

public SQLiteDatabase getmyDb() {
    if (myDb == null)
        myDb = this.getWritableDatabase();
    return myDb;
}

第一次调用时,它将 myDb 设置为第一个数据库,从那时起,始终返回该值。

您将需要设置数据库索引,并在数组中跟踪您的数据库。

于 2013-06-11T03:55:21.853 回答
0
public void createDynamicDatabase(Context context, String tableName, ArrayList<String> title) {

            Log.i("INSIDE createLoginDatabase() Method","*************creatLoginDatabase*********");
            try {

                int i;
                String queryString;
                myDataBase = context.openOrCreateDatabase("Db", Context.MODE_WORLD_WRITEABLE, null);         //Opens database in writable mode.
                //System.out.println("Table Name : "+tableName.get(0));

                queryString = title.get(0)+" VARCHAR(30),";
                Log.d("**createDynamicDatabase", "in oncreate");

                for(i = 1; i < title.size() - 1; i++)
                {               
                    queryString += title.get(i);
                    queryString +=" VARCHAR(30)";
                    queryString +=",";
                }

                queryString+= title.get(i) +" VARCHAR(30)";

                queryString = "CREATE TABLE IF NOT EXISTS " + tableName + "("+queryString+");";

                System.out.println("Create Table Stmt : "+ queryString);

                myDataBase.execSQL(queryString);
            } 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();
        }
于 2013-06-11T04:19:43.387 回答