系统日期更改时在SQLite中创建新表

时间:2018-01-31 06:56:12

标签: android sqlite android-sqlite

我一直在努力创建一个SQLite数据库,每年自动创建一个新表,其名称与abc_2018一致。问题是每次必须添加一个新表(,即年份更改)时,我需要更新DATABASE_VERSION。这可能需要存储当前DATABASE_VERSION并在每次必须添加新表时递增其值。我尝试使用SharedPreferences但我一直收到随机错误。

所以我的问题是如何建立一个机制,在用户的系统日期发生变化时自动创建一个表格,或者更准确地说,当新的一年开始时?“

修改

解决了我的问题:

try {
  cur = db.query(TABLE_NAME, PROJECTION, SELECTION, ARGS, null, null, null);
 } catch (SQLiteException e) {
   if (e.getMessage().contains("no such table")){
     // create new table and execute query
  }
}

<击>

2 个答案:

答案 0 :(得分:1)

您可以使用广播接收器,例如ACTION_DATE_CHANGED

可在https://developer.android.com/reference/android/content/Intent.html#ACTION_DATE_CHANGED

找到更多详情

代码:

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

答案 1 :(得分:1)

而不是: -

try {
  cur = db.query(TABLE_NAME, PROJECTION, SELECTION, ARGS, null, null, null);
 } catch (SQLiteException e) {
   if (e.getMessage().contains("no such table")){
     // create new table and execute query
  }
}

关于pskink评论中指出的潜在问题: -

  

if (e.getMessage().contains("no such table"))不不不,它太丑了   解决方法,我不知道该说什么......如果他们改变它   有一天"No such table"?还是"that table does not exist"

以下内容会更具弹性(考虑到SQLite如何满足向后兼容性): -

cur = db.query(sqlite_master,new String{"tbl_name"},"tbl_name=?",new String[]{TABLE_NAME},null,null,null);
if (cur.getCount < 1) { // ==0 if you prefer
    //Create new table
}