当系统日期更改时,在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
  }
}

而不是 :-

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
  }
}

关于普斯金克评论指出的潜在问题:

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
}

您可以使用广播接收器,如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();
            }

最新更新